Skip to content

Commit

Permalink
Cleaning up user settings in content view of example
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaal111 committed Mar 2, 2024
1 parent ed4d404 commit 85b91e3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 56 deletions.
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
2CD2E50B29FAD02500DEAA95 /* OtherScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CD2E50A29FAD02500DEAA95 /* OtherScreen.swift */; };
2CD2E50D29FAD06B00DEAA95 /* Sidebar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CD2E50C29FAD06B00DEAA95 /* Sidebar.swift */; };
2CD2E50F29FAD19800DEAA95 /* CoreDataScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CD2E50E29FAD19800DEAA95 /* CoreDataScreen.swift */; };
2CD77D062B935480009D455D /* UserSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CD77D052B935480009D455D /* UserSettings.swift */; };
2CEFF99429F41E050042908B /* KamaalBrowser in Frameworks */ = {isa = PBXBuildFile; productRef = 2CEFF99329F41E050042908B /* KamaalBrowser */; };
2CEFF99629F425630042908B /* KamaalPopUp in Frameworks */ = {isa = PBXBuildFile; productRef = 2CEFF99529F425630042908B /* KamaalPopUp */; };
2CEFF99829F42E950042908B /* KamaalNetworker in Frameworks */ = {isa = PBXBuildFile; productRef = 2CEFF99729F42E950042908B /* KamaalNetworker */; };
Expand All @@ -44,6 +45,7 @@
2CD2E50A29FAD02500DEAA95 /* OtherScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OtherScreen.swift; sourceTree = "<group>"; };
2CD2E50C29FAD06B00DEAA95 /* Sidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sidebar.swift; sourceTree = "<group>"; };
2CD2E50E29FAD19800DEAA95 /* CoreDataScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreDataScreen.swift; sourceTree = "<group>"; };
2CD77D052B935480009D455D /* UserSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSettings.swift; sourceTree = "<group>"; };
2CFA195329F2CD6B00FC3700 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
2CFA195629F2CD6B00FC3700 /* ExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleApp.swift; sourceTree = "<group>"; };
2CFA195829F2CD6B00FC3700 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -136,6 +138,7 @@
2CFA195829F2CD6B00FC3700 /* ContentView.swift */,
2CFA195629F2CD6B00FC3700 /* ExampleApp.swift */,
2CC7610529F57F5E009629B2 /* Screens.swift */,
2CD77D052B935480009D455D /* UserSettings.swift */,
2CFA195A29F2CD7000FC3700 /* Assets.xcassets */,
2CD2E50229FACF1C00DEAA95 /* CoreData */,
2CFA195D29F2CD7000FC3700 /* Preview Content */,
Expand Down Expand Up @@ -266,6 +269,7 @@
buildActionMask = 2147483647;
files = (
2CD2E50129FACEDC00DEAA95 /* Item.swift in Sources */,
2CD77D062B935480009D455D /* UserSettings.swift in Sources */,
2CD2E50729FACFC700DEAA95 /* MainView.swift in Sources */,
2CC7610629F57F5E009629B2 /* Screens.swift in Sources */,
2CD2E50529FACF3D00DEAA95 /* HomeScreen.swift in Sources */,
Expand Down
59 changes: 4 additions & 55 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,18 @@ import KamaalSettings
import KamaalNavigation

struct ContentView: View {
@State private var appColor = appColors[6]
@State private var selectedLanguageOption = languageOptions[0]
@EnvironmentObject private var userSettings: UserSettings

var body: some View {
NavigationStackView(initialStack: [Screens](), sidebar: { Sidebar() }, passthroughEnvironment: { view in
view
.settingsEnvironment(configuration: self.configuration)
.settingsEnvironment(configuration: self.userSettings.configuration)
})
.onAppColorChange(self.handleOnAppColorChange)
.onSettingsPreferenceChange(self.handlePreferenceChange)
}

private var configuration: SettingsConfiguration {
.init(
donations: donations,
color: .init(colors: appColors, currentColor: self.appColor),
preferences: self.preferences
)
}

var preferences: [Preference] {
[
.init(
id: UUID(uuidString: "66971130-a466-44cc-83f6-4759b51e7789")!,
label: "Language",
selectedOption: self.selectedLanguageOption,
options: languageOptions
),
]
}

private func handleOnAppColorChange(_ appColor: AppColor) {
self.appColor = appColor
}

private func handlePreferenceChange(_ preference: Preference) {
self.selectedLanguageOption = preference.selectedOption
.onAppColorChange(self.userSettings.handleOnAppColorChange)
.onSettingsPreferenceChange(self.userSettings.handlePreferenceChange)
}
}

private let languageOptions: [Preference.Option] = [
.init(id: UUID(uuidString: "067fb9e5-af94-4425-965b-ebd70e7f9e56")!, label: "English"),
.init(id: UUID(uuidString: "37c735d7-0804-469b-9219-ece30b0cbe4a")!, label: "Dutch"),
.init(id: UUID(uuidString: "0a8db1a5-fe1c-44cf-ba66-15ce2da1fab3")!, label: "French"),
]

private let appColors: [AppColor] = [
.init(id: UUID(uuidString: "1f6f9ac4-1ca6-4f77-880b-01580881a9b4")!, name: "Default", color: Color("AccentColor")),
.init(id: UUID(uuidString: "57547f06-2d3e-4f3d-a639-59c13a5433bb")!, name: "Teal", color: .teal),
.init(id: UUID(uuidString: "c5b39ff8-091a-4c46-a067-0bc5b1df4caf")!, name: "Purple", color: .purple),
.init(id: UUID(uuidString: "1d2a4931-f1c2-42bf-b097-27908e1fa39a")!, name: "Green", color: .green),
.init(id: UUID(uuidString: "7a664baf-b0ac-4764-86b4-3860773fe9c4")!, name: "Pink", color: .pink),
.init(id: UUID(uuidString: "ab3aa7c5-c9e3-45a9-a2ef-f82603f11eab")!, name: "Orange", color: .orange),
.init(id: UUID(uuidString: "0125142b-4a50-4f7f-b02c-a4963a6e4633")!, name: "Red", color: .red),
.init(id: UUID(uuidString: "eb82e779-a7ba-4c75-a6e2-53d35332b940")!, name: "Blue", color: .blue),
]

let donations: [StoreKitDonation] = [
.init(id: "io.kamaal.Example.Carrot", emoji: "🥕", weight: 1),
.init(id: "io.kamaal.Example.House", emoji: "🏡", weight: 20),
.init(id: "io.kamaal.Example.Ship", emoji: "🚢", weight: 69),
]

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Expand Down
5 changes: 4 additions & 1 deletion Example/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import SwiftUI

@main
struct ExampleApp: App {
@StateObject private var userSettings = UserSettings()

let persistenceController = PersistenceController.shared

var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, self.persistenceController.container.viewContext)
.frame(minWidth: 300, minHeight: 300)
.environment(\.managedObjectContext, self.persistenceController.container.viewContext)
.environmentObject(self.userSettings)
}
}
}
70 changes: 70 additions & 0 deletions Example/Example/UserSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// UserSettings.swift
//
//
// Created by Kamaal M Farah on 02/03/2024.
//

import SwiftUI
import KamaalSettings

final class UserSettings: ObservableObject {
@Published private var appColor = appColors[6]
@Published private var selectedLanguageOption = languageOptions[0]

init() { }

var configuration: SettingsConfiguration {
.init(
donations: donations,
color: self.colors,
preferences: self.preferences
)
}

func handleOnAppColorChange(_ appColor: AppColor) {
self.appColor = appColor
}

func handlePreferenceChange(_ preference: Preference) {
self.selectedLanguageOption = preference.selectedOption
}

private var colors: SettingsConfiguration.ColorsConfiguration {
.init(colors: appColors, currentColor: self.appColor)
}

private var preferences: [Preference] {
[
.init(
id: UUID(uuidString: "66971130-a466-44cc-83f6-4759b51e7789")!,
label: "Language",
selectedOption: self.selectedLanguageOption,
options: languageOptions
),
]
}
}

private let languageOptions: [Preference.Option] = [
.init(id: UUID(uuidString: "067fb9e5-af94-4425-965b-ebd70e7f9e56")!, label: "English"),
.init(id: UUID(uuidString: "37c735d7-0804-469b-9219-ece30b0cbe4a")!, label: "Dutch"),
.init(id: UUID(uuidString: "0a8db1a5-fe1c-44cf-ba66-15ce2da1fab3")!, label: "French"),
]

private let appColors: [AppColor] = [
.init(id: UUID(uuidString: "1f6f9ac4-1ca6-4f77-880b-01580881a9b4")!, name: "Default", color: Color("AccentColor")),
.init(id: UUID(uuidString: "57547f06-2d3e-4f3d-a639-59c13a5433bb")!, name: "Teal", color: .teal),
.init(id: UUID(uuidString: "c5b39ff8-091a-4c46-a067-0bc5b1df4caf")!, name: "Purple", color: .purple),
.init(id: UUID(uuidString: "1d2a4931-f1c2-42bf-b097-27908e1fa39a")!, name: "Green", color: .green),
.init(id: UUID(uuidString: "7a664baf-b0ac-4764-86b4-3860773fe9c4")!, name: "Pink", color: .pink),
.init(id: UUID(uuidString: "ab3aa7c5-c9e3-45a9-a2ef-f82603f11eab")!, name: "Orange", color: .orange),
.init(id: UUID(uuidString: "0125142b-4a50-4f7f-b02c-a4963a6e4633")!, name: "Red", color: .red),
.init(id: UUID(uuidString: "eb82e779-a7ba-4c75-a6e2-53d35332b940")!, name: "Blue", color: .blue),
]

private let donations: [StoreKitDonation] = [
.init(id: "io.kamaal.Example.Carrot", emoji: "🥕", weight: 1),
.init(id: "io.kamaal.Example.House", emoji: "🏡", weight: 20),
.init(id: "io.kamaal.Example.Ship", emoji: "🚢", weight: 69),
]

0 comments on commit 85b91e3

Please sign in to comment.