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

Fix showing the import modal on launch for newer phones #1090

Merged
merged 1 commit into from
Feb 6, 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
5 changes: 4 additions & 1 deletion BookPlayer/Coordinators/FolderListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FolderListCoordinator: ItemListCoordinator {
libraryService: LibraryServiceProtocol,
playbackService: PlaybackServiceProtocol,
syncService: SyncServiceProtocol,
importManager: ImportManager,
listRefreshService: ListSyncRefreshService
) {
self.folderRelativePath = folderRelativePath
Expand All @@ -28,7 +29,8 @@ class FolderListCoordinator: ItemListCoordinator {
playerManager: playerManager,
libraryService: libraryService,
playbackService: playbackService,
syncService: syncService,
syncService: syncService,
importManager: importManager,
listRefreshService: listRefreshService
)
}
Expand All @@ -42,6 +44,7 @@ class FolderListCoordinator: ItemListCoordinator {
libraryService: self.libraryService,
playbackService: self.playbackService,
syncService: self.syncService,
importManager: self.importManager,
listRefreshService: listRefreshService,
themeAccent: ThemeManager.shared.currentTheme.linkColor
)
Expand Down
4 changes: 4 additions & 0 deletions BookPlayer/Coordinators/ItemListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ItemListCoordinator: NSObject, Coordinator, AlertPresenter, BPLogger {
let libraryService: LibraryServiceProtocol
let playbackService: PlaybackServiceProtocol
let syncService: SyncServiceProtocol
let importManager: ImportManager
let listRefreshService: ListSyncRefreshService
let flow: BPCoordinatorPresentationFlow

Expand All @@ -27,13 +28,15 @@ class ItemListCoordinator: NSObject, Coordinator, AlertPresenter, BPLogger {
libraryService: LibraryServiceProtocol,
playbackService: PlaybackServiceProtocol,
syncService: SyncServiceProtocol,
importManager: ImportManager,
listRefreshService: ListSyncRefreshService
) {
self.flow = flow
self.playerManager = playerManager
self.libraryService = libraryService
self.playbackService = playbackService
self.syncService = syncService
self.importManager = importManager
self.listRefreshService = listRefreshService
}

Expand All @@ -53,6 +56,7 @@ class ItemListCoordinator: NSObject, Coordinator, AlertPresenter, BPLogger {
libraryService: libraryService,
playbackService: playbackService,
syncService: syncService,
importManager: importManager,
listRefreshService: ListSyncRefreshService(
playerManager: playerManager,
libraryService: libraryService,
Expand Down
66 changes: 41 additions & 25 deletions BookPlayer/Coordinators/LibraryListCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import UIKit

class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegate {
weak var tabBarController: UITabBarController?
let importManager: ImportManager

var fileSubscription: AnyCancellable?
var importOperationSubscription: AnyCancellable?
Expand All @@ -23,27 +22,6 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat

private var disposeBag = Set<AnyCancellable>()

init(
flow: BPCoordinatorPresentationFlow,
playerManager: PlayerManagerProtocol,
importManager: ImportManager,
libraryService: LibraryServiceProtocol,
playbackService: PlaybackServiceProtocol,
syncService: SyncServiceProtocol,
listRefreshService: ListSyncRefreshService
) {
self.importManager = importManager

super.init(
flow: flow,
playerManager: playerManager,
libraryService: libraryService,
playbackService: playbackService,
syncService: syncService,
listRefreshService: listRefreshService
)
}

// swiftlint:disable:next function_body_length
override func start() {
let vc = ItemListViewController.instantiate(from: .Main)
Expand All @@ -53,7 +31,8 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat
networkClient: NetworkClient(),
libraryService: self.libraryService,
playbackService: self.playbackService,
syncService: self.syncService,
syncService: self.syncService,
importManager: importManager,
listRefreshService: listRefreshService,
themeAccent: ThemeManager.shared.currentTheme.linkColor
)
Expand Down Expand Up @@ -120,15 +99,17 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat
AppDelegate.shared?.activeSceneDelegate != nil
else { return }

self.fileSubscription = self.importManager.observeFiles().sink { [weak self] files in
fileSubscription = importManager.observeFiles()
.receive(on: DispatchQueue.main)
.sink { [weak self] files in
guard let self = self,
!files.isEmpty,
self.shouldShowImportScreen() else { return }

self.showImport()
}

self.importOperationSubscription = self.importManager.operationPublisher.sink(receiveValue: { [weak self] operation in
importOperationSubscription = importManager.operationPublisher.sink(receiveValue: { [weak self] operation in
guard
let self,
let lastItemListViewController = self.flow.navigationController.viewControllers.last as? ItemListViewController
Expand All @@ -151,6 +132,41 @@ class LibraryListCoordinator: ItemListCoordinator, UINavigationControllerDelegat

self.importManager.start(operation)
})

notifyPendingFiles()
}

@MainActor
func notifyPendingFiles() {
// Get reference of all the files located inside the Documents, Shared and Inbox folders
let documentsURLs = ((try? FileManager.default.contentsOfDirectory(
at: DataManager.getDocumentsFolderURL(),
includingPropertiesForKeys: nil,
options: .skipsSubdirectoryDescendants
)) ?? [])
.filter {
$0.lastPathComponent != DataManager.processedFolderName
&& $0.lastPathComponent != DataManager.inboxFolderName
&& $0.lastPathComponent != DataManager.backupFolderName
}

let sharedURLs = (try? FileManager.default.contentsOfDirectory(
at: DataManager.getSharedFilesFolderURL(),
includingPropertiesForKeys: nil,
options: .skipsSubdirectoryDescendants
)) ?? []

let inboxURLs = (try? FileManager.default.contentsOfDirectory(
at: DataManager.getInboxFolderURL(),
includingPropertiesForKeys: nil,
options: .skipsSubdirectoryDescendants
)) ?? []

let urls = documentsURLs + sharedURLs + inboxURLs

guard !urls.isEmpty else { return }

processFiles(urls: urls)
}

func loadLastBookIfNeeded() {
Expand Down
2 changes: 1 addition & 1 deletion BookPlayer/Coordinators/MainCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ class MainCoordinator: NSObject {
let libraryCoordinator = LibraryListCoordinator(
flow: .pushFlow(navigationController: AppNavigationController.instantiate(from: .Main)),
playerManager: self.playerManager,
importManager: ImportManager(libraryService: self.libraryService),
libraryService: self.libraryService,
playbackService: self.playbackService,
syncService: syncService,
importManager: ImportManager(libraryService: self.libraryService),
listRefreshService: ListSyncRefreshService(
playerManager: playerManager,
libraryService: libraryService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ class ItemListViewController: UIViewController, MVVMControllerProtocol, Storyboa
if didAppearForFirstTime {
didAppearForFirstTime = false
viewModel.viewDidAppear()
/// Can't do this on viewDidLoad as there's no guarantee that the scene delegate will
/// have an 'active' status
if navigationController?.viewControllers.count == 1 {
navigationController!.interactivePopGestureRecognizer!.delegate = self
viewModel.notifyPendingFiles()
}
}
}
Expand Down
41 changes: 7 additions & 34 deletions BookPlayer/Library/ItemList Screen/ItemListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ItemListViewModel: ViewModelProtocol {
let playbackService: PlaybackServiceProtocol
private let listRefreshService: ListSyncRefreshService
let syncService: SyncServiceProtocol
private let importManager: ImportManager
var offset = 0

public private(set) var defaultArtwork: Data?
Expand Down Expand Up @@ -79,6 +80,7 @@ class ItemListViewModel: ViewModelProtocol {
libraryService: LibraryServiceProtocol,
playbackService: PlaybackServiceProtocol,
syncService: SyncServiceProtocol,
importManager: ImportManager,
listRefreshService: ListSyncRefreshService,
themeAccent: UIColor
) {
Expand All @@ -88,6 +90,7 @@ class ItemListViewModel: ViewModelProtocol {
self.libraryService = libraryService
self.playbackService = playbackService
self.syncService = syncService
self.importManager = importManager
self.listRefreshService = listRefreshService
self.defaultArtwork = ArtworkService.generateDefaultArtwork(from: themeAccent)?.pngData()
}
Expand Down Expand Up @@ -948,7 +951,7 @@ class ItemListViewModel: ViewModelProtocol {

func refreshAppState() async throws {
/// Check if there's any pending file to import
notifyPendingFiles()
await coordinator.getMainCoordinator()?.getLibraryCoordinator()?.notifyPendingFiles()

guard await syncService.queuedJobsCount() == 0 else {
throw BPSyncRefreshError.scheduledTasks
Expand All @@ -964,40 +967,10 @@ class ItemListViewModel: ViewModelProtocol {

// MARK: - Import related functions
extension ItemListViewModel {
func notifyPendingFiles() {
// Get reference of all the files located inside the Documents, Shared and Inbox folders
let documentsURLs = ((try? FileManager.default.contentsOfDirectory(
at: DataManager.getDocumentsFolderURL(),
includingPropertiesForKeys: nil,
options: .skipsSubdirectoryDescendants
)) ?? [])
.filter {
$0.lastPathComponent != DataManager.processedFolderName
&& $0.lastPathComponent != DataManager.inboxFolderName
&& $0.lastPathComponent != DataManager.backupFolderName
}

let sharedURLs = (try? FileManager.default.contentsOfDirectory(
at: DataManager.getSharedFilesFolderURL(),
includingPropertiesForKeys: nil,
options: .skipsSubdirectoryDescendants
)) ?? []

let inboxURLs = (try? FileManager.default.contentsOfDirectory(
at: DataManager.getInboxFolderURL(),
includingPropertiesForKeys: nil,
options: .skipsSubdirectoryDescendants
)) ?? []

let urls = documentsURLs + sharedURLs + inboxURLs

guard !urls.isEmpty else { return }

self.handleNewFiles(urls)
}

func handleNewFiles(_ urls: [URL]) {
self.coordinator.getMainCoordinator()?.getLibraryCoordinator()?.processFiles(urls: urls)
for url in urls {
importManager.process(url)
}
}

func handleOperationCompletion(_ files: [URL], suggestedFolderName: String?) {
Expand Down
5 changes: 3 additions & 2 deletions BookPlayerTests/Coordinators/ItemListCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class LibraryListCoordinatorTests: XCTestCase {
self.libraryListCoordinator = LibraryListCoordinator(
flow: .pushFlow(navigationController: self.presentingController),
playerManager: playerManagerMock,
importManager: ImportManager(libraryService: libraryService),
libraryService: libraryService,
playbackService: coreServices.playbackService,
syncService: syncServiceMock,
importManager: ImportManager(libraryService: libraryService),
listRefreshService: ListSyncRefreshService(
playerManager: playerManagerMock,
libraryService: libraryService,
Expand Down Expand Up @@ -91,7 +91,8 @@ class FolderListCoordinatorTests: XCTestCase {
playerManager: playerManagerMock,
libraryService: libraryService,
playbackService: PlaybackService(libraryService: libraryService),
syncService: syncServiceMock,
syncService: syncServiceMock,
importManager: ImportManager(libraryService: libraryService),
listRefreshService: ListSyncRefreshService(
playerManager: playerManagerMock,
libraryService: libraryService,
Expand Down
1 change: 1 addition & 0 deletions BookPlayerTests/ItemListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ItemListViewModelTests: XCTestCase {
libraryService: libraryService,
playbackService: PlaybackServiceProtocolMock(),
syncService: syncServiceMock,
importManager: ImportManager(libraryService: libraryService),
listRefreshService: ListSyncRefreshService(
playerManager: playerManagerMock,
libraryService: libraryService,
Expand Down