Skip to content
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

Refresh permissions automatically #43

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions Swift Shift/src/app/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import ShortcutRecorder
import Sparkle

struct SettingsView: View {
@State var hasPermissions = false
private var version: String? = nil
@State private var hasPermissions = false
private var version: String?
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

init(hasPermissions: Bool = false) {
self.hasPermissions = hasPermissions
self._hasPermissions = State(initialValue: hasPermissions)
self.version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

private func refreshPermissions() {
hasPermissions = PermissionsManager.hasAccessibilityPermission()
hasPermissions = PermissionsManager.hasAccessibilityPermission()
}

var body: some View {
Expand Down Expand Up @@ -52,21 +53,16 @@ struct SettingsView: View {
}
})
.keyboardShortcut("Q", modifiers: .command)

if !hasPermissions {
Button("Refresh permissions") {
refreshPermissions()
}
.buttonStyle(.borderedProminent)
.tint(.orange)
}
}
.padding([.bottom, .horizontal])
.padding(.top, 5)
}
.onAppear {
refreshPermissions()
}
.onReceive(timer) { _ in
refreshPermissions()
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion Swift Shift/src/permissions/PermissionsRequestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct PermissionRequestView: View {
.lineLimit(2)
HStack {
Text("Accessibility").font(.subheadline)
Image(systemName: "exclamationmark.triangle").foregroundColor(.orange)
Spacer()
Button("Open Preferences") {
PermissionsManager.openPreferences(at: .accessibility)
Expand All @@ -20,5 +21,5 @@ struct PermissionRequestView: View {
}

#Preview {
PermissionRequestView()
PermissionRequestView().padding()
}