Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove async/await from LastPageUpdater API #566

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Domain/AnnotationsService/Sources/LastPageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 23 additions & 17 deletions Domain/AnnotationsService/Sources/LastPageUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
3 changes: 1 addition & 2 deletions Features/AppStructureFeature/Common/TabInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import FeaturesSupport
import QuranAnnotations
import QuranContentFeature
import QuranKit
import QuranViewFeature
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions Features/FeaturesSupport/QuranNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}
2 changes: 1 addition & 1 deletion Features/HomeFeature/HomeBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Features/HomeFeature/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private struct HomeViewUI: View {

let start: AsyncAction

let selectLastPage: ItemAction<LastPage>
let selectLastPage: ItemAction<Page>
let selectSura: ItemAction<Sura>
let selectQuarter: ItemAction<QuarterItem>

Expand Down Expand Up @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Features/HomeFeature/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down Expand Up @@ -57,7 +57,7 @@ final class HomeViewModel: ObservableObject {
_ = await [lastPages, suras, quarters]
}

func navigateTo(_ lastPage: LastPage) {
func navigateTo(_ lastPage: Page) {
navigateToPage(lastPage)
}

Expand All @@ -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

Expand Down
24 changes: 10 additions & 14 deletions Features/QuranContentFeature/ContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,31 @@ 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
}
let oldValue = dataSource.quranUITraits
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])
}
}

Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
23 changes: 10 additions & 13 deletions Features/QuranContentFeature/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ public final class ContentViewModel {
.store(in: &cancellables)

loadNotes()

Task {
await configureAsInitialPage()
}
configureAsInitialPage()
}

// MARK: Public
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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 }
}
Expand Down
4 changes: 2 additions & 2 deletions Features/QuranContentFeature/QuranInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
}