Resolves crash when switching between panes

This commit is contained in:
Stuart Breckenridge 2020-08-05 20:06:44 +08:00
parent 1d0c47a1bd
commit cd8c32055b
No known key found for this signature in database
GPG Key ID: 79BD673276AE83CE
7 changed files with 71 additions and 118 deletions

View File

@ -13,6 +13,7 @@ struct MainApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor(AppDelegate.self) private var delegate
@State private var selectedPane: MacPreferencePane = .general
#endif
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate
@ -20,7 +21,7 @@ struct MainApp: App {
@StateObject private var refreshProgress = RefreshProgressModel()
@StateObject private var defaults = AppDefaults.shared
@SceneBuilder var body: some Scene {
#if os(macOS)
WindowGroup {
@ -76,12 +77,41 @@ struct MainApp: App {
// Mac Preferences
Settings {
MacPreferencesView()
.padding()
TabView(selection: $selectedPane) {
GeneralPreferencesView()
.tabItem {
Image(systemName: "gearshape")
.font(.title2)
Text("General")
}
.tag(MacPreferencePane.general)
AccountsPreferencesView()
.tabItem {
Image(systemName: "at")
.font(.title2)
Text("Accounts")
}
.tag(MacPreferencePane.accounts)
LayoutPreferencesView()
.tabItem {
Image(systemName: "eyeglasses")
.font(.title2)
Text("Viewing")
}
.tag(MacPreferencePane.viewing)
AdvancedPreferencesView()
.tabItem {
Image(systemName: "scale.3d")
.font(.title2)
Text("Advanced")
}
.tag(MacPreferencePane.advanced)
}
.frame(width: 500)
.navigationTitle("Preferences")
.environmentObject(defaults)
.preferredColorScheme(AppDefaults.userInterfaceColorScheme)
.padding()
}
#endif

View File

@ -0,0 +1,28 @@
//
// MacPreferencesView.swift
// macOS
//
// Created by Stuart Breckenridge on 27/6/20.
//
import SwiftUI
enum MacPreferencePane: Int, CaseIterable {
case general = 1
case accounts = 2
case viewing = 3
case advanced = 4
var description: String {
switch self {
case .general:
return "General"
case .accounts:
return "Accounts"
case .viewing:
return "Appearance"
case .advanced:
return "Advanced"
}
}
}

View File

@ -1,105 +0,0 @@
//
// MacPreferencesView.swift
// macOS
//
// Created by Stuart Breckenridge on 27/6/20.
//
import SwiftUI
enum PreferencePane: Int, CaseIterable {
case general = 0
case accounts = 1
case viewing = 2
case advanced = 3
var description: String {
switch self {
case .general:
return "General"
case .accounts:
return "Accounts"
case .viewing:
return "Appearance"
case .advanced:
return "Advanced"
}
}
}
struct MacPreferencesView: View {
@EnvironmentObject var defaults: AppDefaults
@State private var selectedPane: PreferencePane = .general
var body: some View {
TabView(selection: $selectedPane) {
GeneralPreferencesView()
.environmentObject(defaults)
.tabItem {
VStack {
Image(systemName: "gearshape")
.font(.title2)
Text("General")
}
}
.onTapGesture {
selectedPane = .general
}
.tag(PreferencePane.general)
AccountsPreferencesView()
.environmentObject(defaults)
.tabItem {
VStack {
Image(systemName: "at")
.font(.title2)
Text("Accounts")
}
}
.onTapGesture {
selectedPane = .accounts
}
.tag(PreferencePane.accounts)
LayoutPreferencesView()
.environmentObject(defaults)
.tabItem {
VStack {
Image(systemName: "eyeglasses")
.font(.title2)
Text("Viewing")
}
}
.onTapGesture {
selectedPane = .viewing
}
.tag(PreferencePane.viewing)
AdvancedPreferencesView()
.environmentObject(defaults)
.tabItem {
VStack {
Image(systemName: "scale.3d")
.font(.title2)
Text("Advanced")
}
}
.onTapGesture {
selectedPane = .advanced
}
.tag(PreferencePane.advanced)
}
}
}
struct MacPreferencesView_Previews: PreviewProvider {
static var previews: some View {
MacPreferencesView()
}
}

View File

@ -9,7 +9,7 @@ import SwiftUI
struct AdvancedPreferencesView: View {
@EnvironmentObject private var preferences: AppDefaults
@StateObject private var preferences = AppDefaults.shared
@StateObject private var viewModel = AdvancedPreferencesModel()
var body: some View {

View File

@ -9,7 +9,7 @@ import SwiftUI
struct GeneralPreferencesView: View {
@EnvironmentObject private var defaults: AppDefaults
@StateObject private var defaults = AppDefaults.shared
@StateObject private var preferences = GeneralPreferencesModel()
var body: some View {

View File

@ -10,7 +10,7 @@ import SwiftUI
struct LayoutPreferencesView: View {
@EnvironmentObject var defaults: AppDefaults
@StateObject private var defaults = AppDefaults.shared
private let colorPalettes = UserInterfaceColorPalette.allCases
private let sampleTitle = "Lorem dolor sed viverra ipsum. Gravida rutrum quisque non tellus. Rutrum tellus pellentesque eu tincidunt tortor. Sed blandit libero volutpat sed cras ornare. Et netus et malesuada fames ac. Ultrices eros in cursus turpis massa tincidunt dui ut ornare. Lacus sed viverra tellus in. Sollicitudin ac orci phasellus egestas. Purus in mollis nunc sed. Sollicitudin ac orci phasellus egestas tellus rutrum tellus pellentesque. Interdum consectetur libero id faucibus nisl tincidunt eget."

View File

@ -19,7 +19,7 @@
1729529324AA1CAA00D65E66 /* AccountsPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529024AA1CAA00D65E66 /* AccountsPreferencesView.swift */; };
1729529424AA1CAA00D65E66 /* AdvancedPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529124AA1CAA00D65E66 /* AdvancedPreferencesView.swift */; };
1729529524AA1CAA00D65E66 /* GeneralPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529224AA1CAA00D65E66 /* GeneralPreferencesView.swift */; };
1729529724AA1CD000D65E66 /* MacPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529624AA1CD000D65E66 /* MacPreferencesView.swift */; };
1729529724AA1CD000D65E66 /* MacPreferencePanes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529624AA1CD000D65E66 /* MacPreferencePanes.swift */; };
1729529B24AA1FD200D65E66 /* MacSearchField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529A24AA1FD200D65E66 /* MacSearchField.swift */; };
175942AA24AD533200585066 /* RefreshInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5183CCE4226F4DFA0010922C /* RefreshInterval.swift */; };
175942AB24AD533200585066 /* RefreshInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5183CCE4226F4DFA0010922C /* RefreshInterval.swift */; };
@ -1395,7 +1395,7 @@
1729529024AA1CAA00D65E66 /* AccountsPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountsPreferencesView.swift; sourceTree = "<group>"; };
1729529124AA1CAA00D65E66 /* AdvancedPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdvancedPreferencesView.swift; sourceTree = "<group>"; };
1729529224AA1CAA00D65E66 /* GeneralPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralPreferencesView.swift; sourceTree = "<group>"; };
1729529624AA1CD000D65E66 /* MacPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MacPreferencesView.swift; sourceTree = "<group>"; };
1729529624AA1CD000D65E66 /* MacPreferencePanes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MacPreferencePanes.swift; sourceTree = "<group>"; };
1729529A24AA1FD200D65E66 /* MacSearchField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacSearchField.swift; sourceTree = "<group>"; };
1769E32124BC5925000E1E8E /* AccountsPreferencesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsPreferencesModel.swift; sourceTree = "<group>"; };
1769E32424BC5A65000E1E8E /* AddAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddAccountView.swift; sourceTree = "<group>"; };
@ -2100,7 +2100,7 @@
1729528F24AA1A4F00D65E66 /* Preferences */ = {
isa = PBXGroup;
children = (
1729529624AA1CD000D65E66 /* MacPreferencesView.swift */,
1729529624AA1CD000D65E66 /* MacPreferencePanes.swift */,
1729529924AA1CE100D65E66 /* Preference Panes */,
);
path = Preferences;
@ -4571,7 +4571,7 @@
51E4995B24A875D500B667CB /* ArticlePasteboardWriter.swift in Sources */,
51E4993424A867E700B667CB /* UserInfoKey.swift in Sources */,
1776E88F24AC5F8A00E78166 /* AppDefaults.swift in Sources */,
1729529724AA1CD000D65E66 /* MacPreferencesView.swift in Sources */,
1729529724AA1CD000D65E66 /* MacPreferencePanes.swift in Sources */,
51E4994C24A8734C00B667CB /* RedditFeedProvider-Extensions.swift in Sources */,
1729529324AA1CAA00D65E66 /* AccountsPreferencesView.swift in Sources */,
171BCB8D24CB08A3006E22D9 /* FixAccountCredentialView.swift in Sources */,