Adds ability to change search provider

This commit is contained in:
James Magahern
2021-03-09 12:20:51 -08:00
parent 1e39582a46
commit fb03ca2676
5 changed files with 128 additions and 6 deletions

View File

@@ -0,0 +1,51 @@
//
// Settings.swift
// App
//
// Created by James Magahern on 3/9/21.
//
import Foundation
@propertyWrapper
public struct SettingProperty<T: RawRepresentable>
{
public var key: String
public var defaultValue: T
public init(wrappedValue: T, key: String) {
self.key = key
self.defaultValue = wrappedValue
}
public var wrappedValue: T {
get {
guard let rawValue = UserDefaults.standard.object(forKey: key) as? T.RawValue else { return defaultValue }
return T(rawValue: rawValue) ?? defaultValue
}
set {
UserDefaults.standard.setValue(newValue.rawValue, forKey: key)
}
}
}
class Settings
{
static let shared = Settings()
public enum SearchProviderSetting: String, CaseIterable {
case google = "Google"
case duckduckgo = "DuckDuckGo"
case searxnor = "Searx.nor"
func provider() -> SearchProvider {
switch self {
case .google: return SearchProvider.google
case .duckduckgo: return SearchProvider.duckduckgo
case .searxnor: return SearchProvider.searxnor
}
}
}
@SettingProperty(key: "searchProvider")
public var searchProvider: SearchProviderSetting = .searxnor
}