diff --git a/Tusker/Screens/Preferences/Notifications/TriStateToggle.swift b/Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift similarity index 78% rename from Tusker/Screens/Preferences/Notifications/TriStateToggle.swift rename to Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift index 40936c94..fde3aaca 100644 --- a/Tusker/Screens/Preferences/Notifications/TriStateToggle.swift +++ b/Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift @@ -1,6 +1,6 @@ // -// TriStateToggle.swift -// Tusker +// AsyncToggle.swift +// TuskerComponents // // Created by Shadowfacts on 4/7/24. // Copyright © 2024 Shadowfacts. All rights reserved. @@ -8,26 +8,21 @@ import SwiftUI -struct TriStateToggle: View { +public struct AsyncToggle: View { let titleKey: LocalizedStringKey @available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent") let labelHidden: Bool @Binding var mode: Mode let onChange: (Bool) async -> Void - init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding, onChange: @escaping (Bool) async -> Void) { + public init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding, onChange: @escaping (Bool) async -> Void) { self.titleKey = titleKey self.labelHidden = labelHidden self._mode = mode self.onChange = onChange } - var body: some View { - content - } - - @ViewBuilder - private var content: some View { + public var body: some View { if #available(iOS 16.0, *) { LabeledContent(titleKey) { toggleOrSpinner @@ -64,7 +59,7 @@ struct TriStateToggle: View { } } - enum Mode { + public enum Mode { case off case loading case on @@ -72,8 +67,8 @@ struct TriStateToggle: View { } #Preview { - @State var mode = TriStateToggle.Mode.on - return TriStateToggle("", mode: $mode) { _ in + @State var mode = AsyncToggle.Mode.on + return AsyncToggle("", mode: $mode) { _ in try! await Task.sleep(nanoseconds: NSEC_PER_SEC) } } diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index f2e6a02a..73b63548 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -133,7 +133,6 @@ D6552367289870790048A653 /* ScreenCorners in Frameworks */ = {isa = PBXBuildFile; platformFilters = (ios, maccatalyst, ); productRef = D6552366289870790048A653 /* ScreenCorners */; }; D659F35E2953A212002D944A /* TTTKit in Frameworks */ = {isa = PBXBuildFile; productRef = D659F35D2953A212002D944A /* TTTKit */; }; D659F36229541065002D944A /* TTTView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D659F36129541065002D944A /* TTTView.swift */; }; - D65A261B2BC3928A005EB5D8 /* TriStateToggle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */; }; D65A261D2BC39399005EB5D8 /* PushInstanceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */; }; D65B4B542971F71D00DABDFB /* EditedReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B532971F71D00DABDFB /* EditedReport.swift */; }; D65B4B562971F98300DABDFB /* ReportView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B552971F98300DABDFB /* ReportView.swift */; }; @@ -536,7 +535,6 @@ D6531DED246B81C9000F9538 /* GifvPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GifvPlayerView.swift; sourceTree = ""; }; D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = ""; }; D659F36129541065002D944A /* TTTView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTTView.swift; sourceTree = ""; }; - D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TriStateToggle.swift; sourceTree = ""; }; D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushInstanceSettingsView.swift; sourceTree = ""; }; D65A26242BC39A02005EB5D8 /* PushNotifications */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PushNotifications; sourceTree = ""; }; D65B4B532971F71D00DABDFB /* EditedReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditedReport.swift; sourceTree = ""; }; @@ -1203,7 +1201,6 @@ D64B967B2BC19C28002C8990 /* NotificationsPrefsView.swift */, D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */, D64B96832BC3893C002C8990 /* PushSubscriptionView.swift */, - D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */, ); path = Notifications; sourceTree = ""; @@ -2153,7 +2150,6 @@ D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */, D6D706A029466649000827ED /* ScrollingSegmentedControl.swift in Sources */, D686BBE324FBF8110068E6AA /* WrappedProgressView.swift in Sources */, - D65A261B2BC3928A005EB5D8 /* TriStateToggle.swift in Sources */, D620483423D3801D008A63EF /* LinkTextView.swift in Sources */, D61F75882932DB6000C0B37F /* StatusSwipeActions.swift in Sources */, D68A76EA295285D0001DA1B3 /* AddHashtagPinnedTimelineView.swift in Sources */, diff --git a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift index 5c15083d..21d88ecd 100644 --- a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift +++ b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift @@ -10,10 +10,11 @@ import SwiftUI import UserNotifications import UserAccounts import PushNotifications +import TuskerComponents struct NotificationsPrefsView: View { @State private var error: NotificationsSetupError? - @State private var isSetup = TriStateToggle.Mode.off + @State private var isSetup = AsyncToggle.Mode.off @State private var pushProxyRegistration: PushProxyRegistration? @ObservedObject private var userAccounts = UserAccountsManager.shared @@ -32,7 +33,7 @@ struct NotificationsPrefsView: View { private var enableSection: some View { Section { - TriStateToggle("Push Notifications", mode: $isSetup, onChange: isSetupChanged(newValue:)) + AsyncToggle("Push Notifications", mode: $isSetup, onChange: isSetupChanged(newValue:)) } .appGroupedListRowBackground() .alertWithData("An Error Occurred", data: $error) { error in diff --git a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift index 0dc4ecc3..6eddea60 100644 --- a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift +++ b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift @@ -10,11 +10,12 @@ import SwiftUI import UserAccounts import Pachyderm import PushNotifications +import TuskerComponents struct PushInstanceSettingsView: View { let account: UserAccountInfo let pushProxyRegistration: PushProxyRegistration - @State private var mode: TriStateToggle.Mode + @State private var mode: AsyncToggle.Mode @State private var error: Error? @State private var subscription: PushNotifications.PushSubscription? @@ -32,7 +33,7 @@ struct PushInstanceSettingsView: View { HStack { PrefsAccountView(account: account) Spacer() - TriStateToggle("\(account.instanceURL.host!) notifications enabled", labelHidden: true, mode: $mode, onChange: updateNotificationsEnabled(enabled:)) + AsyncToggle("\(account.instanceURL.host!) notifications enabled", labelHidden: true, mode: $mode, onChange: updateNotificationsEnabled(enabled:)) .labelsHidden() } PushSubscriptionView(account: account, subscription: subscription, updateSubscription: updateSubscription) diff --git a/Tusker/Screens/Preferences/Notifications/PushSubscriptionView.swift b/Tusker/Screens/Preferences/Notifications/PushSubscriptionView.swift index f33969fd..3d4d6e3d 100644 --- a/Tusker/Screens/Preferences/Notifications/PushSubscriptionView.swift +++ b/Tusker/Screens/Preferences/Notifications/PushSubscriptionView.swift @@ -9,6 +9,7 @@ import SwiftUI import UserAccounts import PushNotifications +import TuskerComponents struct PushSubscriptionView: View { let account: UserAccountInfo @@ -39,7 +40,7 @@ private struct PushSubscriptionSettingsView: View { var body: some View { VStack(alignment: .prefsAvatar) { - TriStateToggle("Mentions", mode: alertsBinding(for: .mention)) { + AsyncToggle("Mentions", mode: alertsBinding(for: .mention)) { await onChange(alert: .mention, value: $0) } } @@ -51,7 +52,7 @@ private struct PushSubscriptionSettingsView: View { .padding(.leading, 38) } - private func alertsBinding(for alert: PushSubscription.Alerts) -> Binding { + private func alertsBinding(for alert: PushSubscription.Alerts) -> Binding { return Binding { isLoading[alert] == true ? .loading : subscription.alerts.contains(alert) ? .on : .off } set: { newValue in