This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for additional data sources (#11)
* Add initial data source support * Country name cleaning
- Loading branch information
1 parent
df06d41
commit 36540ca
Showing
15 changed files
with
520 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// PreferenceView.swift | ||
// Covidcheck | ||
// | ||
// Created by Julian Schiavo on 18/3/2020. | ||
// Copyright © 2020 Julian Schiavo. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct PreferenceView: View { | ||
|
||
let preference: Preference | ||
|
||
@Binding var isPresented: Bool | ||
|
||
var body: some View { | ||
if preference.type == .picker { | ||
return PickerPreferenceView(preference: preference, isPresented: $isPresented) | ||
} else { | ||
fatalError("") | ||
} | ||
} | ||
} | ||
|
||
struct PickerPreferenceView: View { | ||
|
||
let preference: Preference | ||
|
||
@Binding var isPresented: Bool | ||
|
||
var body: some View { | ||
guard preference.type == .picker else { fatalError("Wrong preference type") } | ||
let pickerOptions = preference.pickerOptions | ||
let pickerOptionNames = Array(pickerOptions.keys).sorted() | ||
|
||
return List(pickerOptionNames, id: \.self) { option in | ||
Button(action: { | ||
self.preference.switch(to: pickerOptions[option]) | ||
self.isPresented.toggle() | ||
}, label: { | ||
Text(option) | ||
}) | ||
} | ||
.listStyle(GroupedListStyle()) | ||
.navigationBarTitle(preference.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Preferences.swift | ||
// Covidcheck | ||
// | ||
// Created by Julian Schiavo on 18/3/2020. | ||
// Copyright © 2020 Julian Schiavo. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum PreferenceType { | ||
case boolean | ||
case picker | ||
} | ||
|
||
protocol Preference { | ||
var type: PreferenceType { get } | ||
var id: String { get } | ||
var name: String { get } | ||
var value: String { get } | ||
var pickerOptions: [String: String] { get } | ||
|
||
func `switch`(to: String?) | ||
} | ||
|
||
struct DataSourcePreference: Preference { | ||
let dataSources: [String: DataSource.Type] = ["SCMP": SCMPDataSource.self, "Bing": BingDataSource.self] | ||
let defaultID = "Bing" | ||
|
||
let type = PreferenceType.picker | ||
|
||
let id = "DataSource" | ||
let name = "Data Source" | ||
|
||
var value: String { | ||
current.name | ||
} | ||
|
||
var current: DataSource { | ||
let id = UserDefaults.standard.string(forKey: "CurrentDataSource") ?? defaultID | ||
guard let dataSource = dataSources[id] ?? dataSources[defaultID] else { | ||
fatalError("Unknown data source ID") | ||
} | ||
return dataSource.init() | ||
} | ||
|
||
var pickerOptions: [String: String] { | ||
let array = dataSources.mapValues { $0.init().name }.map { ($0.value, $0.key) } | ||
return Dictionary(uniqueKeysWithValues: array) | ||
} | ||
|
||
func `switch`(to id: String?) { | ||
UserDefaults.standard.set(id ?? defaultID, forKey: "CurrentDataSource") | ||
DataManager.shared.updateData() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.