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 Image & Translation content view controllers #674

Merged
merged 1 commit into from
Oct 19, 2024
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
78 changes: 18 additions & 60 deletions Features/QuranContentFeature/ContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ public final class ContentViewController: UIViewController, UIGestureRecognizerD
true
}

public func word(at point: CGPoint, in view: UIView) -> Word? {
convert(point, from: view)
.flatMap { $0.word(at: $1) }
public func word(at point: CGPoint) -> Word? {
let actions = viewModel.geometryActions
for action in actions {
if let word = action.word(point) {
return word
}
}
return nil
}

// MARK: Internal
Expand All @@ -65,40 +70,21 @@ public final class ContentViewController: UIViewController, UIGestureRecognizerD

private let viewModel: ContentViewModel

private var pageViews: [PageView] {
findPageViews(in: self)
}

private func setUpPagesView() {
let viewModel = viewModel
let pagesView = PagesView(viewModel: viewModel)
let pagingController = UIHostingController(rootView: pagesView)
addFullScreenChild(pagingController)
}

private func verse(at point: CGPoint, in view: UIView) -> AyahNumber? {
convert(point, from: view)
.flatMap { $0.verse(at: $1) }
}

private func convert(_ point: CGPoint, from view: UIView) -> (view: PageView, point: CGPoint)? {
let localPointsAndControllers = pageViews.map { (view: $0, point: $0.view.convert(point, from: view)) }
let convertedViewPoint = localPointsAndControllers.first { $0.view.view.point(inside: $0.point, with: nil) }
return convertedViewPoint
}

private func findPageViews(in viewController: UIViewController) -> [PageView] {
var result = [PageView]()

for child in viewController.children {
if let fooVC = child as? PageView {
result.append(fooVC)
private func verse(at point: CGPoint) -> AyahNumber? {
let actions = viewModel.geometryActions
for action in actions {
if let verse = action.verse(point) {
return verse
}

// Recursively search in the child's children
result.append(contentsOf: findPageViews(in: child))
}

return result
return nil
}

// MARK: - Gestures
Expand All @@ -120,14 +106,15 @@ public final class ContentViewController: UIViewController, UIGestureRecognizerD
}

let point = sender.location(in: targetView)
let globalPoint = sender.location(in: nil)

switch sender.state {
case .began:
if let verse = verse(at: point, in: targetView) {
if let verse = verse(at: globalPoint) {
viewModel.onViewLongPressStarted(at: point, sourceView: targetView, verse: verse)
}
case .changed:
if let verse = verse(at: point, in: targetView) {
if let verse = verse(at: globalPoint) {
viewModel.onViewLongPressChanged(to: point, verse: verse)
}
case .ended:
Expand All @@ -137,32 +124,3 @@ public final class ContentViewController: UIViewController, UIGestureRecognizerD
}
}
}

private struct PagesView: View {
@ObservedObject var viewModel: ContentViewModel

var body: some View {
GeometryReader { geometry in
QuranPaginationView(
pagingStrategy: pagingStrategy(with: geometry),
selection: $viewModel.visiblePages,
pages: viewModel.deps.quran.pages
) { page in
StaticViewControllerRepresentable(viewController: viewModel.pageViewBuilder.build(at: page))
}
.id(viewModel.quranMode)
}
}

func pagingStrategy(with geometry: GeometryProxy) -> PagingStrategy {
if geometry.size.height > geometry.size.width {
return .singlePage
}

if !TwoPagesUtils.hasEnoughHorizontalSpace() {
return .singlePage
}

return viewModel.pagingStrategy
}
}
14 changes: 5 additions & 9 deletions Features/QuranContentFeature/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import AnnotationsService
import Combine
import Crashing
import QuranAnnotations
import QuranImageFeature
import QuranKit
import QuranPagesFeature
import QuranText
import QuranTextKit
import QuranTranslationFeature
import TranslationService
import UIKit
import VLogging
Expand All @@ -38,8 +40,8 @@ public final class ContentViewModel: ObservableObject {

let highlightsService: QuranHighlightsService

let imageDataSourceBuilder: PageViewBuilder
let translationDataSourceBuilder: PageViewBuilder
let imageDataSourceBuilder: ContentImageBuilder
let translationDataSourceBuilder: ContentTranslationBuilder
}

private struct LongPressData {
Expand Down Expand Up @@ -108,6 +110,7 @@ public final class ContentViewModel: ObservableObject {

@Published var quranMode: QuranMode
@Published var twoPagesEnabled: Bool
@Published var geometryActions: [PageGeometryActions] = []

@Published var highlights: QuranHighlights {
didSet {
Expand All @@ -125,13 +128,6 @@ public final class ContentViewModel: ObservableObject {
twoPagesEnabled ? .doublePage : .singlePage
}

var pageViewBuilder: PageViewBuilder {
switch deps.quranContentStatePreferences.quranMode {
case .arabic: return deps.imageDataSourceBuilder
case .translation: return deps.translationDataSourceBuilder
}
}

func onViewLongPressStarted(at point: CGPoint, sourceView: UIView, verse: AyahNumber) {
longPressData = LongPressData(
sourceView: sourceView,
Expand Down
49 changes: 49 additions & 0 deletions Features/QuranContentFeature/PagesView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// PagesView.swift
//
//
// Created by Mohamed Afifi on 2024-10-06.
//

import QuranPagesFeature
import QuranTextKit
import SwiftUI
import UIx

struct PagesView: View {
@StateObject var viewModel: ContentViewModel

var body: some View {
GeometryReader { geometry in
QuranPaginationView(
pagingStrategy: pagingStrategy(with: geometry),
selection: $viewModel.visiblePages,
pages: viewModel.deps.quran.pages
) { page in
Group {
switch viewModel.quranMode {
case .arabic:
viewModel.deps.imageDataSourceBuilder.build(at: page)
case .translation:
viewModel.deps.translationDataSourceBuilder.build(at: page)
}
}
}
.id(viewModel.quranMode)
}
.collectGeometryActions($viewModel.geometryActions)
}

private func pagingStrategy(with geometry: GeometryProxy) -> PagingStrategy {
// If portrait
if geometry.size.height > geometry.size.width {
return .singlePage
}

if !TwoPagesUtils.hasEnoughHorizontalSpace() {
return .singlePage
}

return viewModel.pagingStrategy
}
}
17 changes: 8 additions & 9 deletions Features/QuranImageFeature/ContentImageBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import ImageService
import QuranKit
import QuranPagesFeature
import ReadingService
import SwiftUI
import UIKit
import Utilities
import VLogging

@MainActor
public struct ContentImageBuilder: PageViewBuilder {
public struct ContentImageBuilder {
// MARK: Lifecycle

public init(container: AppDependencies, highlightsService: QuranHighlightsService) {
Expand All @@ -28,19 +29,17 @@ public struct ContentImageBuilder: PageViewBuilder {

// MARK: Public

public func build(at page: Page) -> PageView {
public func build(at page: Page) -> some View {
let reading = ReadingPreferences.shared.reading
let imageService = Self.buildImageDataService(reading: reading, container: container)

return ContentImageViewController(
let viewModel = ContentImageViewModel(
reading: reading,
page: page,
viewModel: ContentImageViewModel(
reading: reading,
page: page,
imageDataService: imageService,
highlightsService: highlightsService
)
imageDataService: imageService,
highlightsService: highlightsService
)
return ContentImageView(viewModel: viewModel)
}

// MARK: Internal
Expand Down
10 changes: 9 additions & 1 deletion Features/QuranImageFeature/ContentImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import NoorUI
import QuranGeometry
import QuranKit
import QuranPagesFeature
import SwiftUI

struct ContentImageView: View {
@ObservedObject var viewModel: ContentImageViewModel
@StateObject var viewModel: ContentImageViewModel

var body: some View {
VStack {
Expand All @@ -27,6 +28,13 @@ struct ContentImageView: View {
onGlobalFrameChange: { viewModel.imageFrame = $0 }
)
}
.geometryActions(
PageGeometryActions(
id: ObjectIdentifier(viewModel),
word: { point in viewModel.wordAtGlobalPoint(point) },
verse: { point in viewModel.wordAtGlobalPoint(point)?.verse }
)
)
.task {
await viewModel.loadImagePage()
}
Expand Down
55 changes: 0 additions & 55 deletions Features/QuranImageFeature/ContentImageViewController.swift

This file was deleted.

Loading
Loading