Skip to content

Commit

Permalink
✨ Add a feature automatically at login (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
techinpark authored Mar 7, 2021
1 parent 5ad1711 commit e346998
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 21 deletions.
38 changes: 34 additions & 4 deletions Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Cocoa
import SwiftSoup
import Then
import LaunchAtLogin

@main
class AppDelegate: NSObject, NSApplicationDelegate {
Expand Down Expand Up @@ -50,7 +51,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
$0.keyEquivalent = ""
$0.tag = 4
}

private let quitMenuItem = NSMenuItem().then {
$0.title = Localized.quit
$0.action = #selector(onQuitClick)
Expand All @@ -59,18 +60,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

private let friendMenuItem = NSMenuItem().then {
$0.title = Localized.change_friend_username
$0.title = Localized.changeFriendUsername
$0.action = #selector(onChangeFriendUsernameClick)
$0.tag = 6
$0.keyEquivalent = "f"
}

private let RemoveFriendMenuItem = NSMenuItem().then {
$0.title = Localized.remove_friend_username
$0.title = Localized.removeFriendUsername
$0.action = #selector(onRemoveFriendUsernameClick)
$0.tag = 6
$0.keyEquivalent = "d"
}

private let settingMenuItem = NSMenuItem().then {
$0.title = Localized.setting
$0.action = #selector(onSettingClick)
$0.keyEquivalent = "s"
$0.tag = 7
}

func applicationDidFinishLaunching(_: Notification) {

Expand Down Expand Up @@ -104,6 +112,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(.separator())
menu.addItem(refreshMenuItem)
menu.addItem(changeUserMenuItem)
menu.addItem(settingMenuItem)
menu.addItem(quitMenuItem)

statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
Expand All @@ -122,11 +131,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let userMenuItemTitle = Localized.hello.replacingOccurrences(of: "${username}", with: username).replacingOccurrences(of: "${withFriend}", with: withFriend)
userMenuItem.attributedTitle = NSAttributedString(string: userMenuItemTitle)

let friendMenuItemTitle = self.friendUsername.isEmpty ? Localized.set_friend_username : Localized.change_friend_username
let friendMenuItemTitle = self.friendUsername.isEmpty ? Localized.setFriendUsername : Localized.changeFriendUsername
friendMenuItem.title = friendMenuItemTitle

RemoveFriendMenuItem.isHidden = self.friendUsername.isEmpty
}

private func showSettingAlert() {
let alert = NSAlert()

alert.messageText = Localized.settingTitle
let button = NSButton(checkboxWithTitle: Localized.autoLaunch, target: nil, action: #selector(setupLauchToggle))
button.state = LaunchAtLogin.isEnabled ? .on : .off
alert.accessoryView = button

if alert.runModal() == .alertFirstButtonReturn {
return
}
}

private func showChangeUsernameAlert() {
let alert = NSAlert()
Expand Down Expand Up @@ -220,6 +242,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let url = URL(string: "https://github.com/techinpark/Jandi")!
NSWorkspace.shared.open(url)
}

@objc func onSettingClick() {
showSettingAlert()
}

@objc func setupLauchToggle() {
LaunchAtLogin.isEnabled.toggle()
}


private func changeUsername(withUsername username: String) {
Expand Down
19 changes: 12 additions & 7 deletions Sources/Consts/Localized.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ enum Localized {
static let withFriend = NSLocalizedString("with_friend", comment: "\nwith @${username}")
static let refresh = NSLocalizedString("refresh", comment: "refresh")
static let changeUsername = NSLocalizedString("change_username", comment: "Change username")
static let change_friend_username = NSLocalizedString("change_friend_username", comment: "Change friend username")
static let set_friend_username = NSLocalizedString("set_friend_username", comment: "Set friend username")
static let remove_friend_username = NSLocalizedString("remove_friend_username", comment: "Remove friend username")
static let changeFriendUsername = NSLocalizedString("change_friend_username", comment: "Change friend username")
static let setFriendUsername = NSLocalizedString("set_friend_username", comment: "Set friend username")
static let removeFriendUsername = NSLocalizedString("remove_friend_username", comment: "Remove friend username")
static let help = NSLocalizedString("help", comment: "help")
static let textFieldPlaceholder = NSLocalizedString("textfield_placeholder", comment: "Github username")
static let setUsernameDescription = NSLocalizedString("username_description", comment: "Set username")
static let changeUsernameDescription = NSLocalizedString("change_username_description", comment: "Change username")
static let autoLaunch = NSLocalizedString("auto_launch", comment: "Automatically start Jandi at login")
static let information = NSLocalizedString("information", comment: "Enter your GitHub username. We’ll fetch the number of contributions.")
static let friend_information = NSLocalizedString("friend_information", comment: "Enter your friend GitHub username. We'll fetch the number of contributions.")
static let streak_first_stage = NSLocalizedString("streak_first_stage", comment: "streak stage 1")
static let streak_second_stage = NSLocalizedString("streak_second_stage", comment: "streak stage 2")
static let streak_third_stage = NSLocalizedString("streak_third_stage", comment: "streak stage 3")
static let streak_fourth_stage = NSLocalizedString("streak_fourth_stage", comment: "streak stage 4")
static let setting = NSLocalizedString("setting", comment: "Preferences")
static let settingTitle = NSLocalizedString("setting_title", comment: "Preferences Title")

static let streakFristStage = NSLocalizedString("streak_first_stage", comment: "streak stage 1")
static let streakSecondStage = NSLocalizedString("streak_second_stage", comment: "streak stage 2")
static let streakThirdStage = NSLocalizedString("streak_third_stage", comment: "streak stage 3")
static let streakForthStage = NSLocalizedString("streak_fourth_stage", comment: "streak stage 4")

static let ok = NSLocalizedString("ok", comment: "Okay")
static let cancel = NSLocalizedString("cancel", comment: "cancel")
static let error = NSLocalizedString("error", comment: "error")
Expand Down
8 changes: 4 additions & 4 deletions Sources/Extensions/Int+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ extension Int {
func getStreaks() -> String {
switch self {
case 0:
return Localized.streak_first_stage
return Localized.streakFristStage
case 1 ..< 4:
return Localized.streak_second_stage
return Localized.streakSecondStage
case 4 ..< 10:
return Localized.streak_third_stage.replacingOccurrences(of: "${day}", with: self.description)
return Localized.streakThirdStage.replacingOccurrences(of: "${day}", with: self.description)
default:
return Localized.streak_fourth_stage.replacingOccurrences(of: "${day}", with: self.description)
return Localized.streakForthStage.replacingOccurrences(of: "${day}", with: self.description)
}
}
}
5 changes: 4 additions & 1 deletion Supporting FIles/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"wellcome_user_message" = "👋 Hello, @${username} ${withFriend}";
"with_friend" = "\nwith @${username}";
"refresh" = "⏳ Refresh";
"change_username" = "⚙️ Change username";
"change_username" = "🪄 Change username";
"change_friend_username" = "🤝 Change Friend username";
"set_friend_username" = "🤝 Set Friend username";
"remove_friend_username" = "🙋‍♂️ Remove Friend username";
"help" = "🐛 Bug Report";
"auto_launch" = "Automatically start Jandi at login";
"textfield_placeholder" = "GitHub username";
"username_description" = "Set username";
"change_username_description" = "Change username";
Expand All @@ -23,6 +24,8 @@
"streak_second_stage" = "🌱 I planted my first Jandi today.";
"streak_third_stage" = "🌿 It has been ${day} days since the Jandi sprouts.";
"streak_fourth_stage" = "🌳 It's been ${day} days since the Jandi has grown well.";
"setting" = "⚙️ Preferences";
"setting_title" = "App Preferences";
"ok" = "Okay";
"cancel" = "Cancel";
"error" = "⚠️ Error";
Expand Down
5 changes: 4 additions & 1 deletion Supporting FIles/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"wellcome_user_message" = "👋 안녕, @${username} ${withFriend}";
"with_friend" = "\n @${username}와 함께";
"refresh" = "⏳ 새로고침";
"change_username" = "⚙️ 아이디 변경";
"change_username" = "🪄 아이디 변경";
"change_friend_username" = "🤝 친구 아이디 변경";
"set_friend_username" = "🤝 친구 아이디 설정";
"remove_friend_username" = "🙋‍♂️ 친구 아이디 제거";
"help" = "🐛 버그 제보하기";
"auto_launch" = "재시작시 자동 실행";
"textfield_placeholder" = "Github 아이디";
"username_description" = "아이디를 입력해주세요";
"change_username_description" = "아이디를 변경합니다";
Expand All @@ -23,6 +24,8 @@
"streak_second_stage" = "🌱 오늘 처음으로 잔디를 심었어요.";
"streak_third_stage" = "🌿 잔디에 싹이 튼 지 ${day}일이 되었어요.";
"streak_fourth_stage" = "🌳 잔디가 무럭무럭 자란지 ${day}일이 되었어요.";
"setting" = "⚙️ 설정";
"setting_title" = "환경설정";
"ok" = "확인";
"cancel" = "취소";
"error" = "⚠️ 오류";
Expand Down
47 changes: 43 additions & 4 deletions jandi.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ED18259525C40ACC00FCD100 /* Then in Frameworks */ = {isa = PBXBuildFile; productRef = ED18259425C40ACC00FCD100 /* Then */; };
ED18259C25C4420A00FCD100 /* Int+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED18259B25C4420A00FCD100 /* Int+Extensions.swift */; };
ED1825A025C4422D00FCD100 /* Date+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED18259F25C4422D00FCD100 /* Date+Extensions.swift */; };
ED33D3CC25F3805400C18DE9 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = ED33D3CB25F3805400C18DE9 /* LaunchAtLogin */; };
ED3FF1F225E3EEAA008CBD34 /* ContributeData.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED3FF1F125E3EEA1008CBD34 /* ContributeData.swift */; };
ED8F6FE425C461320099D861 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = ED8F6FE625C461320099D861 /* Localizable.strings */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -51,6 +52,7 @@
buildActionMask = 2147483647;
files = (
ED18259125C4098C00FCD100 /* SwiftSoup in Frameworks */,
ED33D3CC25F3805400C18DE9 /* LaunchAtLogin in Frameworks */,
ED18259525C40ACC00FCD100 /* Then in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -137,6 +139,7 @@
buildPhases = (
ED18257725C4096400FCD100 /* Sources */,
ED18257825C4096400FCD100 /* Frameworks */,
ED33D3CE25F3808B00C18DE9 /* LaunchAtLogin */,
ED18257925C4096400FCD100 /* Resources */,
);
buildRules = (
Expand All @@ -147,6 +150,7 @@
packageProductDependencies = (
ED18259025C4098C00FCD100 /* SwiftSoup */,
ED18259425C40ACC00FCD100 /* Then */,
ED33D3CB25F3805400C18DE9 /* LaunchAtLogin */,
);
productName = jandi;
productReference = ED18257B25C4096400FCD100 /* jandi.app */;
Expand Down Expand Up @@ -179,6 +183,7 @@
packageReferences = (
ED18258F25C4098C00FCD100 /* XCRemoteSwiftPackageReference "SwiftSoup" */,
ED18259325C40ACC00FCD100 /* XCRemoteSwiftPackageReference "Then" */,
ED33D3CA25F3805400C18DE9 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */,
);
productRefGroup = ED18257C25C4096400FCD100 /* Products */;
projectDirPath = "";
Expand All @@ -202,6 +207,27 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
ED33D3CE25F3808B00C18DE9 /* LaunchAtLogin */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = LaunchAtLogin;
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "# Type a script or drag a script file from your workspace to insert its path.\n\"${BUILT_PRODUCTS_DIR}/LaunchAtLogin_LaunchAtLogin.bundle/Contents/Resources/copy-helper-swiftpm.sh\"\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
ED18257725C4096400FCD100 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -369,7 +395,7 @@
CODE_SIGN_ENTITLEMENTS = "$(SRCROOT)/Sources/jandi.entitlements";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 10;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = U6GQL8JQMT;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "$(SRCROOT)/Sources/Info.plist";
Expand All @@ -378,7 +404,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 1.5;
MARKETING_VERSION = 1.7;
PRODUCT_BUNDLE_IDENTIFIER = com.tmsae.jandi;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -394,7 +420,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 10;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = U6GQL8JQMT;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "$(SRCROOT)/Sources/Info.plist";
Expand All @@ -403,7 +429,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 1.5;
MARKETING_VERSION = 1.7;
PRODUCT_BUNDLE_IDENTIFIER = com.tmsae.jandi;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -450,6 +476,14 @@
minimumVersion = 2.7.0;
};
};
ED33D3CA25F3805400C18DE9 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/sindresorhus/LaunchAtLogin";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -463,6 +497,11 @@
package = ED18259325C40ACC00FCD100 /* XCRemoteSwiftPackageReference "Then" */;
productName = Then;
};
ED33D3CB25F3805400C18DE9 /* LaunchAtLogin */ = {
isa = XCSwiftPackageProductDependency;
package = ED33D3CA25F3805400C18DE9 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */;
productName = LaunchAtLogin;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = ED18257325C4096400FCD100 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "LaunchAtLogin",
"repositoryURL": "https://github.com/sindresorhus/LaunchAtLogin",
"state": {
"branch": null,
"revision": "0f39982b9d6993eef253b81219d3c39ba1e680f3",
"version": "4.0.0"
}
},
{
"package": "SwiftSoup",
"repositoryURL": "https://github.com/scinfu/SwiftSoup",
Expand Down

0 comments on commit e346998

Please sign in to comment.