NewsBlur/clients/ios/Classes/ImportExportPreferences.swift
David Sinclair f9e067400c #1277 (slow swiping between stories)
- Added an Export Preferences… button in the Preferences view to save a copy of the preferences to a file.
- Added an Import Preferences… button to load a saved preferences file and replace the prefs.
- Added a document type and UTI type for the prefs file (you can provide an icon if you like).
- Enabled iCloud document support to allow saving to shared locations.
- Added a bridging header to enable Swift code to work properly.
2020-01-27 20:27:44 -08:00

80 lines
2.7 KiB
Swift

//
// ImportExportPreferences.swift
// NewsBlur
//
// Created by David Sinclair on 2020-01-27.
// Copyright © 2020 NewsBlur. All rights reserved.
//
import UIKit
/// Singleton class to import or export the preferences.
class ImportExportPreferences: NSObject {
/// Singleton shared instance.
static let shared = ImportExportPreferences()
/// Private init to prevent others constructing a new instance.
private override init() {
}
/// Import the preferences.
@objc(importFromController:) class func importPreferences(from controller: UIViewController) {
shared.importPreferences(from: controller)
}
/// Export the preferences.
@objc(exportFromController:) class func exportPreferences(from controller: UIViewController) {
shared.exportPreferences(from: controller)
}
}
private extension ImportExportPreferences {
struct Constant {
static let fileType = "com.newsblur.preferences"
static let fileName = "NewsBlur Preferences"
static let fileExtension = "newsblurprefs"
}
func importPreferences(from controller: UIViewController) {
let picker = UIDocumentPickerViewController(documentTypes: [Constant.fileType], in: .import)
picker.delegate = self
controller.present(picker, animated: true, completion: nil)
}
func exportPreferences(from controller: UIViewController) {
let temporaryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let prefsURL = temporaryURL.appendingPathComponent(Constant.fileName).appendingPathExtension(Constant.fileExtension)
let dictionary = UserDefaults.standard.dictionaryRepresentation() as NSDictionary
dictionary.write(to: prefsURL, atomically: true)
let picker: UIDocumentPickerViewController
if #available(iOS 11.0, *) {
picker = UIDocumentPickerViewController(urls: [prefsURL], in: .exportToService)
} else {
picker = UIDocumentPickerViewController(url: prefsURL, in: .exportToService)
}
controller.present(picker, animated: true, completion: nil)
}
}
extension ImportExportPreferences: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first, let dictionary = NSDictionary(contentsOf: url) as? [String : AnyObject] else {
return
}
let prefs = UserDefaults.standard
for (key, value) in dictionary {
prefs.set(value, forKey: key)
}
NewsBlurAppDelegate.shared()?.reloadFeedsView(true)
}
}