Repository navigation
feat(ios): every notification choice under Home, and the not-following notice #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import FTWKit | ||
| import SwiftUI | ||
| import UserNotifications | ||
|
|
||
| /// Home › Notifications: whether this iPhone lets FTW notify, then the box's | ||
| /// own choices, the same ones the web app and the box's settings show. The | ||
| /// box decides when to send; these say which kinds it sends at all. | ||
| struct NotifySettingsView: View { | ||
| let home: HomeModels | ||
| @State private var allowed: UNAuthorizationStatus? | ||
| @Environment(\.scenePhase) private var scenePhase | ||
|
|
||
| private var rules: NotifyRules { home.notify } | ||
|
|
||
| var body: some View { | ||
| List { | ||
| phone | ||
| box | ||
| if rules.phase == .ready, rules.canChange { | ||
| ForEach(NotifyRules.Group.allCases, id: \.self) { group in | ||
| let kinds = rules.kinds.filter { NotifyRules.group($0) == group } | ||
| if !kinds.isEmpty { | ||
| Section(Self.title(group)) { | ||
| ForEach(kinds, id: \.self) { kind in | ||
| Toggle(NotifyRules.label(kind), isOn: Binding( | ||
| get: { rules.on[kind] == true }, | ||
| set: { value in Task { await rules.set(kind, value) } } | ||
| )) | ||
| .accessibilityIdentifier("notify.\(kind)") | ||
| } | ||
| } | ||
| .disabled(!rules.boxOn) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| .listStyle(.insetGrouped) | ||
| .scrollContentBackground(.hidden) | ||
| .background(Theme.ground) | ||
| .navigationTitle(L("Notifications")) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| .animation(Theme.spring, value: rules.kinds) | ||
| // Read when the view opens and when the session comes back. | ||
| .task(id: home.site.session.phase == .streaming) { await rules.load() } | ||
| // Back from the iPhone's Settings, the answer may have changed. | ||
| .task(id: scenePhase) { allowed = await Notifications.allowed() } | ||
| } | ||
|
|
||
| /// This iPhone's own answer: asked here if never asked, or the way to | ||
| /// Settings if refused. | ||
| @ViewBuilder private var phone: some View { | ||
| switch allowed { | ||
| case .notDetermined?: | ||
| Section { | ||
| Button { | ||
| Task { | ||
| await Notifications.askAndRegister() | ||
| allowed = await Notifications.allowed() | ||
| } | ||
| } label: { | ||
| Label(L("Allow notifications on this iPhone"), systemImage: "bell.badge") | ||
| } | ||
| .accessibilityIdentifier("notify.allow") | ||
| } | ||
| case .denied?: | ||
| Section { | ||
| Text(L("Notifications for FTW are off in the iPhone's Settings.")) | ||
| if let url = Notifications.settingsURL { | ||
| Link(L("Open Settings"), destination: url) | ||
| } | ||
| } | ||
| default: | ||
| EmptyView() | ||
| } | ||
| } | ||
|
|
||
| /// The box's own switch, or why there is none to show. | ||
| @ViewBuilder private var box: some View { | ||
| Section { | ||
| if !home.site.canConfigure { | ||
| Text(L("Notifications are chosen by this home's owner.")).foregroundStyle(Theme.ink2) | ||
| } else if rules.phase == .oldBox { | ||
| Text(AppWords.text("Your box doesn't have that yet — it may be running older software.")).foregroundStyle(Theme.ink2) | ||
| } else if rules.phase == .unknown { | ||
| Text(L(home.site.session.phase == .streaming ? "Reading your box…" : "Shown when your box is in reach.")) | ||
| .foregroundStyle(Theme.ink2) | ||
| } else { | ||
| Toggle(L("Notifications from your box"), isOn: Binding( | ||
| get: { rules.boxOn }, | ||
| set: { value in Task { await rules.setBoxOn(value) } } | ||
| )) | ||
| .accessibilityIdentifier("notify.box") | ||
| } | ||
| } footer: { | ||
| VStack(alignment: .leading, spacing: 4) { | ||
| if rules.phase == .ready { | ||
| Text(L("Your box decides when to send them, and every owner of this home gets the same. Notices about access come even with this off.")) | ||
| } | ||
| if let problem = rules.problem { | ||
| Text(problem).foregroundStyle(Theme.alarm) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static func title(_ group: NotifyRules.Group) -> String { | ||
| switch group { | ||
| case .car: return L("The car") | ||
| case .home: return L("The box and its devices") | ||
| case .access: return L("Access") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If notification permission was denied before this launch, following “Open Settings” and granting permission only refreshes
allowedwhen the user returns; it never registers with APNs.Notifications.start()skips registration for denied permission, andSiteModel.installPushSink()does not reinstall an existing sink when the connection resumes. Consequently, the warning disappears but this phone still receives no notifications until the app is relaunched. When the scene becomes active with permission granted, also register for remote notifications.Useful? React with 👍 / 👎.