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

Select first reciter if no match found with the stored reciter id #600

Merged
merged 2 commits into from
Dec 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,24 @@ actor DownloadBatchDataController {

private func startPendingTasksIfNeeded() async {
if !initialRunningTasks.initialized {
logger.warning("startPendingTasksIfNeeded not initialized")
return
}

// if we have a session
guard let session else {
logger.warning("startPendingTasksIfNeeded no session")
return
}
// and there are empty slots to use for downloading
let runningTasks = await runningTasks
guard runningTasks < maxSimultaneousDownloads else {
logger.info("startPendingTasksIfNeeded no empty slots for download")
return
}
// and there are things to download
guard !batches.isEmpty else {
logger.info("startPendingTasksIfNeeded no batches to download")
return
}

Expand All @@ -215,11 +219,7 @@ actor DownloadBatchDataController {
}
}

if downloadTasks.isEmpty {
return
}

logger.info("Enqueuing \(downloadTasks.count) to download on empty channels.")
logger.info("startDownloadTasks \(downloadTasks.count) to download on empty channels.")

// start the tasks
for download in downloadTasks {
Expand Down
11 changes: 8 additions & 3 deletions Data/BatchDownloader/Sources/Downloader/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class DownloadManager: Sendable {
logger.info("Starting download manager")
let session = createSession()
await dataController.start(with: session)
logger.info("Download manager start completed")
logger.info("Download manager started")
}

@MainActor
Expand All @@ -78,11 +78,16 @@ public final class DownloadManager: Sendable {
}

public func getOnGoingDownloads() async -> [DownloadBatchResponse] {
await dataController.getOnGoingDownloads()
logger.info("getOnGoingDownloads requested")
let downloads = await dataController.getOnGoingDownloads()
logger.debug("Found \(downloads.count) ongoing downloads")
return downloads
}

public func download(_ batch: DownloadBatchRequest) async throws -> DownloadBatchResponse {
try await dataController.download(batch)
logger.debug("Requested to download \(batch.requests.map(\.url.absoluteString))")
let result = try await dataController.download(batch)
return result
}

// MARK: Private
Expand Down
13 changes: 12 additions & 1 deletion Features/AudioBannerFeature/AudioBannerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ public final class AudioBannerViewModel: RemoteCommandsHandlerDelegate {
}

var selectedReciter: Reciter? {
reciters.first { $0.id == preferences.lastSelectedReciterId }
let storedSelectedReciterId = preferences.lastSelectedReciterId
let selectedReciter = reciters.first { $0.id == storedSelectedReciterId }
if selectedReciter == nil {
let firstReciter = reciters.first
logger.error("AudioBanner: couldn't find reciter \(storedSelectedReciterId) using \(String(describing: firstReciter?.id)) instead")
return firstReciter
}
return selectedReciter
}

func start() async {
Expand Down Expand Up @@ -293,16 +300,20 @@ public final class AudioBannerViewModel: RemoteCommandsHandlerDelegate {
cancellableTasks.task { [weak self] in
do {
let downloaded = await self?.downloader.downloaded(reciter: selectedReciter, from: from, to: end) ?? true
logger.info("AudioBanner: reciter downloaded? \(downloaded)")
if !downloaded {
self?.startDownloading()
let download = try await self?.downloader.download(from: from, to: end, reciter: selectedReciter)
guard let download else {
logger.info("AudioBanner: couldn't create a download request")
return
}

await self?.observe([download])

for try await _ in download.progress { }

logger.info("AudioBanner: download completed")
}

try await self?.audioPlayer.play(
Expand Down
16 changes: 10 additions & 6 deletions Features/WordPointerFeature/WordPointerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import NoorUI
import Popover_OC
import UIKit
import UIx
import VLogging

public final class WordPointerViewController: UIViewController {
private enum GestureState {
case began
case changed(translation: CGPoint)
case ended(velocity: CGPoint)
}

// MARK: Lifecycle

init(viewModel: WordPointerViewModel) {
Expand Down Expand Up @@ -119,12 +126,6 @@ public final class WordPointerViewController: UIViewController {

// MARK: Private

private enum GestureState {
case began
case changed(translation: CGPoint)
case ended(velocity: CGPoint)
}

private let viewModel: WordPointerViewModel

// For word translation
Expand Down Expand Up @@ -180,11 +181,14 @@ public final class WordPointerViewController: UIViewController {
private func makeGestureState(_ gesture: UIPanGestureRecognizer) -> GestureState? {
switch gesture.state {
case .began:
logger.debug("Started pointer dragging")
return .began
case .changed:
let translation = gesture.translation(in: container)
logger.debug("Pointer dragged to new position \(translation)")
return .changed(translation: translation)
case .ended, .cancelled, .failed:
logger.debug("Ended pointer dragging \(gesture.state.rawValue)")
let velocity = gesture.velocity(in: container)
return .ended(velocity: velocity)
case .possible:
Expand Down
18 changes: 12 additions & 6 deletions Features/WordPointerFeature/WordPointerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Crashing
import QuranKit
import UIKit
import Utilities
import VLogging
import WordTextService

@MainActor
Expand All @@ -21,6 +22,12 @@ public protocol WordPointerListener: AnyObject {

@MainActor
final class WordPointerViewModel {
enum PanResult {
case none
case hidePopover
case showPopover(text: String)
}

// MARK: Lifecycle

init(service: WordTextService) {
Expand All @@ -29,12 +36,6 @@ final class WordPointerViewModel {

// MARK: Internal

enum PanResult {
case none
case hidePopover
case showPopover(text: String)
}

weak var listener: WordPointerListener?

func viewPanBegan() {
Expand All @@ -43,19 +44,24 @@ final class WordPointerViewModel {

func viewPanned(to point: CGPoint, in view: UIView) async -> PanResult {
guard let word = listener?.word(at: point, in: view) else {
logger.debug("No word found at position \(point)")
unhighlightWord()
return .hidePopover
}
logger.debug("Highlighting word \(word) at position: \(point)")
listener?.highlightWord(word)

if selectedWord == word {
logger.debug("Same word selected before")
return .none
}
do {
if let text = try await service.textForWord(word) {
logger.debug("Found text '\(text)' for word \(word)")
selectedWord = word
return .showPopover(text: text)
} else {
logger.debug("No text found for word \(word)")
return .hidePopover
}
} catch {
Expand Down
Loading