diff --git a/Domain/AnnotationsService/Sources/LastPageService.swift b/Domain/AnnotationsService/Sources/LastPageService.swift index c135e853..873f1f96 100644 --- a/Domain/AnnotationsService/Sources/LastPageService.swift +++ b/Domain/AnnotationsService/Sources/LastPageService.swift @@ -36,9 +36,9 @@ public struct LastPageService { return LastPage(quran: page.quran, persistenceModel) } - func update(page: LastPage, toPage: Page) async throws -> LastPage { + func update(page: Page, toPage: Page) async throws -> LastPage { let persistenceModel = try await persistence.update( - page: page.page.pageNumber, + page: page.pageNumber, toPage: toPage.pageNumber ) return LastPage(quran: toPage.quran, persistenceModel) diff --git a/Domain/AnnotationsService/Sources/LastPageUpdater.swift b/Domain/AnnotationsService/Sources/LastPageUpdater.swift index 32937b26..c22039f5 100644 --- a/Domain/AnnotationsService/Sources/LastPageUpdater.swift +++ b/Domain/AnnotationsService/Sources/LastPageUpdater.swift @@ -33,45 +33,51 @@ public final class LastPageUpdater { // MARK: Public - public private(set) var lastPage: LastPage? + public private(set) var lastPage: Page? - public func configure(initialPage: Page, lastPage: LastPage?) async { + public func configure(initialPage: Page, lastPage: Page?) { self.lastPage = lastPage if let lastPage { - await updateTo(page: initialPage, lastPage: lastPage) + updateTo(page: initialPage, lastPage: lastPage) } else { - await create(page: initialPage) + create(page: initialPage) } } - public func updateTo(pages: [Page]) async { + public func updateTo(pages: [Page]) { guard let page = pages.min() else { return } // don't update if it's the same page - guard let lastPage, page != lastPage.page else { return } + guard let lastPage, page != lastPage else { return } - await updateTo(page: page, lastPage: lastPage) + updateTo(page: page, lastPage: lastPage) } // MARK: Private private let service: LastPageService - private func updateTo(page: Page, lastPage: LastPage) async { - do { - self.lastPage = try await service.update(page: lastPage, toPage: page) - } catch { - crasher.recordError(error, reason: "Failed to update last page") + private func updateTo(page: Page, lastPage: Page) { + self.lastPage = page + Task { + do { + _ = try await service.update(page: lastPage, toPage: page) + } catch { + crasher.recordError(error, reason: "Failed to update last page") + } } } - private func create(page: Page) async { - do { - lastPage = try await service.add(page: page) - } catch { - crasher.recordError(error, reason: "Failed to create a last page") + private func create(page: Page) { + lastPage = page + Task { + do { + _ = try await service.add(page: page) + } catch { + crasher.recordError(error, reason: "Failed to create a last page") + } } } } diff --git a/Features/AppStructureFeature/Common/TabInteractor.swift b/Features/AppStructureFeature/Common/TabInteractor.swift index ab4001df..039ade75 100644 --- a/Features/AppStructureFeature/Common/TabInteractor.swift +++ b/Features/AppStructureFeature/Common/TabInteractor.swift @@ -7,7 +7,6 @@ // import FeaturesSupport -import QuranAnnotations import QuranContentFeature import QuranKit import QuranViewFeature @@ -27,7 +26,7 @@ class TabInteractor: QuranNavigator { weak var presenter: TabPresenter? - func navigateTo(page: Page, lastPage: LastPage?, highlightingSearchAyah: AyahNumber?) { + func navigateTo(page: Page, lastPage: Page?, highlightingSearchAyah: AyahNumber?) { let input = QuranInput(initialPage: page, lastPage: lastPage, highlightingSearchAyah: highlightingSearchAyah) let viewController = quranBuilder.build(input: input) presenter?.pushViewController(viewController, animated: true) diff --git a/Features/FeaturesSupport/QuranNavigator.swift b/Features/FeaturesSupport/QuranNavigator.swift index b8e610b8..f9a89745 100644 --- a/Features/FeaturesSupport/QuranNavigator.swift +++ b/Features/FeaturesSupport/QuranNavigator.swift @@ -5,10 +5,9 @@ // Created by Mohamed Afifi on 2023-06-19. // -import QuranAnnotations import QuranKit @MainActor public protocol QuranNavigator: AnyObject { - func navigateTo(page: Page, lastPage: LastPage?, highlightingSearchAyah: AyahNumber?) + func navigateTo(page: Page, lastPage: Page?, highlightingSearchAyah: AyahNumber?) } diff --git a/Features/HomeFeature/HomeBuilder.swift b/Features/HomeFeature/HomeBuilder.swift index 1f194094..c89de216 100644 --- a/Features/HomeFeature/HomeBuilder.swift +++ b/Features/HomeFeature/HomeBuilder.swift @@ -34,7 +34,7 @@ public struct HomeBuilder { lastPageService: lastPageService, textRetriever: textRetriever, navigateToPage: { [weak listener] lastPage in - listener?.navigateTo(page: lastPage.page, lastPage: lastPage, highlightingSearchAyah: nil) + listener?.navigateTo(page: lastPage, lastPage: lastPage, highlightingSearchAyah: nil) }, navigateToSura: { [weak listener] sura in listener?.navigateTo(page: sura.page, lastPage: nil, highlightingSearchAyah: nil) diff --git a/Features/HomeFeature/HomeView.swift b/Features/HomeFeature/HomeView.swift index c31b129d..74f51eb3 100644 --- a/Features/HomeFeature/HomeView.swift +++ b/Features/HomeFeature/HomeView.swift @@ -37,7 +37,7 @@ private struct HomeViewUI: View { let start: AsyncAction - let selectLastPage: ItemAction + let selectLastPage: ItemAction let selectSura: ItemAction let selectQuarter: ItemAction @@ -69,7 +69,7 @@ private struct HomeViewUI: View { subtitle: .init(text: lastPage.createdOn.timeAgo(), location: .bottom), accessory: .text(NumberFormatter.shared.format(lastPage.page.pageNumber)) ) { - selectLastPage(lastPage) + selectLastPage(lastPage.page) } } diff --git a/Features/HomeFeature/HomeViewModel.swift b/Features/HomeFeature/HomeViewModel.swift index 5ac45f87..0b148c07 100644 --- a/Features/HomeFeature/HomeViewModel.swift +++ b/Features/HomeFeature/HomeViewModel.swift @@ -27,7 +27,7 @@ final class HomeViewModel: ObservableObject { init( lastPageService: LastPageService, textRetriever: QuranTextDataService, - navigateToPage: @escaping (LastPage) -> Void, + navigateToPage: @escaping (Page) -> Void, navigateToSura: @escaping (Sura) -> Void, navigateToQuarter: @escaping (Quarter) -> Void ) { @@ -57,7 +57,7 @@ final class HomeViewModel: ObservableObject { _ = await [lastPages, suras, quarters] } - func navigateTo(_ lastPage: LastPage) { + func navigateTo(_ lastPage: Page) { navigateToPage(lastPage) } @@ -73,7 +73,7 @@ final class HomeViewModel: ObservableObject { private let lastPageService: LastPageService private let textRetriever: QuranTextDataService - private let navigateToPage: (LastPage) -> Void + private let navigateToPage: (Page) -> Void private let navigateToSura: (Sura) -> Void private let navigateToQuarter: (Quarter) -> Void diff --git a/Features/QuranContentFeature/ContentViewController.swift b/Features/QuranContentFeature/ContentViewController.swift index b210257d..7ad324e2 100644 --- a/Features/QuranContentFeature/ContentViewController.swift +++ b/Features/QuranContentFeature/ContentViewController.swift @@ -91,14 +91,12 @@ final class ContentViewController: UIViewController, UIGestureRecognizerDelegate viewModel.$quranUITraits .receive(on: DispatchQueue.main) .sink { [weak self] newTraits in - Task { - await self?.quranUITraitsUpdatedTo(newTraits) - } + self?.quranUITraitsUpdatedTo(newTraits) } .store(in: &cancellables) } - private func quranUITraitsUpdatedTo(_ quranUITraits: QuranUITraits) async { + private func quranUITraitsUpdatedTo(_ quranUITraits: QuranUITraits) { guard let dataSource = viewModel.dataSource else { return } @@ -106,18 +104,18 @@ final class ContentViewController: UIViewController, UIGestureRecognizerDelegate dataSource.quranUITraits = quranUITraits if let ayah = quranUITraits.highlights.verseToScrollTo(comparingTo: oldValue.highlights) { - await scrollTo(page: ayah.page, animated: true, forceReload: false) + scrollTo(page: ayah.page, animated: true, forceReload: false) } } - private func scrollTo(page: Page, animated: Bool, forceReload: Bool) async { + private func scrollTo(page: Page, animated: Bool, forceReload: Bool) { if UIApplication.shared.applicationState != .background { // update the UI only when the app is in foreground viewModel.dataSource?.scrollToPage(page, animated: animated, forceReload: forceReload) - await viewModel.visiblePagesUpdated() + viewModel.visiblePagesUpdated() } else { // Only update last page while in background - await viewModel.updateLastPageTo([page]) + viewModel.updateLastPageTo([page]) } } @@ -143,9 +141,7 @@ final class ContentViewController: UIViewController, UIGestureRecognizerDelegate viewModel.$dataSource .receive(on: DispatchQueue.main) .sink { [weak self] dataSource in - Task { - await self?.install(dataSource) - } + self?.install(dataSource) } .store(in: &cancellables) } @@ -171,7 +167,7 @@ final class ContentViewController: UIViewController, UIGestureRecognizerDelegate return pageController } - private func install(_ dataSource: PageDataSource?) async { + private func install(_ dataSource: PageDataSource?) { guard let dataSource else { return } @@ -182,8 +178,8 @@ final class ContentViewController: UIViewController, UIGestureRecognizerDelegate dataSource.usePageViewController(pageController) dataSource.scrollToPage(viewModel.lastViewedPage, animated: false, forceReload: true) - await viewModel.visiblePagesLoaded() - await quranUITraitsUpdatedTo(viewModel.quranUITraits) + viewModel.visiblePagesLoaded() + quranUITraitsUpdatedTo(viewModel.quranUITraits) } // MARK: - Gestures diff --git a/Features/QuranContentFeature/ContentViewModel.swift b/Features/QuranContentFeature/ContentViewModel.swift index e74f1f2b..31a48477 100644 --- a/Features/QuranContentFeature/ContentViewModel.swift +++ b/Features/QuranContentFeature/ContentViewModel.swift @@ -84,10 +84,7 @@ public final class ContentViewModel { .store(in: &cancellables) loadNotes() - - Task { - await configureAsInitialPage() - } + configureAsInitialPage() } // MARK: Public @@ -129,14 +126,14 @@ public final class ContentViewModel { } var lastViewedPage: Page { - deps.lastPageUpdater.lastPage?.page ?? input.initialPage + deps.lastPageUpdater.lastPage ?? input.initialPage } func userWillBeginDragScroll() { listener?.userWillBeginDragScroll() } - func visiblePagesLoaded() async { + func visiblePagesLoaded() { let pages = visiblePages let isTranslationView = deps.quranContentStatePreferences.quranMode == .translation crasher.setValue(pages.map(\.pageNumber), forKey: .pages) @@ -152,17 +149,17 @@ public final class ContentViewModel { } listener?.setVisiblePages(pages) - await updateLastPageTo(pages) + updateLastPageTo(pages) } - func visiblePagesUpdated() async { + func visiblePagesUpdated() { // remove search highlight when page changes quranUITraits.highlights.searchVerses = [] - await visiblePagesLoaded() + visiblePagesLoaded() } - func updateLastPageTo(_ pages: [Page]) async { - await deps.lastPageUpdater.updateTo(pages: pages) + func updateLastPageTo(_ pages: [Page]) { + deps.lastPageUpdater.updateTo(pages: pages) } func onViewLongPressStarted(at point: CGPoint, sourceView: UIView) { @@ -247,8 +244,8 @@ public final class ContentViewModel { return dict } - private func configureAsInitialPage() async { - await deps.lastPageUpdater.configure(initialPage: input.initialPage, lastPage: input.lastPage) + private func configureAsInitialPage() { + deps.lastPageUpdater.configure(initialPage: input.initialPage, lastPage: input.lastPage) loadNewElementModule() quranUITraits.highlights.searchVerses = [input.highlightingSearchAyah].compactMap { $0 } } diff --git a/Features/QuranContentFeature/QuranInput.swift b/Features/QuranContentFeature/QuranInput.swift index 39813004..0a319a1c 100644 --- a/Features/QuranContentFeature/QuranInput.swift +++ b/Features/QuranContentFeature/QuranInput.swift @@ -12,7 +12,7 @@ import QuranKit public struct QuranInput { // MARK: Lifecycle - public init(initialPage: Page, lastPage: LastPage?, highlightingSearchAyah: AyahNumber?) { + public init(initialPage: Page, lastPage: Page?, highlightingSearchAyah: AyahNumber?) { self.initialPage = initialPage self.lastPage = lastPage self.highlightingSearchAyah = highlightingSearchAyah @@ -21,6 +21,6 @@ public struct QuranInput { // MARK: Public public let initialPage: Page - public let lastPage: LastPage? + public let lastPage: Page? public let highlightingSearchAyah: AyahNumber? }