Move AsyncToggle to TuskerComponents

This commit is contained in:
Shadowfacts 2024-04-08 18:45:54 -04:00
parent bdd4a4d755
commit f02afaac26
5 changed files with 17 additions and 23 deletions

View File

@ -1,6 +1,6 @@
// //
// TriStateToggle.swift // AsyncToggle.swift
// Tusker // TuskerComponents
// //
// Created by Shadowfacts on 4/7/24. // Created by Shadowfacts on 4/7/24.
// Copyright © 2024 Shadowfacts. All rights reserved. // Copyright © 2024 Shadowfacts. All rights reserved.
@ -8,26 +8,21 @@
import SwiftUI import SwiftUI
struct TriStateToggle: View { public struct AsyncToggle: View {
let titleKey: LocalizedStringKey let titleKey: LocalizedStringKey
@available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent") @available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent")
let labelHidden: Bool let labelHidden: Bool
@Binding var mode: Mode @Binding var mode: Mode
let onChange: (Bool) async -> Void let onChange: (Bool) async -> Void
init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding<Mode>, onChange: @escaping (Bool) async -> Void) { public init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding<Mode>, onChange: @escaping (Bool) async -> Void) {
self.titleKey = titleKey self.titleKey = titleKey
self.labelHidden = labelHidden self.labelHidden = labelHidden
self._mode = mode self._mode = mode
self.onChange = onChange self.onChange = onChange
} }
var body: some View { public var body: some View {
content
}
@ViewBuilder
private var content: some View {
if #available(iOS 16.0, *) { if #available(iOS 16.0, *) {
LabeledContent(titleKey) { LabeledContent(titleKey) {
toggleOrSpinner toggleOrSpinner
@ -64,7 +59,7 @@ struct TriStateToggle: View {
} }
} }
enum Mode { public enum Mode {
case off case off
case loading case loading
case on case on
@ -72,8 +67,8 @@ struct TriStateToggle: View {
} }
#Preview { #Preview {
@State var mode = TriStateToggle.Mode.on @State var mode = AsyncToggle.Mode.on
return TriStateToggle("", mode: $mode) { _ in return AsyncToggle("", mode: $mode) { _ in
try! await Task.sleep(nanoseconds: NSEC_PER_SEC) try! await Task.sleep(nanoseconds: NSEC_PER_SEC)
} }
} }

View File

@ -133,7 +133,6 @@
D6552367289870790048A653 /* ScreenCorners in Frameworks */ = {isa = PBXBuildFile; platformFilters = (ios, maccatalyst, ); productRef = D6552366289870790048A653 /* ScreenCorners */; }; D6552367289870790048A653 /* ScreenCorners in Frameworks */ = {isa = PBXBuildFile; platformFilters = (ios, maccatalyst, ); productRef = D6552366289870790048A653 /* ScreenCorners */; };
D659F35E2953A212002D944A /* TTTKit in Frameworks */ = {isa = PBXBuildFile; productRef = D659F35D2953A212002D944A /* TTTKit */; }; D659F35E2953A212002D944A /* TTTKit in Frameworks */ = {isa = PBXBuildFile; productRef = D659F35D2953A212002D944A /* TTTKit */; };
D659F36229541065002D944A /* TTTView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D659F36129541065002D944A /* TTTView.swift */; }; 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 */; }; D65A261D2BC39399005EB5D8 /* PushInstanceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */; };
D65B4B542971F71D00DABDFB /* EditedReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B532971F71D00DABDFB /* EditedReport.swift */; }; D65B4B542971F71D00DABDFB /* EditedReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B532971F71D00DABDFB /* EditedReport.swift */; };
D65B4B562971F98300DABDFB /* ReportView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D65B4B552971F98300DABDFB /* ReportView.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 = "<group>"; }; D6531DED246B81C9000F9538 /* GifvPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GifvPlayerView.swift; sourceTree = "<group>"; };
D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = "<group>"; }; D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = "<group>"; };
D659F36129541065002D944A /* TTTView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTTView.swift; sourceTree = "<group>"; }; D659F36129541065002D944A /* TTTView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TTTView.swift; sourceTree = "<group>"; };
D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TriStateToggle.swift; sourceTree = "<group>"; };
D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushInstanceSettingsView.swift; sourceTree = "<group>"; }; D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushInstanceSettingsView.swift; sourceTree = "<group>"; };
D65A26242BC39A02005EB5D8 /* PushNotifications */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PushNotifications; sourceTree = "<group>"; }; D65A26242BC39A02005EB5D8 /* PushNotifications */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PushNotifications; sourceTree = "<group>"; };
D65B4B532971F71D00DABDFB /* EditedReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditedReport.swift; sourceTree = "<group>"; }; D65B4B532971F71D00DABDFB /* EditedReport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditedReport.swift; sourceTree = "<group>"; };
@ -1203,7 +1201,6 @@
D64B967B2BC19C28002C8990 /* NotificationsPrefsView.swift */, D64B967B2BC19C28002C8990 /* NotificationsPrefsView.swift */,
D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */, D65A261C2BC39399005EB5D8 /* PushInstanceSettingsView.swift */,
D64B96832BC3893C002C8990 /* PushSubscriptionView.swift */, D64B96832BC3893C002C8990 /* PushSubscriptionView.swift */,
D65A261A2BC3928A005EB5D8 /* TriStateToggle.swift */,
); );
path = Notifications; path = Notifications;
sourceTree = "<group>"; sourceTree = "<group>";
@ -2153,7 +2150,6 @@
D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */, D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */,
D6D706A029466649000827ED /* ScrollingSegmentedControl.swift in Sources */, D6D706A029466649000827ED /* ScrollingSegmentedControl.swift in Sources */,
D686BBE324FBF8110068E6AA /* WrappedProgressView.swift in Sources */, D686BBE324FBF8110068E6AA /* WrappedProgressView.swift in Sources */,
D65A261B2BC3928A005EB5D8 /* TriStateToggle.swift in Sources */,
D620483423D3801D008A63EF /* LinkTextView.swift in Sources */, D620483423D3801D008A63EF /* LinkTextView.swift in Sources */,
D61F75882932DB6000C0B37F /* StatusSwipeActions.swift in Sources */, D61F75882932DB6000C0B37F /* StatusSwipeActions.swift in Sources */,
D68A76EA295285D0001DA1B3 /* AddHashtagPinnedTimelineView.swift in Sources */, D68A76EA295285D0001DA1B3 /* AddHashtagPinnedTimelineView.swift in Sources */,

View File

@ -10,10 +10,11 @@ import SwiftUI
import UserNotifications import UserNotifications
import UserAccounts import UserAccounts
import PushNotifications import PushNotifications
import TuskerComponents
struct NotificationsPrefsView: View { struct NotificationsPrefsView: View {
@State private var error: NotificationsSetupError? @State private var error: NotificationsSetupError?
@State private var isSetup = TriStateToggle.Mode.off @State private var isSetup = AsyncToggle.Mode.off
@State private var pushProxyRegistration: PushProxyRegistration? @State private var pushProxyRegistration: PushProxyRegistration?
@ObservedObject private var userAccounts = UserAccountsManager.shared @ObservedObject private var userAccounts = UserAccountsManager.shared
@ -32,7 +33,7 @@ struct NotificationsPrefsView: View {
private var enableSection: some View { private var enableSection: some View {
Section { Section {
TriStateToggle("Push Notifications", mode: $isSetup, onChange: isSetupChanged(newValue:)) AsyncToggle("Push Notifications", mode: $isSetup, onChange: isSetupChanged(newValue:))
} }
.appGroupedListRowBackground() .appGroupedListRowBackground()
.alertWithData("An Error Occurred", data: $error) { error in .alertWithData("An Error Occurred", data: $error) { error in

View File

@ -10,11 +10,12 @@ import SwiftUI
import UserAccounts import UserAccounts
import Pachyderm import Pachyderm
import PushNotifications import PushNotifications
import TuskerComponents
struct PushInstanceSettingsView: View { struct PushInstanceSettingsView: View {
let account: UserAccountInfo let account: UserAccountInfo
let pushProxyRegistration: PushProxyRegistration let pushProxyRegistration: PushProxyRegistration
@State private var mode: TriStateToggle.Mode @State private var mode: AsyncToggle.Mode
@State private var error: Error? @State private var error: Error?
@State private var subscription: PushNotifications.PushSubscription? @State private var subscription: PushNotifications.PushSubscription?
@ -32,7 +33,7 @@ struct PushInstanceSettingsView: View {
HStack { HStack {
PrefsAccountView(account: account) PrefsAccountView(account: account)
Spacer() 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() .labelsHidden()
} }
PushSubscriptionView(account: account, subscription: subscription, updateSubscription: updateSubscription) PushSubscriptionView(account: account, subscription: subscription, updateSubscription: updateSubscription)

View File

@ -9,6 +9,7 @@
import SwiftUI import SwiftUI
import UserAccounts import UserAccounts
import PushNotifications import PushNotifications
import TuskerComponents
struct PushSubscriptionView: View { struct PushSubscriptionView: View {
let account: UserAccountInfo let account: UserAccountInfo
@ -39,7 +40,7 @@ private struct PushSubscriptionSettingsView: View {
var body: some View { var body: some View {
VStack(alignment: .prefsAvatar) { VStack(alignment: .prefsAvatar) {
TriStateToggle("Mentions", mode: alertsBinding(for: .mention)) { AsyncToggle("Mentions", mode: alertsBinding(for: .mention)) {
await onChange(alert: .mention, value: $0) await onChange(alert: .mention, value: $0)
} }
} }
@ -51,7 +52,7 @@ private struct PushSubscriptionSettingsView: View {
.padding(.leading, 38) .padding(.leading, 38)
} }
private func alertsBinding(for alert: PushSubscription.Alerts) -> Binding<TriStateToggle.Mode> { private func alertsBinding(for alert: PushSubscription.Alerts) -> Binding<AsyncToggle.Mode> {
return Binding { return Binding {
isLoading[alert] == true ? .loading : subscription.alerts.contains(alert) ? .on : .off isLoading[alert] == true ? .loading : subscription.alerts.contains(alert) ? .on : .off
} set: { newValue in } set: { newValue in