-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from winebarrel/add_settings_view
Add SettingsView
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -30,6 +30,8 @@ playground.xcworkspace | |
# .swiftpm | ||
|
||
.build/ | ||
build/ | ||
build.log | ||
|
||
# CocoaPods | ||
# | ||
|
This file contains 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,6 @@ | ||
import Foundation | ||
|
||
enum Constants { | ||
static let defaultGithubQuery = "is:open is:pr author:@me archived:false" | ||
static let defaultInterval: TimeInterval = 60 | ||
} |
This file contains 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,37 @@ | ||
// from https://zenn.dev/kyome/articles/a8ac1eb11b3c1d | ||
import SwiftUI | ||
|
||
struct PreActionButtonStyle: PrimitiveButtonStyle { | ||
let preAction: () -> Void | ||
|
||
init(preAction: @escaping () -> Void) { | ||
self.preAction = preAction | ||
} | ||
|
||
func makeBody(configuration: Configuration) -> some View { | ||
Button(role: configuration.role) { | ||
preAction() | ||
configuration.trigger() | ||
} label: { | ||
configuration.label | ||
} | ||
} | ||
} | ||
|
||
struct PreActionButtonStyleModifier: ViewModifier { | ||
let preAction: () -> Void | ||
|
||
init(preAction: @escaping () -> Void) { | ||
self.preAction = preAction | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content.buttonStyle(PreActionButtonStyle(preAction: preAction)) | ||
} | ||
} | ||
|
||
extension View { | ||
func preActionButtonStyle(preAction: @escaping () -> Void) -> some View { | ||
modifier(PreActionButtonStyleModifier(preAction: preAction)) | ||
} | ||
} |
This file contains 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 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 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,72 @@ | ||
import ServiceManagement | ||
import SwiftUI | ||
|
||
struct SettingView: View { | ||
@Binding var githubToken: String | ||
@AppStorage("interval") private var interval = Constants.defaultInterval | ||
@AppStorage("githubQuery") private var githubQuery = Constants.defaultGithubQuery | ||
@State private var launchAtLogin: Bool = SMAppService.mainApp.status == .enabled | ||
|
||
var body: some View { | ||
Form { | ||
SecureField(text: $githubToken) { | ||
Link("GitHub token", destination: URL(string: "https://github.com/settings/tokens")!) | ||
}.onChange(of: githubToken) { | ||
Vault.githubToken = githubToken | ||
} | ||
TextField("Interval (sec)", value: $interval, format: .number.grouping(.never)) | ||
.onChange(of: interval) { | ||
if interval < 1 { | ||
interval = 1 | ||
} else if interval > 3600 { | ||
interval = 3600 | ||
} | ||
} | ||
HStack { | ||
TextField(text: $githubQuery, axis: .vertical) { | ||
Link("Search query", destination: URL(string: "https://docs.github.com/search-github/searching-on-github/searching-issues-and-pull-requests")!) | ||
} | ||
.lineLimit(5...) | ||
Link(destination: URL(string: "https://github.com/pulls?q=" + (githubQuery.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""))!) { | ||
Image(systemName: "magnifyingglass") | ||
} | ||
} | ||
Toggle("Launch at login", isOn: $launchAtLogin) | ||
.onChange(of: launchAtLogin) { | ||
do { | ||
if launchAtLogin { | ||
try SMAppService.mainApp.register() | ||
} else { | ||
try SMAppService.mainApp.unregister() | ||
} | ||
} catch { | ||
Logger.shared.error("failed to update 'Launch at login': \(error)") | ||
} | ||
} | ||
Link(destination: URL(string: "https://github.com/winebarrel/Pulse")!) { | ||
// swiftlint:disable force_cast | ||
let appVer = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String | ||
let buildVer = Bundle.main.infoDictionary!["CFBundleVersion"] as! String | ||
// swiftlint:enable force_cast | ||
Text("Ver. \(appVer).\(buildVer)") | ||
} | ||
.frame(maxWidth: .infinity, alignment: .trailing) | ||
} | ||
.padding(20) | ||
.frame(width: 400) | ||
} | ||
|
||
func onClosed(_ action: @escaping () -> Void) -> some View { | ||
onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { notification in | ||
if let window = notification.object as? NSWindow, window.title == "Pulse Settings" { | ||
action() | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
SettingView( | ||
githubToken: .constant("") | ||
) | ||
} |