Adopt PreferencesModel for implementing light/dark mode switch (in iOS)

This commit is contained in:
Angelo Stavrow 2020-08-11 14:32:45 -04:00
parent fceadda878
commit b787e34012
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
5 changed files with 21 additions and 11 deletions

View File

@ -2,6 +2,7 @@ import SwiftUI
struct ContentView: View {
@ObservedObject var postStore: PostStore
@ObservedObject var preferences: PreferencesModel
var body: some View {
NavigationView {
@ -13,11 +14,12 @@ struct ContentView: View {
.foregroundColor(.secondary)
}
.environmentObject(postStore)
.environmentObject(preferences)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(postStore: testPostStore)
ContentView(postStore: testPostStore, preferences: PreferencesModel())
}
}

View File

@ -1,18 +1,18 @@
import SwiftUI
struct PreferencesView: View {
@State private var appearance: Int = 0
@ObservedObject var preferences: PreferencesModel
var body: some View {
#if os(iOS)
Picker(selection: $appearance, label: Text("Appearance")) {
Picker(selection: $preferences.appearance, label: Text("Appearance")) {
Text("System").tag(0)
Text("Light Mode").tag(1)
Text("Dark Mode").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
#elseif os(macOS)
Picker(selection: $appearance, label: Text("Appearance")) {
Picker(selection: $preferences.appearance, label: Text("Appearance")) {
Text("System").tag(0)
Text("Light Mode").tag(1)
Text("Dark Mode").tag(2)
@ -23,6 +23,6 @@ struct PreferencesView: View {
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
PreferencesView()
PreferencesView(preferences: PreferencesModel())
}
}

View File

@ -2,6 +2,8 @@ import SwiftUI
@main
struct WriteFreely_MultiPlatformApp: App {
@StateObject private var preferences = PreferencesModel()
#if DEBUG
@StateObject private var store = testPostStore
#else
@ -10,12 +12,13 @@ struct WriteFreely_MultiPlatformApp: App {
var body: some Scene {
WindowGroup {
ContentView(postStore: store)
ContentView(postStore: store, preferences: preferences)
.environment(\.colorScheme, preferences.preferredColorScheme)
}
#if os(macOS)
Settings {
SettingsView()
SettingsView(preferences: preferences)
.frame(minWidth: 300, maxWidth: 300, minHeight: 200, maxHeight: 200)
.padding()
}

View File

@ -1,6 +1,8 @@
import SwiftUI
struct SettingsView: View {
@EnvironmentObject var preferences: PreferencesModel
@Binding var isPresented: Bool
var body: some View {
@ -11,10 +13,11 @@ struct SettingsView: View {
AccountView()
}
Section(header: Text("Appearance")) {
PreferencesView()
PreferencesView(preferences: preferences)
}
}
}
.preferredColorScheme(preferences.preferredColorScheme)
}
}

View File

@ -1,6 +1,8 @@
import SwiftUI
struct SettingsView: View {
@ObservedObject var preferences: PreferencesModel
@State var selectedView = 0
var body: some View {
@ -16,7 +18,7 @@ struct SettingsView: View {
}
.tag(0)
VStack {
PreferencesView()
PreferencesView(preferences: preferences)
Spacer()
}
.tabItem {
@ -30,12 +32,12 @@ struct SettingsView: View {
struct SettingsView_AccountTabPreviews: PreviewProvider {
static var previews: some View {
SettingsView(selectedView: 0)
SettingsView(preferences: PreferencesModel(), selectedView: 0)
}
}
struct SettingsView_PreferencesTabPreviews: PreviewProvider {
static var previews: some View {
SettingsView(selectedView: 1)
SettingsView(preferences: PreferencesModel(), selectedView: 1)
}
}