Skip to content

Commit

Permalink
Introduce SwiftFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Sep 13, 2020
1 parent 5f3861f commit 26ef122
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 209 deletions.
3 changes: 3 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--swiftversion 5
--exclude Carthage
--nospaceoperators ..<, ...
21 changes: 21 additions & 0 deletions eul.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
6C7DB6E324E80F9400133B06 /* PreferenceDisplayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferenceDisplayView.swift; sourceTree = "<group>"; };
6C7DB6E524E82C5A00133B06 /* Preference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preference.swift; sourceTree = "<group>"; };
6C7DB6E724E82C7700133B06 /* StringEnum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringEnum.swift; sourceTree = "<group>"; };
6C9667D9250E3D6F00E51ABB /* .swiftformat */ = {isa = PBXFileReference; lastKnownFileType = text; path = .swiftformat; sourceTree = "<group>"; };
6CAEED5324A5DE4700C39597 /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
6CAEED6124A5F51600C39597 /* Text.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Text.swift; sourceTree = "<group>"; };
6CAEED6424A62DF800C39597 /* SMC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SMC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -208,6 +209,7 @@
6C5F853D249FACE000CF0EF8 /* eul */,
6C5F853C249FACE000CF0EF8 /* Products */,
6C4E880824A649BC00766E15 /* Cartfile */,
6C9667D9250E3D6F00E51ABB /* .swiftformat */,
6C4E880A24A6545E00766E15 /* Frameworks */,
);
sourceTree = "<group>";
Expand Down Expand Up @@ -339,6 +341,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 6C5F854E249FACE200CF0EF8 /* Build configuration list for PBXNativeTarget "eul" */;
buildPhases = (
6C9667DA250E3DA200E51ABB /* SwiftFormat */,
6C5F8537249FACE000CF0EF8 /* Sources */,
6C5F8538249FACE000CF0EF8 /* Frameworks */,
6C5F8539249FACE000CF0EF8 /* Resources */,
Expand Down Expand Up @@ -404,6 +407,24 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
6C9667DA250E3DA200E51ABB /* SwiftFormat */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = SwiftFormat;
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftformat >/dev/null; then\n swiftformat .\nelse\n echo \"warning: SwiftFormat not installed, download from https://github.com/nicklockwood/SwiftFormat\"\nfi\n";
};
6CC0798D250CEE96000D7DAC /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
12 changes: 6 additions & 6 deletions eul/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

import Cocoa
import SwiftUI
import Localize_Swift
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -26,12 +26,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
func applicationDidFinishLaunching(_: Notification) {
let contentView = ContentView()
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
backing: .buffered, defer: false
)
window.center()
window.setFrameAutosaveName("Eul Preferences")
window.contentView = NSHostingView(rootView: contentView)
Expand All @@ -40,10 +41,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
window.makeKeyAndOrderFront(nil)
SmcControl.shared.start()
self.refreshRepeatedly()
refreshRepeatedly()
}

func applicationWillTerminate(_ aNotification: Notification) {
func applicationWillTerminate(_: Notification) {
// Insert code here to tear down your application
}

Expand All @@ -56,4 +57,3 @@ class AppDelegate: NSObject, NSApplicationDelegate {
NSApplication.shared.terminate(self)
}
}

6 changes: 3 additions & 3 deletions eul/Extension/Animation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import SwiftUI

extension Animation {
static let normal: Animation = Animation.spring(dampingFraction: 1.5).speed(2.5)
static let fast: Animation = Animation.spring(response: 0.3, dampingFraction: 1).speed(2)
static let slow: Animation = Animation.spring(dampingFraction: 1.5).speed(1)
static let normal = Animation.spring(dampingFraction: 1.5).speed(2.5)
static let fast = Animation.spring(response: 0.3, dampingFraction: 1).speed(2)
static let slow = Animation.spring(dampingFraction: 1.5).speed(1)
}
2 changes: 1 addition & 1 deletion eul/Extension/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Color {
self.init(red: Double(r) / 255, green: Double(g) / 255, blue: Double(b) / 255, opacity: Double(a) / 255)
}

static private var isDark: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua }
private static var isDark: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua }
static let border = Color(NSColor.controlColor)
static let controlBackground = Color(NSColor.controlBackgroundColor)
static let text = Color(NSColor.textColor)
Expand Down
18 changes: 6 additions & 12 deletions eul/Extension/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,30 @@ import SwiftUI

extension Text {
func compact() -> some View {
self
.font(.system(size: 8, weight: .regular))
font(.system(size: 8, weight: .regular))
}

func normal() -> some View {
self
.font(.system(size: 12, weight: .regular))
font(.system(size: 12, weight: .regular))
}

func section() -> some View {
self
.font(.headline)
font(.headline)
.padding(.top, 8)
.padding(.bottom, 4)
}

func subsection() -> some View {
self
.font(.system(size: 12, weight: .regular))
font(.system(size: 12, weight: .regular))
.padding(.top, 8)
}

func inlineSection() -> some View {
self
.font(.system(size: 12, weight: .regular))
font(.system(size: 12, weight: .regular))
}

func menuSection() -> some View {
self
.font(.system(size: 11, weight: .semibold))
font(.system(size: 11, weight: .semibold))
.padding(.top, 8)
.padding(.bottom, 4)
.frame(maxWidth: .infinity, alignment: .leading)
Expand Down
3 changes: 1 addition & 2 deletions eul/Extension/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import SwiftUI

extension View {
func menuInfo() -> some View {
self
.font(.system(size: 14, weight: .regular))
font(.system(size: 14, weight: .regular))
.foregroundColor(.info)
.padding(.leading, 20)
.padding(.trailing, 12)
Expand Down
1 change: 0 additions & 1 deletion eul/Schema/Preference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation


struct Preference {
enum TextDisplay: String, StringEnum {
case compact
Expand Down
2 changes: 1 addition & 1 deletion eul/Schema/Refreshable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

@objc protocol RefreshableObjC: class {
@objc protocol RefreshableObjC: AnyObject {
func refresh()
}

Expand Down
2 changes: 1 addition & 1 deletion eul/StatusBar/StatusBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2020 Gao Sun. All rights reserved.
//

import SwiftUI
import Combine
import SwiftUI

class StatusBarManager {
@ObservedObject var preferenceStore = PreferenceStore.shared
Expand Down
1 change: 1 addition & 0 deletions eul/Store/MemoryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MemoryStore: ObservableObject, Refreshable {
var freeString: String {
MemoryStore.memoryUnit(total - used)
}

var usedString: String {
MemoryStore.memoryUnit(used)
}
Expand Down
12 changes: 7 additions & 5 deletions eul/Store/PreferenceStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2020 Gao Sun. All rights reserved.
//

import Foundation
import Combine
import Foundation
import Localize_Swift
import SwiftyJSON

Expand Down Expand Up @@ -38,11 +38,13 @@ class PreferenceStore: ObservableObject {
SmcControl.shared.tempUnit = newValue
}
}

@Published var language = Localize.currentLanguage() {
willSet {
Localize.setCurrentLanguage(newValue)
}
}

@Published var textDisplay = Preference.TextDisplay.compact
@Published var isActiveComponentToggling = false
@Published var activeComponents = EulComponent.allCases
Expand All @@ -56,7 +58,7 @@ class PreferenceStore: ObservableObject {
"language": language,
"textDisplay": textDisplay.rawValue,
"activeComponents": activeComponents.map { $0.rawValue },
"availableComponents": availableComponents.map { $0.rawValue }
"availableComponents": availableComponents.map { $0.rawValue },
])
}

Expand Down Expand Up @@ -92,7 +94,7 @@ class PreferenceStore: ObservableObject {
let url = URL(string: "https://api.github.com/repos/\(repo)/releases/latest")

if let url = url {
let task = session.dataTask(with: url) { data, response, error in
let task = session.dataTask(with: url) { data, _, error in
DispatchQueue.main.async {
if
error == nil,
Expand All @@ -108,8 +110,8 @@ class PreferenceStore: ObservableObject {
}
task.resume()
} else {
self.isUpdateAvailable = false
self.checkUpdateFailed = true
isUpdateAvailable = false
checkUpdateFailed = true
}
}

Expand Down
24 changes: 12 additions & 12 deletions eul/Utilities/ByteUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public struct ByteUnit {
public let bytes: UInt64

public var kilobytes: Double {
Double(bytes) / 1_024
Double(bytes) / 1024
}

public var megabytes: Double {
kilobytes / 1_024
kilobytes / 1024
}

public var gigabytes: Double {
megabytes / 1_024
megabytes / 1024
}

public init(_ bytes: UInt64) {
Expand All @@ -34,16 +34,16 @@ public struct ByteUnit {

public var readable: String {
switch bytes {
case 0..<1_024:
return "\(bytes) bytes"
case 1_024..<(1_024 * 1_024):
return "\(String(format: "%.2f", kilobytes)) kb"
case 1_024..<(1_024 * 1_024 * 1_024):
return "\(String(format: "%.2f", megabytes)) mb"
case (1_024 * 1_024 * 1_024)...UInt64.max:
return "\(String(format: "%.2f", gigabytes)) gb"
case 0..<1024:
return "\(bytes) bytes"
case 1024..<(1024 * 1024):
return "\(String(format: "%.2f", kilobytes)) kb"
case 1024..<(1024 * 1024 * 1024):
return "\(String(format: "%.2f", megabytes)) mb"
case (1024 * 1024 * 1024)...UInt64.max:
return "\(String(format: "%.2f", gigabytes)) gb"
default:
return "\(bytes) bytes"
return "\(bytes) bytes"
}
}
}
3 changes: 2 additions & 1 deletion eul/Utilities/Info.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

import Foundation
import SystemKit
import IOKit.ps
import SystemKit

struct Info {
enum BatteryCondition: String {
Expand Down Expand Up @@ -37,6 +37,7 @@ struct Info {
var currentPercentage: Int {
Int(Double(currentCapacity) / Double(maxCapacity) * 100)
}

var condition: BatteryCondition = .good
var powerSource: PowerSourceState = .unknown
var timeToFullCharge = 0
Expand Down
Loading

0 comments on commit 26ef122

Please sign in to comment.