-
-
Notifications
You must be signed in to change notification settings - Fork 350
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 #28 from gao-sun/v1.3
v1.3
- Loading branch information
Showing
20 changed files
with
515 additions
and
122 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
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,46 @@ | ||
// | ||
// EulMenuComponent.swift | ||
// eul | ||
// | ||
// Created by Gao Sun on 2020/10/24. | ||
// Copyright © 2020 Gao Sun. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
enum EulMenuComponent: String, CaseIterable, Identifiable { | ||
var id: String { | ||
rawValue | ||
} | ||
|
||
var localizedDescription: String { | ||
"component.\(rawValue.lowercased())".localized() | ||
} | ||
|
||
func getView() -> AnyView { | ||
switch self { | ||
case .Battery: | ||
return AnyView(BatteryMenuBlockView()) | ||
case .CPU: | ||
return AnyView(CpuMenuBlockView()) | ||
case .Fan: | ||
return AnyView(FanMenuBlockView()) | ||
case .Memory: | ||
return AnyView(MemoryMenuBlockView()) | ||
case .Network: | ||
return AnyView(NetworkMenuBlockMenuView()) | ||
} | ||
} | ||
|
||
case CPU | ||
case Fan | ||
case Memory | ||
case Battery | ||
case Network | ||
|
||
static var allCases: [EulMenuComponent] { | ||
let components: [EulMenuComponent] = [.CPU, .Fan, .Memory, .Network] | ||
return BatteryStore.shared.isValid ? components + [.Battery] : components | ||
} | ||
} |
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,102 @@ | ||
// | ||
// ComponentsStore.swift | ||
// eul | ||
// | ||
// Created by Gao Sun on 2020/10/24. | ||
// Copyright © 2020 Gao Sun. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import SwiftyJSON | ||
|
||
class ComponentsStore<Component: CaseIterable & RawRepresentable & Equatable>: ObservableObject where Component.RawValue == String { | ||
@Published var showComponents = true | ||
@Published var isActiveComponentToggling = false | ||
@Published var activeComponents = Array(Component.allCases) | ||
@Published var availableComponents: [Component] = [] | ||
|
||
private let userDefaultsKey: String | ||
private var cancellable: AnyCancellable? | ||
|
||
var json: JSON { | ||
JSON([ | ||
"showComponents": showComponents, | ||
"activeComponents": activeComponents.map { $0.rawValue }, | ||
"availableComponents": availableComponents.map { $0.rawValue }, | ||
]) | ||
} | ||
|
||
init(key: String) { | ||
userDefaultsKey = key | ||
loadFromDefaults() | ||
cancellable = objectWillChange.sink { | ||
DispatchQueue.main.async { | ||
self.saveToDefaults() | ||
} | ||
} | ||
} | ||
|
||
func toggleActiveComponent(at index: Int) { | ||
isActiveComponentToggling = true | ||
availableComponents.append(activeComponents[index]) | ||
activeComponents.remove(at: index) | ||
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // wait for rendering, will crash w/o delay | ||
self.isActiveComponentToggling = false | ||
} | ||
} | ||
|
||
func toggleAvailableComponent(at index: Int) { | ||
activeComponents.append(availableComponents[index]) | ||
availableComponents.remove(at: index) | ||
} | ||
|
||
func loadFromDefaults() { | ||
if let raw = UserDefaults.standard.data(forKey: userDefaultsKey) { | ||
do { | ||
let data = try JSON(data: raw) | ||
|
||
print("⚙️ loaded data from user defaults", userDefaultsKey, data) | ||
|
||
if let value = data["showComponents"].bool { | ||
showComponents = value | ||
} | ||
|
||
if let array = data["activeComponents"].array { | ||
activeComponents = array.compactMap { | ||
if let string = $0.string { | ||
return Component(rawValue: string) | ||
} | ||
return nil | ||
} | ||
} | ||
if let array = data["availableComponents"].array { | ||
availableComponents = array.compactMap { | ||
if let string = $0.string { | ||
return Component(rawValue: string) | ||
} | ||
return nil | ||
} | ||
availableComponents += Array(Component.allCases).filter { | ||
!activeComponents.contains($0) && !availableComponents.contains($0) | ||
} | ||
} | ||
} catch { | ||
print("Unable to get preference data from user defaults") | ||
} | ||
} | ||
} | ||
|
||
func saveToDefaults() { | ||
do { | ||
let data = try json.rawData() | ||
UserDefaults.standard.set(data, forKey: userDefaultsKey) | ||
} catch { | ||
print("Unable to get preference data") | ||
} | ||
} | ||
} | ||
|
||
let sharedComponentsStore = ComponentsStore<EulComponent>(key: "components") | ||
let sharedMenuComponentsStore = ComponentsStore<EulMenuComponent>(key: "menuComponents") |
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
Oops, something went wrong.