Implement group app containers

This commit is contained in:
Maurice Parker 2019-09-22 13:09:06 -05:00
parent 3cd33902cf
commit e14a75d515
3 changed files with 24 additions and 12 deletions

View File

@ -10,6 +10,8 @@ import UIKit
struct AppDefaults { struct AppDefaults {
static var shared = UserDefaults.standard
struct Key { struct Key {
static let lastImageCacheFlushDate = "lastImageCacheFlushDate" static let lastImageCacheFlushDate = "lastImageCacheFlushDate"
static let firstRunDate = "firstRunDate" static let firstRunDate = "firstRunDate"
@ -21,7 +23,7 @@ struct AppDefaults {
} }
static let isFirstRun: Bool = { static let isFirstRun: Bool = {
if let _ = UserDefaults.standard.object(forKey: Key.firstRunDate) as? Date { if let _ = AppDefaults.shared.object(forKey: Key.firstRunDate) as? Date {
return false return false
} }
firstRunDate = Date() firstRunDate = Date()
@ -39,11 +41,11 @@ struct AppDefaults {
static var refreshInterval: RefreshInterval { static var refreshInterval: RefreshInterval {
get { get {
let rawValue = UserDefaults.standard.integer(forKey: Key.refreshInterval) let rawValue = AppDefaults.shared.integer(forKey: Key.refreshInterval)
return RefreshInterval(rawValue: rawValue) ?? RefreshInterval.everyHour return RefreshInterval(rawValue: rawValue) ?? RefreshInterval.everyHour
} }
set { set {
UserDefaults.standard.set(newValue.rawValue, forKey: Key.refreshInterval) AppDefaults.shared.set(newValue.rawValue, forKey: Key.refreshInterval)
} }
} }
@ -89,7 +91,7 @@ struct AppDefaults {
Key.timelineGroupByFeed: false, Key.timelineGroupByFeed: false,
Key.timelineNumberOfLines: 3, Key.timelineNumberOfLines: 3,
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue] Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue]
UserDefaults.standard.register(defaults: defaults) AppDefaults.shared.register(defaults: defaults)
} }
} }
@ -106,27 +108,27 @@ private extension AppDefaults {
} }
static func bool(for key: String) -> Bool { static func bool(for key: String) -> Bool {
return UserDefaults.standard.bool(forKey: key) return AppDefaults.shared.bool(forKey: key)
} }
static func setBool(for key: String, _ flag: Bool) { static func setBool(for key: String, _ flag: Bool) {
UserDefaults.standard.set(flag, forKey: key) AppDefaults.shared.set(flag, forKey: key)
} }
static func int(for key: String) -> Int { static func int(for key: String) -> Int {
return UserDefaults.standard.integer(forKey: key) return AppDefaults.shared.integer(forKey: key)
} }
static func setInt(for key: String, _ x: Int) { static func setInt(for key: String, _ x: Int) {
UserDefaults.standard.set(x, forKey: key) AppDefaults.shared.set(x, forKey: key)
} }
static func date(for key: String) -> Date? { static func date(for key: String) -> Date? {
return UserDefaults.standard.object(forKey: key) as? Date return AppDefaults.shared.object(forKey: key) as? Date
} }
static func setDate(for key: String, _ date: Date?) { static func setDate(for key: String, _ date: Date?) {
UserDefaults.standard.set(date, forKey: key) AppDefaults.shared.set(date, forKey: key)
} }
static func sortDirection(for key:String) -> ComparisonResult { static func sortDirection(for key:String) -> ComparisonResult {

View File

@ -56,7 +56,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
// Force lazy initialization of the web view provider so that it can warm up the queue of prepared web views // Force lazy initialization of the web view provider so that it can warm up the queue of prepared web views
let _ = DetailViewControllerWebViewProvider.shared let _ = DetailViewControllerWebViewProvider.shared
AccountManager.shared = AccountManager(accountsFolder: RSDataSubfolder(nil, "Accounts")!) let accountsURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.\(Bundle.main.bundleIdentifier!)")
let accountsFolder = accountsURL!.appendingPathComponent("Accounts").absoluteString
let accountsFolderPath = accountsFolder.suffix(from: accountsFolder.index(accountsFolder.startIndex, offsetBy: 7))
AccountManager.shared = AccountManager(accountsFolder: String(accountsFolderPath))
AppDefaults.shared = UserDefaults.init(suiteName: "group.\(Bundle.main.bundleIdentifier!)")!
registerBackgroundTasks() registerBackgroundTasks()

View File

@ -23,7 +23,12 @@ class ShareViewController: SLComposeServiceViewController, ShareFolderPickerCont
override func viewDidLoad() { override func viewDidLoad() {
AccountManager.shared = AccountManager(accountsFolder: RSDataSubfolder(nil, "Accounts")!) let rootID = Bundle.main.bundleIdentifier!.replacingOccurrences(of: ".Share-Extension", with: "")
let accountsURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.\(rootID)")
let accountsFolder = accountsURL!.appendingPathComponent("Accounts").absoluteString
let accountsFolderPath = accountsFolder.suffix(from: accountsFolder.index(accountsFolder.startIndex, offsetBy: 7))
AccountManager.shared = AccountManager(accountsFolder: String(accountsFolderPath))
pickerData = FlattenedAccountFolderPickerData() pickerData = FlattenedAccountFolderPickerData()
if pickerData?.containers.count ?? 0 > 0 { if pickerData?.containers.count ?? 0 > 0 {