@@ -2,7 +2,7 @@ import SwiftUI
22
33final class BooleanUserDefaultsDebugViewModel : ObservableObject {
44 private let persistentRepository : UserPersistentRepository
5- private let coreDataStack : CoreDataStack
5+ private let coreDataStack : CoreDataStackSwift
66
77 private var allUserDefaultsSections = Sections ( ) {
88 didSet {
@@ -16,40 +16,51 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
1616 }
1717 }
1818
19- @Published var userDefaultsSections : Sections = [ ]
19+ @MainActor @ Published private ( set ) var userDefaultsSections : Sections = [ ]
2020
21- init ( coreDataStack: CoreDataStack = ContextManager . shared,
21+ init ( coreDataStack: CoreDataStackSwift = ContextManager . shared,
2222 persistentRepository: UserPersistentRepository = UserPersistentStoreFactory . instance ( ) ) {
2323 self . coreDataStack = coreDataStack
2424 self . persistentRepository = persistentRepository
2525 }
2626
2727 func load( ) {
28+ Task {
29+ await self . load ( )
30+ }
31+ }
32+
33+ private func load( ) async {
2834 let allUserDefaults = persistentRepository. dictionaryRepresentation ( )
2935 var loadedUserDefaultsSections = Sections ( )
3036 var otherSection = [ Row] ( )
3137
3238 for (key, value) in allUserDefaults {
3339 if let groupedUserDefaults = value as? [ String : Bool ] , !isFeatureFlagsSection( key) {
40+ let rows = await processGroupedUserDefaults ( groupedUserDefaults)
3441 let section = Section (
3542 key: key,
36- rows: processGroupedUserDefaults ( groupedUserDefaults )
43+ rows: rows
3744 )
3845 loadedUserDefaultsSections. append ( section)
3946 } else if let booleanUserDefault = value as? Bool , !isGutenbergUserDefault( key) {
4047 otherSection. append ( . init( key: key, title: key, value: booleanUserDefault) )
4148 }
4249 }
50+
4351 if !otherSection. isEmpty {
4452 let rows = otherSection. sorted { $0. title < $1. title }
4553 let section = Section ( key: Strings . otherBooleanUserDefaultsSectionID, rows: rows)
4654 loadedUserDefaultsSections. append ( section)
4755 }
48- allUserDefaultsSections = loadedUserDefaultsSections
56+
57+ self . allUserDefaultsSections = loadedUserDefaultsSections
4958 }
5059
51- private func reloadSections( ) {
52- self . userDefaultsSections = filterUserDefaults ( by: searchQuery)
60+ private func reloadSections( ) {
61+ Task { @MainActor in
62+ self . userDefaultsSections = filterUserDefaults ( by: searchQuery)
63+ }
5364 }
5465
5566 private func filterUserDefaults( by query: String ) -> Sections {
@@ -73,22 +84,28 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
7384 return filteredSections
7485 }
7586
76- private func processGroupedUserDefaults( _ userDefaults: [ String : Bool ] ) -> [ Row ] {
77- var rows = userDefaults. reduce ( into: [ Row] ( ) ) { result, keyValue in
78- let ( key, value) = keyValue
79- result. append ( processSingleUserDefault ( key: key, value: value) )
87+ private func processGroupedUserDefaults( _ userDefaults: [ String : Bool ] ) async -> [ Row ] {
88+ let rows = try ? await self . coreDataStack. performAndSave { [ weak self] context -> [ Row ] in
89+ guard let self else {
90+ return [ ]
91+ }
92+ return userDefaults
93+ . reduce ( into: [ Row] ( ) ) { result, keyValue in
94+ let ( key, value) = keyValue
95+ let row = self . processSingleUserDefault ( key: key, value: value, in: context)
96+ result. append ( row)
97+ } . sorted { $0. title < $1. title }
8098 }
81- rows = rows. sorted { $0. title < $1. title }
82- return rows
99+ return rows ?? [ ]
83100 }
84101
85- private func processSingleUserDefault( key: String , value: Bool ) -> Row {
86- let title = findBlog ( byID: key) ? . url ?? key
102+ private func processSingleUserDefault( key: String , value: Bool , in context : NSManagedObjectContext ) -> Row {
103+ let title = findBlog ( byID: key, in : context ) ? . url ?? key
87104 return Row ( key: key, title: title, value: value)
88105 }
89106
90- private func findBlog( byID id: String ) -> Blog ? {
91- return try ? Blog . lookup ( withID: Int ( id) ?? 0 , in: coreDataStack . mainContext )
107+ private func findBlog( byID id: String , in context : NSManagedObjectContext ) -> Blog ? {
108+ return try ? Blog . lookup ( withID: Int ( id) ?? 0 , in: context )
92109 }
93110
94111 func updateUserDefault( _ newValue: Bool , section: Section , row: Row ) {
0 commit comments