Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

refactor: use sindresorhus/defaults to simplify the code base #8

Merged
merged 4 commits into from
May 1, 2022
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
41 changes: 33 additions & 8 deletions PaimonMenuBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
76CCDDE027EAD1C4009CFC64 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76CCDDDF27EAD1C4009CFC64 /* SettingsView.swift */; };
76CCDDE227EAD1C5009CFC64 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 76CCDDE127EAD1C5009CFC64 /* Assets.xcassets */; };
76CCDDE527EAD1C5009CFC64 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 76CCDDE427EAD1C5009CFC64 /* Preview Assets.xcassets */; };
76D73BBF27EC650500CCDEA6 /* GameRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76D73BBE27EC650500CCDEA6 /* GameRecord.swift */; };
76D73BBF27EC650500CCDEA6 /* DataModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76D73BBE27EC650500CCDEA6 /* DataModels.swift */; };
76D73BC127EC67D300CCDEA6 /* Networking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76D73BC027EC67D300CCDEA6 /* Networking.swift */; };
76E429A927EDDE000032313C /* GameRecordViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E429A827EDDE000032313C /* GameRecordViewModel.swift */; };
76E429A927EDDE000032313C /* GameRecordUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E429A827EDDE000032313C /* GameRecordUpdater.swift */; };
76E986B627EDD5FC004ECC6C /* MenuExtrasView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E986B527EDD5FC004ECC6C /* MenuExtrasView.swift */; };
76F9AE6D27F570D90051CDC8 /* UpdaterViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76F9AE6C27F570D90051CDC8 /* UpdaterViewModel.swift */; };
9F38F96F281D1BE90004D240 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F38F96E281D1BE90004D240 /* Defaults.swift */; };
9F38F972281D4D000004D240 /* Defaults in Frameworks */ = {isa = PBXBuildFile; productRef = 9F38F971281D4D000004D240 /* Defaults */; };
9F38F974281D4D120004D240 /* Defaults+Workaround.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F38F973281D4D120004D240 /* Defaults+Workaround.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -34,19 +37,22 @@
76CCDDE127EAD1C5009CFC64 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
76CCDDE427EAD1C5009CFC64 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
76CCDDE627EAD1C5009CFC64 /* PaimonMenuBar.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PaimonMenuBar.entitlements; sourceTree = "<group>"; };
76D73BBE27EC650500CCDEA6 /* GameRecord.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameRecord.swift; sourceTree = "<group>"; };
76D73BBE27EC650500CCDEA6 /* DataModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataModels.swift; sourceTree = "<group>"; };
76D73BC027EC67D300CCDEA6 /* Networking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Networking.swift; sourceTree = "<group>"; };
76E429A827EDDE000032313C /* GameRecordViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameRecordViewModel.swift; sourceTree = "<group>"; };
76E429A827EDDE000032313C /* GameRecordUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameRecordUpdater.swift; sourceTree = "<group>"; };
76E986B527EDD5FC004ECC6C /* MenuExtrasView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuExtrasView.swift; sourceTree = "<group>"; };
76F9AE6B27F570640051CDC8 /* PaimonMenuBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PaimonMenuBar.app; sourceTree = BUILT_PRODUCTS_DIR; };
76F9AE6C27F570D90051CDC8 /* UpdaterViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdaterViewModel.swift; sourceTree = "<group>"; };
9F38F96E281D1BE90004D240 /* Defaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Defaults.swift; sourceTree = "<group>"; };
9F38F973281D4D120004D240 /* Defaults+Workaround.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Defaults+Workaround.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
76CCDDD727EAD1C4009CFC64 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9F38F972281D4D000004D240 /* Defaults in Frameworks */,
76085E6127FC23B000960915 /* LaunchAtLogin in Frameworks */,
76085E6427FC23EA00960915 /* Sparkle in Frameworks */,
);
Expand All @@ -70,16 +76,18 @@
7621675527F2FC080023F8B2 /* Localizable.strings */,
76CCDDDD27EAD1C4009CFC64 /* PaimonMenuBarApp.swift */,
76E986B527EDD5FC004ECC6C /* MenuExtrasView.swift */,
76E429A827EDDE000032313C /* GameRecordViewModel.swift */,
76E429A827EDDE000032313C /* GameRecordUpdater.swift */,
76CCDDDF27EAD1C4009CFC64 /* SettingsView.swift */,
76F9AE6C27F570D90051CDC8 /* UpdaterViewModel.swift */,
9F38F96E281D1BE90004D240 /* Defaults.swift */,
7686474027EF082400BCC350 /* Bundle.swift */,
76CCDDE127EAD1C5009CFC64 /* Assets.xcassets */,
76CCDDE627EAD1C5009CFC64 /* PaimonMenuBar.entitlements */,
76CCDDE327EAD1C5009CFC64 /* Preview Content */,
76C290EF27EAFFB000A30C9F /* AppDelegate.swift */,
76D73BBE27EC650500CCDEA6 /* GameRecord.swift */,
76D73BBE27EC650500CCDEA6 /* DataModels.swift */,
76D73BC027EC67D300CCDEA6 /* Networking.swift */,
9F38F973281D4D120004D240 /* Defaults+Workaround.swift */,
);
path = PaimonMenuBar;
sourceTree = "<group>";
Expand Down Expand Up @@ -112,6 +120,7 @@
packageProductDependencies = (
76085E6027FC23B000960915 /* LaunchAtLogin */,
76085E6327FC23EA00960915 /* Sparkle */,
9F38F971281D4D000004D240 /* Defaults */,
);
productName = PaimonMenuBar;
productReference = 76F9AE6B27F570640051CDC8 /* PaimonMenuBar.app */;
Expand Down Expand Up @@ -145,6 +154,7 @@
packageReferences = (
76085E5F27FC23B000960915 /* XCRemoteSwiftPackageReference "LaunchAtLogin" */,
76085E6227FC23EA00960915 /* XCRemoteSwiftPackageReference "Sparkle" */,
9F38F970281D4D000004D240 /* XCRemoteSwiftPackageReference "Defaults" */,
);
productRefGroup = 76CCDDD127EAD1C4009CFC64;
projectDirPath = "";
Expand Down Expand Up @@ -195,11 +205,13 @@
files = (
76E986B627EDD5FC004ECC6C /* MenuExtrasView.swift in Sources */,
7686474127EF082400BCC350 /* Bundle.swift in Sources */,
76E429A927EDDE000032313C /* GameRecordViewModel.swift in Sources */,
76D73BBF27EC650500CCDEA6 /* GameRecord.swift in Sources */,
76E429A927EDDE000032313C /* GameRecordUpdater.swift in Sources */,
9F38F974281D4D120004D240 /* Defaults+Workaround.swift in Sources */,
76D73BBF27EC650500CCDEA6 /* DataModels.swift in Sources */,
76C290F027EAFFB000A30C9F /* AppDelegate.swift in Sources */,
76D73BC127EC67D300CCDEA6 /* Networking.swift in Sources */,
76F9AE6D27F570D90051CDC8 /* UpdaterViewModel.swift in Sources */,
9F38F96F281D1BE90004D240 /* Defaults.swift in Sources */,
76CCDDE027EAD1C4009CFC64 /* SettingsView.swift in Sources */,
76CCDDDE27EAD1C4009CFC64 /* PaimonMenuBarApp.swift in Sources */,
);
Expand Down Expand Up @@ -437,6 +449,14 @@
kind = branch;
};
};
9F38F970281D4D000004D240 /* XCRemoteSwiftPackageReference "Defaults" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/sindresorhus/Defaults";
requirement = {
branch = main;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -450,6 +470,11 @@
package = 76085E6227FC23EA00960915 /* XCRemoteSwiftPackageReference "Sparkle" */;
productName = Sparkle;
};
9F38F971281D4D000004D240 /* Defaults */ = {
isa = XCSwiftPackageProductDependency;
package = 9F38F970281D4D000004D240 /* XCRemoteSwiftPackageReference "Defaults" */;
productName = Defaults;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 76CCDDD227EAD1C4009CFC64 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "defaults",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sindresorhus/Defaults",
"state" : {
"branch" : "main",
"revision" : "3535f3d088113cf24705014eec6a17f0fd73237f"
}
},
{
"identity" : "launchatlogin",
"kind" : "remoteSourceControl",
Expand Down
5 changes: 3 additions & 2 deletions PaimonMenuBar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import AppKit
import Defaults
import Foundation
import SwiftUI

Expand All @@ -24,7 +25,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
button.image?.size.width = 19
button.image?.size.height = 19

let gameRecord = GameRecordViewModel.shared.gameRecord
let gameRecord = Defaults[.lastGameRecord]
if gameRecord.retcode == nil {
button.title = "" // Cookie Not configured
} else {
Expand All @@ -51,7 +52,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

// Update game record on initial launch
print("App is started")
GameRecordViewModel.shared.tryUpdateGameRecord()
GameRecordUpdater.shared.tryFetchGameRecordAndRender()

// Close main APP window on initial launch
NSApp.setActivationPolicy(.accessory)
Expand Down
125 changes: 125 additions & 0 deletions PaimonMenuBar/DataModels.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// DataModels.swift
// PaimonMenuBar
//
// Created by Spencer Woo on 2022/3/24.
//

import Defaults
import Foundation

struct GameRecord: Codable, Defaults.Serializable {
/**
We specifically use nil to mark that this GameRecord is valid. The server will always present this field in the response.
*/
var retcode: Int?
var message: String

var data: GameData

static let empty = GameRecord(
retcode: nil, // Indicate that this is a mock record

message: "OK",
data: GameData(
current_resin: 0, max_resin: 160, resin_recovery_time: "0", finished_task_num: 0,
total_task_num: 4, is_extra_task_reward_received: false, remain_resin_discount_num: 0,
resin_discount_num_limit: 3, current_expedition_num: 1, max_expedition_num: 5,
expeditions: [Expeditions(status: "Finished", avatar_side_icon: "", remained_time: "0")],
current_home_coin: 0, max_home_coin: 2400, home_coin_recovery_time: "0", calendar_url: "",
transformer: Transformer(
obtained: true,
recovery_time: RecoveryTime(Day: 0, Hour: 0, Minute: 0, Second: 0, reached: false), wiki: ""
)
)
)
}

struct GameData: Codable, Defaults.Serializable {
var current_resin: Int
var max_resin: Int
var resin_recovery_time: String
var finished_task_num: Int
var total_task_num: Int
var is_extra_task_reward_received: Bool
var remain_resin_discount_num: Int
var resin_discount_num_limit: Int
var current_expedition_num: Int
var max_expedition_num: Int

var expeditions: [Expeditions]

var current_home_coin: Int
var max_home_coin: Int
var home_coin_recovery_time: String
var calendar_url: String

var transformer: Transformer
}

struct Expeditions: Codable, Hashable, Defaults.Serializable {
var status: String
var avatar_side_icon: String
var remained_time: String
}

struct Transformer: Codable, Defaults.Serializable {
var obtained: Bool
var recovery_time: RecoveryTime
var wiki: String
}

struct RecoveryTime: Codable, Defaults.Serializable {
var Day: Int
var Hour: Int
var Minute: Int
var Second: Int
var reached: Bool
}

enum GenshinServer: String, CaseIterable, Identifiable, Defaults.Serializable {
case cn_gf01 // 天空岛
case cn_qd01 // 世界树

case os_usa // Global (NA)
case os_euro // Global (EU)
case os_asia // Global (Asia)
case os_cht // Global (SAR)

var id: String { rawValue }

var serverName: String {
switch self {
case .cn_gf01:
return "天空岛"
case .cn_qd01:
return "世界树"
case .os_usa:
return "NA"
case .os_euro:
return "EU"
case .os_asia:
return "Asia"
case .os_cht:
return "SAR"
}
}

var isCNServer: Bool {
switch self {
case .cn_gf01, .cn_qd01:
return true
default:
return false
}
}

var cookieSiteUrl: String {
switch self {
case .cn_gf01, .cn_qd01:
return "https://bbs.mihoyo.com/ys"
case .os_usa, .os_euro, .os_asia, .os_cht:
return "https://www.hoyolab.com/home"
}
}
}
40 changes: 40 additions & 0 deletions PaimonMenuBar/Defaults+Workaround.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// See https://github.com/sindresorhus/Defaults/blob/main/workaround.md

import Defaults
import Foundation

public extension Defaults.Serializable where Self: Codable {
static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
}

public extension Defaults.Serializable where Self: Codable & NSSecureCoding {
static var bridge: Defaults.CodableNSSecureCodingBridge<Self> { Defaults.CodableNSSecureCodingBridge() }
}

public extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding {
static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
}

public extension Defaults.Serializable where Self: Codable & RawRepresentable {
static var bridge: Defaults.RawRepresentableCodableBridge<Self> { Defaults.RawRepresentableCodableBridge() }
}

public extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable {
static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
}

public extension Defaults.Serializable where Self: RawRepresentable {
static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
}

public extension Defaults.Serializable where Self: NSSecureCoding {
static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
}

public extension Defaults.CollectionSerializable where Element: Defaults.Serializable {
static var bridge: Defaults.CollectionBridge<Self> { Defaults.CollectionBridge() }
}

public extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable {
static var bridge: Defaults.SetAlgebraBridge<Self> { Defaults.SetAlgebraBridge() }
}
22 changes: 22 additions & 0 deletions PaimonMenuBar/Defaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Defaults.swift
// PaimonMenuBar
//
// Created by Breezewish on 2022/4/30.
//

import Defaults
import Foundation

extension Defaults.Keys {
static let uid = Key<String>("uid", default: "")

static let server = Key<GenshinServer>("server", default: .cn_gf01)

static let cookie = Key<String>("cookie", default: "")

// resin restores every 8 minutes
static let recordUpdateInterval = Key<Double>("update_interval", default: 60 * 8)

static let lastGameRecord = Key<GameRecord>("game_record", default: GameRecord.empty)
}
Loading