Skip to content

Commit

Permalink
add color scheme setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ski-u committed Dec 5, 2024
1 parent 4405e9c commit f85794a
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Features/Sources/AppFeature/AppView.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import ComposableArchitecture
import Models
import Settings
import SwiftUI
import Today

public struct AppView: View {
var store: StoreOf<AppReducer>

@Shared(.appStorage("colorScheme")) var userColorScheme = UserColorScheme.light

public init(store: StoreOf<AppReducer>) {
self.store = store
}
Expand All @@ -30,5 +33,6 @@ public struct AppView: View {
}
}
}
.preferredColorScheme(userColorScheme.colorScheme)
}
}
16 changes: 16 additions & 0 deletions Features/Sources/AppFeature/Views/UserColorScheme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Models
import SwiftUI

extension UserColorScheme {
var colorScheme: ColorScheme? {
switch self {
case .dark:
.dark
case .light:
.light
case .system:
nil
}
}
}

5 changes: 5 additions & 0 deletions Features/Sources/Models/UserColorScheme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public enum UserColorScheme: String, Sendable {
case dark = "dark"
case light = "light"
case system = "system"
}
70 changes: 70 additions & 0 deletions Features/Sources/Settings/Appearance/AppearanceView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import Models
import Sharing
import SwiftUI

struct AppearanceView: View {
@Shared(.appStorage("colorScheme")) var userColorScheme = UserColorScheme.system

var body: some View {
Form {
Button(
action: {
$userColorScheme.withLock {
$0 = .light
}
}
) {
HStack {
Text("Light")
.foregroundStyle(Color.primary)

Spacer()

if userColorScheme == .light {
Image(systemName: "checkmark")
}
}
}

Button(
action: {
$userColorScheme.withLock {
$0 = .dark
}
}
) {
HStack {
Text("Dark")
.foregroundStyle(Color.primary)

Spacer()

if userColorScheme == .dark {
Image(systemName: "checkmark")
}
}
}

Button(
action: {
$userColorScheme.withLock {
$0 = .system
}
}
) {
HStack {
Text("System")
.foregroundStyle(Color.primary)

Spacer()

if userColorScheme == .system {
Image(systemName: "checkmark")
}
}
}
}
.navigationTitle("Appearance")
.navigationBarTitleDisplayMode(.inline)
}
}
2 changes: 1 addition & 1 deletion Features/Sources/Settings/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct SettingsView: View {
}

NavigationLink {
Text("Appearance")
AppearanceView()
} label: {
NavigationLinkLabel(
color: .cyan,
Expand Down
6 changes: 0 additions & 6 deletions Features/Tests/AppFeatureTests/AppFeatureTests.swift

This file was deleted.

14 changes: 14 additions & 0 deletions Features/Tests/AppFeatureTests/UserColorSchemeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Models
import Testing

@testable import AppFeature

@MainActor
struct UserColorSchemeTests {
@Test
func colorScheme() {
#expect(UserColorScheme.dark.colorScheme == .dark)
#expect(UserColorScheme.light.colorScheme == .light)
#expect(UserColorScheme.system.colorScheme == nil)
}
}

0 comments on commit f85794a

Please sign in to comment.