Issue #1: format the BlockedCount number output to locale.
This commit is contained in:
parent
4e591c9597
commit
9f30f9f686
1 changed files with 21 additions and 16 deletions
|
|
@ -3,8 +3,18 @@ import SwiftUI
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct BlockedCount: View {
|
struct BlockedCount: View {
|
||||||
|
|
||||||
@State private var txtRecord: String = "..."
|
@State private var txtRecord: String = "..."
|
||||||
|
|
||||||
|
private static let startIndex = 44
|
||||||
|
|
||||||
|
private static let formatter: NumberFormatter = {
|
||||||
|
let formatter = NumberFormatter()
|
||||||
|
formatter.numberStyle = .decimal
|
||||||
|
|
||||||
|
return formatter
|
||||||
|
}()
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Text(txtRecord)
|
Text(txtRecord)
|
||||||
.onAppear {
|
.onAppear {
|
||||||
|
|
@ -16,26 +26,21 @@ struct BlockedCount: View {
|
||||||
// This is a DNS wire format response and we make a lot of assumptions
|
// This is a DNS wire format response and we make a lot of assumptions
|
||||||
// It is not critical functionality so just let it fail if it fails
|
// It is not critical functionality so just let it fail if it fails
|
||||||
|
|
||||||
guard data.count > 44 else { return nil }
|
guard data.count > Self.startIndex else {
|
||||||
|
return nil
|
||||||
let startIndex = 44
|
|
||||||
let subData = data.suffix(from: startIndex)
|
|
||||||
|
|
||||||
// Find the first space character (ASCII 32)
|
|
||||||
var endIndex: Int?
|
|
||||||
for (offset, byte) in subData.enumerated() {
|
|
||||||
if byte == 32 { // Space character
|
|
||||||
endIndex = offset
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let foundEndIndex = endIndex else { return nil }
|
// Find the first space character (ASCII 32).
|
||||||
|
guard let endIndex = data.suffix(from: Self.startIndex).firstIndex(of: 32) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Extract bytes between start and space
|
guard let numberString = String(data: data[Self.startIndex ..< endIndex], encoding: .utf8)
|
||||||
let numberData = subData.prefix(foundEndIndex)
|
else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return String(decoding: numberData, as: UTF8.self)
|
return Self.formatter.string(for: Int(numberString))
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchTXTRecord() {
|
func fetchTXTRecord() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue