Skip to content

Commit

Permalink
Load User Defaults asynchronously to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
salimbraksa committed Feb 2, 2024
1 parent 9e80279 commit 9b537e3
Showing 1 changed file with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

final class BooleanUserDefaultsDebugViewModel: ObservableObject {
private let persistentRepository: UserPersistentRepository
private let coreDataStack: CoreDataStack
private let coreDataStack: CoreDataStackSwift

private var allUserDefaultsSections = Sections() {
didSet {
Expand All @@ -16,40 +16,51 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
}
}

@Published var userDefaultsSections: Sections = []
@MainActor @Published private(set) var userDefaultsSections: Sections = []

init(coreDataStack: CoreDataStack = ContextManager.shared,
init(coreDataStack: CoreDataStackSwift = ContextManager.shared,
persistentRepository: UserPersistentRepository = UserPersistentStoreFactory.instance()) {
self.coreDataStack = coreDataStack
self.persistentRepository = persistentRepository
}

func load() {
Task {
await self.load()
}
}

private func load() async {
let allUserDefaults = persistentRepository.dictionaryRepresentation()
var loadedUserDefaultsSections = Sections()
var otherSection = [Row]()

for (key, value) in allUserDefaults {
if let groupedUserDefaults = value as? [String: Bool], !isFeatureFlagsSection(key) {
let rows = await processGroupedUserDefaults(groupedUserDefaults)
let section = Section(
key: key,
rows: processGroupedUserDefaults(groupedUserDefaults)
rows: rows
)
loadedUserDefaultsSections.append(section)
} else if let booleanUserDefault = value as? Bool, !isGutenbergUserDefault(key) {
otherSection.append(.init(key: key, title: key, value: booleanUserDefault))
}
}

if !otherSection.isEmpty {
let rows = otherSection.sorted { $0.title < $1.title }
let section = Section(key: Strings.otherBooleanUserDefaultsSectionID, rows: rows)
loadedUserDefaultsSections.append(section)
}
allUserDefaultsSections = loadedUserDefaultsSections

self.allUserDefaultsSections = loadedUserDefaultsSections
}

private func reloadSections() {
self.userDefaultsSections = filterUserDefaults(by: searchQuery)
private func reloadSections() {
Task { @MainActor in
self.userDefaultsSections = filterUserDefaults(by: searchQuery)
}
}

private func filterUserDefaults(by query: String) -> Sections {
Expand All @@ -73,22 +84,28 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
return filteredSections
}

private func processGroupedUserDefaults(_ userDefaults: [String: Bool]) -> [Row] {
var rows = userDefaults.reduce(into: [Row]()) { result, keyValue in
let (key, value) = keyValue
result.append(processSingleUserDefault(key: key, value: value))
private func processGroupedUserDefaults(_ userDefaults: [String: Bool]) async -> [Row] {
let rows = try? await self.coreDataStack.performAndSave { [weak self] context -> [Row] in
guard let self else {
return []
}
return userDefaults
.reduce(into: [Row]()) { result, keyValue in
let (key, value) = keyValue
let row = self.processSingleUserDefault(key: key, value: value, in: context)
result.append(row)
}.sorted { $0.title < $1.title }
}
rows = rows.sorted { $0.title < $1.title }
return rows
return rows ?? []
}

private func processSingleUserDefault(key: String, value: Bool) -> Row {
let title = findBlog(byID: key)?.url ?? key
private func processSingleUserDefault(key: String, value: Bool, in context: NSManagedObjectContext) -> Row {
let title = findBlog(byID: key, in: context)?.url ?? key
return Row(key: key, title: title, value: value)
}

private func findBlog(byID id: String) -> Blog? {
return try? Blog.lookup(withID: Int(id) ?? 0, in: coreDataStack.mainContext)
private func findBlog(byID id: String, in context: NSManagedObjectContext) -> Blog? {
return try? Blog.lookup(withID: Int(id) ?? 0, in: context)
}

func updateUserDefault(_ newValue: Bool, section: Section, row: Row) {
Expand Down

0 comments on commit 9b537e3

Please sign in to comment.