Skip to content

Commit

Permalink
Cleanup list views
Browse files Browse the repository at this point in the history
  • Loading branch information
hk14004 committed Mar 29, 2023
1 parent 7510ca1 commit b44e879
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
4 changes: 2 additions & 2 deletions CatalogViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@
D40BC2F829CE286000B4A1E2 /* Navigation */ = {
isa = PBXGroup;
children = (
D40BC2FB29CE299E00B4A1E2 /* Categories */,
D40BC2F929CE288300B4A1E2 /* AppCoordinator.swift */,
D40BC2FB29CE299E00B4A1E2 /* Categories */,
);
path = Navigation;
sourceTree = "<group>";
Expand Down Expand Up @@ -219,8 +219,8 @@
D40BC30029CE2C8B00B4A1E2 /* Screens */ = {
isa = PBXGroup;
children = (
D40BC30629CE3C6D00B4A1E2 /* CategoryProductsScreen */,
D40BC30529CE3C0700B4A1E2 /* CategoriesScreen */,
D40BC30629CE3C6D00B4A1E2 /* CategoryProductsScreen */,
);
path = Screens;
sourceTree = "<group>";
Expand Down
4 changes: 0 additions & 4 deletions CatalogViewer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
Expand All @@ -31,6 +29,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


}

18 changes: 7 additions & 11 deletions CatalogViewer/UI/Screens/CategoriesScreen/CategoriesScreenVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension CategoriesScreenVM {
// Load DB cache and display again, this time we know if there is no data
await updateCategoriesSection()
// Observe and react to DB changes
observeLocalData()
observeCachedData()
}
}

Expand All @@ -97,29 +97,25 @@ extension CategoriesScreenVM {
if !categoriesRefreshed, loaded.isEmpty {
return
}

loadedCategories = loaded
onLocalCategoriesUpdated(list: loaded)
onRenderCategories(list: loaded)
}

private func refreshCategories() async {
await categoryRepository.refreshCategories()
categoriesRefreshed = true
}

private func observeLocalData() {
private func observeCachedData() {
bag.categoriesHandle = categoryRepository.observeCategories()
.dropFirst()
.removeDuplicates().sink { [weak self] _categories in
self?.onLocalCategoriesUpdated(list: _categories)
.removeDuplicates().sink { [unowned self] _categories in
loadedCategories = _categories
onRenderCategories(list: _categories)
}
}

private func onLocalCategoriesUpdated(list: [Category]) {
loadedCategories = list

// Update UI

private func onRenderCategories(list: [Category]) {
func onUpdateUI() {
let updatedSection = makeCategoriesListSection(items: loadedCategories)
sections.update(section: updatedSection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CategoryProductsScreenVM: ObservableObject {
private let productsRepository: ProductRepositoryProtocol
private var productsRefreshed: Bool = false
private lazy var redactedProducts: [Cell] = makeRedactedCells(count: 12)

init(category: Category, productsRepository: ProductRepositoryProtocol) {
self.category = category
self.productsRepository = productsRepository
Expand All @@ -73,7 +73,7 @@ extension CategoryProductsScreenVM {
// Load DB cache and display again, this time we know if there is no data
await updateProductsSection()
// Observe and react to DB changes
observeLocalData()
observeCachedData()
}
}

Expand All @@ -84,7 +84,7 @@ extension CategoryProductsScreenVM {
}

loadedProducts = loaded
onLocalProductsUpdated(list: loaded)
onRenderProducts(list: loaded)

}

Expand All @@ -93,20 +93,17 @@ extension CategoryProductsScreenVM {
productsRefreshed = true
}

private func observeLocalData() {
private func observeCachedData() {
bag.productsHandle = productsRepository.observeProducts(categoryIds: [category.id])
.dropFirst()
.removeDuplicates()
.sink { [weak self] _products in
self?.onLocalProductsUpdated(list: _products)
.sink { [unowned self] _products in
loadedProducts = _products
onRenderProducts(list: _products)
}
}

private func onLocalProductsUpdated(list: [Product]) {
loadedProducts = list

// Update UI

private func onRenderProducts(list: [Product]) {
func onUpdateUI() {
let updatedSection = makeProductListSection(items: loadedProducts)
sections.update(section: updatedSection)
Expand Down

0 comments on commit b44e879

Please sign in to comment.