Skip to content

Commit

Permalink
clean: リファクタ
Browse files Browse the repository at this point in the history
  • Loading branch information
SNQ-2001 committed Jan 10, 2023
1 parent 9d24546 commit 7284bb3
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 99 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.7
20 changes: 12 additions & 8 deletions GitHubContributionsMenuBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
1C078205296C3D0D000D42B4 /* ThemaColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C078204296C3D0D000D42B4 /* ThemaColor.swift */; };
1C078207296C6943000D42B4 /* ViewType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C078206296C6943000D42B4 /* ViewType.swift */; };
1C078209296C6C0C000D42B4 /* Contributions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C078208296C6C0C000D42B4 /* Contributions.swift */; };
1C1787502909248700BFEB6E /* Settings.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 1C17874F2909248700BFEB6E /* Settings.bundle */; };
1C1787542909291900BFEB6E /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C1787532909291900BFEB6E /* Assets.swift */; };
1C1AF58E2915253B00576761 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1C17875529092A3800BFEB6E /* Assets.xcassets */; };
Expand All @@ -19,7 +21,6 @@
1C6694AB2822EB6600ABFED2 /* Color+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694AA2822EB6600ABFED2 /* Color+.swift */; };
1C6694AD2822EB9400ABFED2 /* ContributionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694AC2822EB9400ABFED2 /* ContributionsView.swift */; };
1C6694AF2822EBC300ABFED2 /* GridStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694AE2822EBC300ABFED2 /* GridStack.swift */; };
1C6694B12822EBD800ABFED2 /* UXView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694B02822EBD800ABFED2 /* UXView.swift */; };
1C6694B52822EC2700ABFED2 /* View+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694B42822EC2700ABFED2 /* View+.swift */; };
1C6694B82822EC8F00ABFED2 /* Contribution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694B72822EC8F00ABFED2 /* Contribution.swift */; };
1C6694BA2822ECA400ABFED2 /* GitHub.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C6694B92822ECA400ABFED2 /* GitHub.swift */; };
Expand All @@ -33,6 +34,8 @@

/* Begin PBXFileReference section */
1C078204296C3D0D000D42B4 /* ThemaColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemaColor.swift; sourceTree = "<group>"; };
1C078206296C6943000D42B4 /* ViewType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewType.swift; sourceTree = "<group>"; };
1C078208296C6C0C000D42B4 /* Contributions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contributions.swift; sourceTree = "<group>"; };
1C17874F2909248700BFEB6E /* Settings.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Settings.bundle; sourceTree = "<group>"; };
1C1787532909291900BFEB6E /* Assets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assets.swift; sourceTree = "<group>"; };
1C17875529092A3800BFEB6E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -47,7 +50,6 @@
1C6694AA2822EB6600ABFED2 /* Color+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+.swift"; sourceTree = "<group>"; };
1C6694AC2822EB9400ABFED2 /* ContributionsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContributionsView.swift; sourceTree = "<group>"; };
1C6694AE2822EBC300ABFED2 /* GridStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GridStack.swift; sourceTree = "<group>"; };
1C6694B02822EBD800ABFED2 /* UXView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UXView.swift; sourceTree = "<group>"; };
1C6694B42822EC2700ABFED2 /* View+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+.swift"; sourceTree = "<group>"; };
1C6694B72822EC8F00ABFED2 /* Contribution.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contribution.swift; sourceTree = "<group>"; };
1C6694B92822ECA400ABFED2 /* GitHub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHub.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -83,6 +85,8 @@
isa = PBXGroup;
children = (
1C078204296C3D0D000D42B4 /* ThemaColor.swift */,
1C078206296C6943000D42B4 /* ViewType.swift */,
1C078208296C6C0C000D42B4 /* Contributions.swift */,
);
path = Entity;
sourceTree = "<group>";
Expand Down Expand Up @@ -150,7 +154,6 @@
isa = PBXGroup;
children = (
1C6694A22822EAD900ABFED2 /* ContentView.swift */,
1C6694B02822EBD800ABFED2 /* UXView.swift */,
1CAFD6942824193200C9A576 /* Contributions */,
1CAFD689282411D700C9A576 /* Settings */,
);
Expand Down Expand Up @@ -319,16 +322,17 @@
1C6694AD2822EB9400ABFED2 /* ContributionsView.swift in Sources */,
1CAFD68B282412F500C9A576 /* SettingsLabelStyle.swift in Sources */,
1C1787542909291900BFEB6E /* Assets.swift in Sources */,
1C6694B12822EBD800ABFED2 /* UXView.swift in Sources */,
1C6694A62822EAFB00ABFED2 /* ContributionsViewModel.swift in Sources */,
1CAFD6862823D81100C9A576 /* SettingsView.swift in Sources */,
1C6694BA2822ECA400ABFED2 /* GitHub.swift in Sources */,
1C6694922822EA3800ABFED2 /* GitHubContributionsMenuBarApp.swift in Sources */,
1C6694AF2822EBC300ABFED2 /* GridStack.swift in Sources */,
1C6694B82822EC8F00ABFED2 /* Contribution.swift in Sources */,
1C078209296C6C0C000D42B4 /* Contributions.swift in Sources */,
1C078205296C3D0D000D42B4 /* ThemaColor.swift in Sources */,
1C6694B52822EC2700ABFED2 /* View+.swift in Sources */,
1C6694AB2822EB6600ABFED2 /* Color+.swift in Sources */,
1C078207296C6943000D42B4 /* ViewType.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -460,7 +464,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 111;
CURRENT_PROJECT_VERSION = 112;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"GitHubContributionsMenuBar/Preview Content\"";
DEVELOPMENT_TEAM = 69BMRJXU5B;
Expand All @@ -477,7 +481,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.1.1;
MARKETING_VERSION = 1.2.0;
PRODUCT_BUNDLE_IDENTIFIER = com.GitHubContributionsMenuBar;
PRODUCT_NAME = Contributions;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -495,7 +499,7 @@
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 111;
CURRENT_PROJECT_VERSION = 112;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"GitHubContributionsMenuBar/Preview Content\"";
DEVELOPMENT_TEAM = 69BMRJXU5B;
Expand All @@ -512,7 +516,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.1.1;
MARKETING_VERSION = 1.2.0;
PRODUCT_BUNDLE_IDENTIFIER = com.GitHubContributionsMenuBar;
PRODUCT_NAME = Contributions;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>GitHubContributionsMenuBar.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>0</integer>
</dict>
</dict>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion GitHubContributionsMenuBar/Generated/Assets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ internal struct ImageAsset {
#if os(iOS) || os(tvOS)
let image = Image(named: name, in: bundle, compatibleWith: nil)
#elseif os(macOS)
let name = NSImage.Name(self.name)
let name = NSImage.Name(name)
let image = (bundle == .main) ? NSImage(named: name) : bundle.image(forResource: name)
#elseif os(watchOS)
let image = Image(named: name)
Expand Down
13 changes: 13 additions & 0 deletions GitHubContributionsMenuBar/Model/Entity/Contributions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Contributions.swift
// GitHubContributionsMenuBar
//
// Created by 宮本大新 on 2023/01/10.
//

import Foundation

struct Contributions {
var levels: [[GitHub.Contribution.Level]] = []
var count: Int = .zero
}
32 changes: 32 additions & 0 deletions GitHubContributionsMenuBar/Model/Entity/ViewType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ViewType.swift
// GitHubContributionsMenuBar
//
// Created by 宮本大新 on 2023/01/10.
//

import Foundation

enum ViewType {
case contributions
case settings
case emptyUserName
case error(Error)
case progress
}

extension ViewType {
static func == (lhs: ViewType, rhs: ViewType) -> Bool {
switch (lhs, rhs) {
case (.contributions, .contributions),
(.settings, .settings),
(.emptyUserName, .emptyUserName),
(.progress, .progress):
return true
case let (.error(lhsError), .error(rhsError)):
return lhsError.localizedDescription == rhsError.localizedDescription
default:
return false
}
}
}
1 change: 0 additions & 1 deletion GitHubContributionsMenuBar/Model/Network/GitHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public enum GitHub {
let contributions = try document.select("rect").compactMap(contribution)
promise(.success(contributions))
} catch {
print("🟥")
promise(.failure(error))
}
}
Expand Down
88 changes: 64 additions & 24 deletions GitHubContributionsMenuBar/View/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,58 @@ import SwiftUI

struct ContentView: View {
@ObservedObject var viewModel: ContributionsViewModel
@Environment(\.colorScheme) var colorScheme
var body: some View {
VStack(spacing: 8) {
toolBar

if viewModel.viewMode {
settings
} else {
contributions
switch viewModel.viewType {
case .contributions:
toolBar()
contributionsView()
case .settings:
toolBar()
settingsView()
case .emptyUserName:
emptyUserNameLabel()
case let .error(error):
errorLabel(error: error)
case .progress:
progressView()
}
}
.frame(width: 265, height: 115)
.padding(.all, 12)
.onChange(of: viewModel.viewMode) { _ in
viewModel.updateContributions()
}
}
}

extension ContentView {
private var contributions: some View {
private func contributionsView() -> some View {
ContributionsView(viewModel: viewModel)
}

private var settings: some View {
private func settingsView() -> some View {
SettingsView(viewModel: viewModel)
}

private var toolBar: some View {
private func toolBar() -> some View {
HStack(spacing: 6) {
switchingButton
switchingButton()

Text(viewModel.username)
.frame(height: 12)

Spacer()

if viewModel.viewMode {
quitButton
} else {
contributionsCountLabel
switch viewModel.viewType {
case .contributions:
contributionsCountLabel()
case .settings, .emptyUserName, .error, .progress:
quitButton()
}
}
.captionStyle()
}

private var switchingButton: some View {
Image(systemName: viewModel.viewMode ? "arrowshape.turn.up.backward.circle" : "gearshape")
private func switchingButton() -> some View {
Image(systemName: viewModel.viewType == .settings ? "arrowshape.turn.up.backward.circle" : "gearshape")
.resizable()
.frame(width: 10, height: 10)
.unredacted()
Expand All @@ -70,13 +74,21 @@ extension ContentView {
viewModel.hoverSwitchingButton = hovering
}
.onTapGesture {
withAnimation {
viewModel.viewMode.toggle()
switch viewModel.viewType {
case .contributions, .emptyUserName:
withAnimation { viewModel.viewType = .settings }
case .settings:
if !viewModel.username.isEmpty {
withAnimation { viewModel.viewType = .contributions }
viewModel.updateContributions()
}
case .progress, .error:
return
}
}
}

private var quitButton: some View {
private func quitButton() -> some View {
Text("Quit")
.frame(height: 10)
.background(
Expand All @@ -94,9 +106,37 @@ extension ContentView {
.padding(.trailing, 3)
}

private var contributionsCountLabel: some View {
private func contributionsCountLabel() -> some View {
let count = viewModel.contributions.count
return Text("\(count <= 1 ? "\(count) contribution" : "\(count) contributions")")
.frame(height: 12)
}

private func errorLabel(error: Error) -> some View {
Text("\(Image(systemName: "exclamationmark.triangle")) \(error.localizedDescription)")
.fontWeight(.medium)
.foregroundColor(.blue)
.frame(maxHeight: .infinity)
.onTapGesture {
viewModel.updateContributions()
}
}

private func emptyUserNameLabel() -> some View {
Text("\(Image(systemName: "info.circle")) Set a GitHub username")
.fontWeight(.medium)
.foregroundColor(.blue)
.frame(maxHeight: .infinity)
.onTapGesture {
withAnimation { viewModel.viewType = .settings }
}
}

private func progressView() -> some View {
ProgressView()
.progressViewStyle(.circular)
.scaleEffect(1.3)
.frame(width: 1.3 * 20, height: 1.3 * 20)
.frame(maxHeight: .infinity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@

import SwiftUI

public struct ContributionsView: View {
struct ContributionsView: View {
@ObservedObject var viewModel: ContributionsViewModel
public var body: some View {
if color().isEmpty {
UXView(viewModel: viewModel)
} else {
GridStack(rows: 7, columns: 20, spacing: 3.0) { row, column in
if let color = color().element(at: row)?.element(at: column) {
color.tileStyle()
} else {
Color.clear
}
var body: some View {
GridStack(rows: 7, columns: 20, spacing: 3.0) { row, column in
if let color = color().element(at: row)?.element(at: column) {
color.tileStyle()
} else {
Color.clear
}
}
}
Expand Down
42 changes: 0 additions & 42 deletions GitHubContributionsMenuBar/View/UXView.swift

This file was deleted.

Loading

0 comments on commit 7284bb3

Please sign in to comment.