Skip to content

Commit

Permalink
Add showAppleAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 17, 2025
1 parent 7862c1b commit b55842e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/Helper/HelperProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ let helperServiceName = "org.pqrs.TrueWidget.Helper"

@objc
protocol HelperProtocol {
//
// AppleAccount
//

func appleAccount(reply: @escaping (String) -> Void)

//
// BundleVersions
//
Expand Down
11 changes: 11 additions & 0 deletions src/Helper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class HelperService: NSObject, NSXPCListenerDelegate, HelperProtocol {
return true
}

//
// AppleAccount
//

func appleAccount(reply: @escaping (String) -> Void) {
let account =
(UserDefaults.standard.persistentDomain(forName: "MobileMeAccounts")?["Accounts"]
as? [[String: Any]])?.first?["AccountID"] as? String ?? ""
reply(account)
}

//
// BundleVersions
//
Expand Down
1 change: 1 addition & 0 deletions src/TrueWidget/swift/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ final class UserSettings: ObservableObject {
@AppStorage("showHostName") var showHostName: Bool = true
@AppStorage("showRootVolumeName") var showRootVolumeName: Bool = false
@AppStorage("showUserName") var showUserName: Bool = false
@AppStorage("showAppleAccount") var showAppleAccount: Bool = false

//
// Xcode
Expand Down
10 changes: 10 additions & 0 deletions src/TrueWidget/swift/Views/Main/MainOperatingSystemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ struct MainOperatingSystemView: View {
if userSettings.showUserName {
Text(operatingSystem.userName)
}

if userSettings.showAppleAccount {
// The spacing between the icon and text is too wide when using a Label, so managing it manually with an HStack.
HStack(alignment: .center, spacing: 4) {
Image(systemName: "apple.logo")
Text(
operatingSystem.appleAccount.isEmpty ? "---" : operatingSystem.appleAccount
)
}
}
}
.frame(maxWidth: .infinity, alignment: .trailing)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ struct SettingsOperatingSystemView: View {
Text("Show user name")
}
.switchToggleStyle()

Toggle(isOn: $userSettings.showAppleAccount) {
Text("Show Apple Account")
}
.switchToggleStyle()
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
Expand Down
42 changes: 26 additions & 16 deletions src/TrueWidget/swift/WidgetSource/OperatingSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extension WidgetSource {
@Published public var hostName = ""
@Published public var rootVolumeName = ""
@Published public var userName = ""
@Published public var appleAccount = ""

let timer: AsyncTimerSequence<ContinuousClock>
var timerTask: Task<Void, Never>?
Expand Down Expand Up @@ -59,25 +60,34 @@ extension WidgetSource {
return ""
}

@MainActor
private func update() {
if !userSettings.showHostName {
return
}

// `ProcessInfo.processInfo.hostName` is not reflected the host name changes after the application is launched.
// So, we have to use `gethostname`.`
let length = 128
var buffer = [CChar](repeating: 0, count: length)
let error = gethostname(&buffer, length)
if error == 0 {
if let name = String(utf8String: buffer) {
var h = name
if let index = name.firstIndex(of: ".") {
h = String(name[...index].dropLast())
if userSettings.showHostName {
// `ProcessInfo.processInfo.hostName` is not reflected the host name changes after the application is launched.
// So, we have to use `gethostname`.`
let length = 128
var buffer = [CChar](repeating: 0, count: length)
let error = gethostname(&buffer, length)
if error == 0 {
if let name = String(utf8String: buffer) {
var h = name
if let index = name.firstIndex(of: ".") {
h = String(name[...index].dropLast())
}

if hostName != h {
hostName = h
}
}
}
}

if hostName != h {
hostName = h
if userSettings.showAppleAccount {
HelperClient.shared.proxy?.appleAccount { account in
Task { @MainActor in
if self.appleAccount != account {
self.appleAccount = account
}
}
}
}
Expand Down

0 comments on commit b55842e

Please sign in to comment.