You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.6 KiB

// Copyright © 2015 Abhishek Banthia
import CoreLoggerKit
import CoreModelKit
3 years ago
import Foundation
struct ConfigExport {
3 years ago
private func generateJSON(from store: DataStore) {
let selectedKeys: Set<String> = Set([
UserDefaultKeys.showOnboardingFlow,
UserDefaultKeys.selectedTimeZoneFormatKey,
UserDefaultKeys.themeKey,
UserDefaultKeys.showDayInMenu,
UserDefaultKeys.showDateInMenu,
UserDefaultKeys.showPlaceInMenu,
UserDefaultKeys.displayFutureSliderKey,
UserDefaultKeys.startAtLogin,
UserDefaultKeys.showAppInForeground,
UserDefaultKeys.sunriseSunsetTime,
UserDefaultKeys.userFontSizePreference,
UserDefaultKeys.showUpcomingEventView,
UserDefaultKeys.showAllDayEventsInUpcomingView,
UserDefaultKeys.showMeetingInMenubar,
UserDefaultKeys.truncateTextLength,
UserDefaultKeys.futureSliderRange,
UserDefaultKeys.selectedCalendars,
UserDefaultKeys.appDisplayOptions,
UserDefaultKeys.longStatusBarWarningMessage,
UserDefaultKeys.menubarCompactMode,
UserDefaultKeys.defaultMenubarMode,
UserDefaultKeys.installHomeIndicatorObject,
UserDefaultKeys.switchToCompactModeAlert,
])
let dictionaryRep = UserDefaults.standard.dictionaryRepresentation()
3 years ago
var clockerPrefs: [String: Any] = [:]
for (key, value) in dictionaryRep {
if selectedKeys.contains(key) {
Logger.info("Config Export: Key is \(key) and value is \(value)")
clockerPrefs[key] = value
}
}
3 years ago
do {
3 years ago
let decodeJSON: [[String: Any]] = store.timezones().compactMap { data -> [String: Any]? in
guard let customObject = TimezoneData.customObject(from: data) else { return nil }
let timezoneDictionary: [String: Any] = [
"Name": customObject.formattedAddress ?? "",
"Custom": customObject.customLabel ?? "",
"TimezoneID": customObject.timezoneID ?? "N/A",
"Is System": customObject.isSystemTimezone ? 1 : 0,
"Is Favorite": customObject.isFavourite == 1 ? 1 : 0,
"Sunrise or Sunset": customObject.isSunriseOrSunset ? 1 : 0,
"Latitude": customObject.latitude ?? 0.0,
"Longitude": customObject.longitude ?? 0.0,
"Place Identifier": customObject.placeID ?? "0.0",
"Selection Type": "\(customObject.selectionType)",
]
return timezoneDictionary
}
3 years ago
let timezoneDict = ["Timezones": decodeJSON]
3 years ago
clockerPrefs.merge(timezoneDict) { current, _ in current }
guard let documentDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileUrl = documentDirectoryUrl.appendingPathComponent("Persons.json")
// Transform array into data and save it into file
do {
let data = try JSONSerialization.data(withJSONObject: clockerPrefs, options: [])
try data.write(to: fileUrl, options: [])
} catch {
print(error)
}
3 years ago
let json = try JSONSerialization.data(withJSONObject: clockerPrefs, options: .prettyPrinted)
print(json)
} catch {
Logger.info("Failure Observed \(error.localizedDescription)")
}
}
}