Skip to content

Commit

Permalink
bookmark injection, download manager fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbarela committed Sep 6, 2024
1 parent 1cd801e commit 534c6be
Show file tree
Hide file tree
Showing 55 changed files with 177 additions and 222 deletions.
Binary file added .github/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion Marlin/Marlin/DataSources/Bookmark/BookmarkNotes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct BookmarkNotes: View {
@EnvironmentObject var repository: BookmarkRepository
var itemKey: String?
var dataSource: String?
var notes: String?
Expand Down
9 changes: 8 additions & 1 deletion Marlin/Marlin/DataSources/Publication/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class DownloadManager: NSObject {
init(subject: PassthroughSubject<DownloadProgress, Never>, downloadable: Downloadable) {
self.subject = subject
self.downloadable = downloadable
super.init()
}

func download() {
Expand Down Expand Up @@ -199,7 +200,10 @@ extension DownloadManager: URLSessionDownloadDelegate {
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
urlSession.invalidateAndCancel()

guard let url = downloadTask.currentRequest?.url, let downloadable = urlToDownloadableMap[url] else {
subject.send(completion: .finished)
return
}

Expand All @@ -209,18 +213,19 @@ extension DownloadManager: URLSessionDownloadDelegate {
let destinationUrl: URL? = URL(string: downloadable.savePath)

guard let destinationUrl = destinationUrl else {
subject.send(completion: .finished)
return
}

guard let httpResponse = downloadTask.response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
saveError(downloadable: downloadable, response: downloadTask.response)
subject.send(completion: .finished)
return
}

prepareForSaving(destinationUrl: destinationUrl)

print("Does the file exist \(location.path)")
if !FileManager.default.fileExists(atPath: location.path) {
print("error file not saved")
subject.send(
Expand All @@ -232,6 +237,7 @@ extension DownloadManager: URLSessionDownloadDelegate {
error: "Error downloading (file not saved)"
)
)
subject.send(completion: .finished)
return
}

Expand Down Expand Up @@ -272,6 +278,7 @@ extension DownloadManager: URLSessionDownloadDelegate {
error: ""
)
)
subject.send(completion: .finished)
} catch {
print("error saving file to \(destinationUrl.path) \(error)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import MapKit
import geopackage_ios

struct GeoPackageFeatureItemDetailView: View {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter

var featureItem: GeoPackageFeatureItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI

struct GeoPackageFeatureItemSummaryView: DataSourceSummaryView {
@EnvironmentObject var router: MarlinRouter
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@StateObject var bookmarkViewModel: BookmarkViewModel = BookmarkViewModel()

var showMoreDetails: Bool = true
Expand Down Expand Up @@ -50,7 +49,6 @@ struct GeoPackageFeatureItemSummaryView: DataSourceSummaryView {
)
}
.onAppear {
bookmarkViewModel.repository = bookmarkRepository
bookmarkViewModel.getBookmark(itemKey: featureItem.itemKey, dataSource: DataSources.geoPackage.key)
}

Expand Down
6 changes: 0 additions & 6 deletions Marlin/Marlin/MarlinApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ struct MarlinApp: App {
var appState: AppState

@StateObject var dataSourceList: DataSourceList = DataSourceList()
var bookmarkRepository: BookmarkRepository
var userPlaceRepository: UserPlaceRepository
var searchRepository: SearchRepository

Expand Down Expand Up @@ -172,10 +171,6 @@ struct MarlinApp: App {
routeWaypointRepository = RouteWaypointRepository(
localDataSource: RouteWaypointCoreDataDataSource(context: persistentStore.viewContext))

bookmarkRepository = BookmarkRepository(
localDataSource: BookmarkCoreDataDataSource()
)

asamsTileRepository = AsamsTileRepository()
modusTileRepository = ModusTileRepository()
portsTileRepository = PortsTileRepository()
Expand Down Expand Up @@ -203,7 +198,6 @@ struct MarlinApp: App {
.environmentObject(LocationManager.shared())
.environmentObject(appState)
.environmentObject(dataSourceList)
.environmentObject(bookmarkRepository)
.environmentObject(routeRepository)
.environmentObject(routeWaypointRepository)
.environmentObject(userPlaceRepository)
Expand Down
11 changes: 11 additions & 0 deletions Marlin/Marlin/Repository/Bookmark/BookmarkLocalDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import Combine
import UIKit
import BackgroundTasks

private struct BookmarkLocalDataSourceProviderKey: InjectionKey {
static var currentValue: BookmarkLocalDataSource = BookmarkCoreDataDataSource()
}

extension InjectedValues {
var bookmarkLocalDataSource: BookmarkLocalDataSource {
get { Self[BookmarkLocalDataSourceProviderKey.self] }
set { Self[BookmarkLocalDataSourceProviderKey.self] = newValue }
}
}

protocol BookmarkLocalDataSource {
func getBookmark(itemKey: String, dataSource: String) -> BookmarkModel?
func createBookmark(notes: String?, itemKey: String, dataSource: String) async
Expand Down
20 changes: 13 additions & 7 deletions Marlin/Marlin/Repository/Bookmark/BookmarkRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ enum BookmarkItem: Hashable, Identifiable {
case sectionHeader(header: String)
}

private struct BookmarkRepositoryProviderKey: InjectionKey {
static var currentValue: BookmarkRepository = BookmarkRepository()
}

extension InjectedValues {
var bookmarkRepository: BookmarkRepository {
get { Self[BookmarkRepositoryProviderKey.self] }
set { Self[BookmarkRepositoryProviderKey.self] = newValue }
}
}

class BookmarkRepository: ObservableObject {
let localDataSource: BookmarkLocalDataSource
@Injected(\.bookmarkLocalDataSource)
private var localDataSource: BookmarkLocalDataSource

@Injected(\.asamRepository)
var asamRepository: AsamRepository
Expand All @@ -44,12 +56,6 @@ class BookmarkRepository: ObservableObject {
@Injected(\.navWarningRepository)
var navigationalWarningRepository: NavigationalWarningRepository

init(
localDataSource: BookmarkLocalDataSource
) {
self.localDataSource = localDataSource
}

func getBookmark(itemKey: String, dataSource: String) -> BookmarkModel? {
localDataSource.getBookmark(itemKey: itemKey, dataSource: dataSource)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ extension NoticeToMarinersRepository {
cancellable = subject
.sink(
receiveCompletion: { [weak self] _ in
self?.remoteDataSource.cleanupDownload(model: notice)
if let cancellable = cancellable {
self?.cancellables.remove(cancellable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,16 @@ extension PublicationCoreDataDataSource {
}

func checkFileExists(s3Key: String) -> Bool {
print("XXX check file exists in publication core data source")
return context.performAndWait {
guard let epub = context.fetchFirst(ElectronicPublication.self, key: "s3Key", value: s3Key) else {
print("XXX no publication")
return false
}
var downloaded = false
if let destinationUrl = URL(string: epub.savePath) {
downloaded = FileManager().fileExists(atPath: destinationUrl.path)
print("XXX file exists \(downloaded)")
}
if downloaded != epub.isDownloaded {
epub.isDownloaded = downloaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PublicationRemoteDataSource: RemoteDataSource<PublicationModel> {
}

func cancelDownload(model: PublicationModel) {
cleanupDownload(model: model)
downloads[model.id]?.cancel()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ extension PublicationRepository {
cancellable = subject
.sink(
receiveCompletion: { [weak self] _ in
self?.remoteDataSource.cleanupDownload(model: publication)
if let cancellable = cancellable {
self?.cancellables.remove(cancellable)
}
Expand All @@ -149,7 +150,8 @@ extension PublicationRepository {
}

func checkFileExists(id: String) -> Bool {
localDataSource.checkFileExists(s3Key: id)
print("XXX checking file exists in local data source \(localDataSource)")
return localDataSource.checkFileExists(s3Key: id)
}

func cancelDownload(s3Key: String) {
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/ASAM/AsamSummaryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct AsamSummaryView: DataSourceSummaryView {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter

var showSectionHeader: Bool = false
Expand Down Expand Up @@ -46,7 +45,6 @@ struct AsamSummaryView: DataSourceSummaryView {
)
}
.onAppear {
bookmarkViewModel.repository = bookmarkRepository
bookmarkViewModel.getBookmark(itemKey: asam.id, dataSource: DataSources.asam.key)
}
}
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/Bookmark/BookmarkListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct BookmarkListView: View {
@EnvironmentObject var repository: BookmarkRepository
@StateObject var viewModel: BookmarksViewModel = BookmarksViewModel()

@EnvironmentObject var router: MarlinRouter
Expand Down Expand Up @@ -82,7 +81,6 @@ struct BookmarkListView: View {
.background(Color.backgroundColor)
.foregroundColor(Color.onSurfaceColor)
.onAppear {
viewModel.repository = repository
Metrics.shared.dataSourceList(dataSource: DataSources.bookmark)
}
}
Expand Down
4 changes: 3 additions & 1 deletion Marlin/Marlin/UI/Bookmark/BookmarkSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import SwiftUI

struct BookmarkSummary: DataSourceSummaryView {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@Injected(\.bookmarkRepository)
private var bookmarkRepository: BookmarkRepository

@EnvironmentObject var router: MarlinRouter

var showMoreDetails: Bool = false
Expand Down
11 changes: 6 additions & 5 deletions Marlin/Marlin/UI/Bookmark/BookmarkViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ class BookmarkViewModel: ObservableObject {
@Published var bookmarkBottomSheet: Bool = false
@Published var bnotes: String = ""

var repository: BookmarkRepository?
@Injected(\.bookmarkRepository)
private var repository: BookmarkRepository

@discardableResult
func getBookmark(itemKey: String, dataSource: String) -> BookmarkModel? {
self.itemKey = itemKey
self.dataSource = dataSource
bookmark = repository?.getBookmark(itemKey: itemKey, dataSource: dataSource)
bookmark = repository.getBookmark(itemKey: itemKey, dataSource: dataSource)
self.isBookmarked = bookmark != nil
return bookmark
}

func createBookmark(notes: String) {
Task {
await repository?.createBookmark(
await repository.createBookmark(
notes: notes,
itemKey: self.itemKey ?? "",
dataSource: self.dataSource ?? ""
Expand All @@ -39,13 +40,13 @@ class BookmarkViewModel: ObservableObject {

@MainActor
func updateBookmarked() {
bookmark = repository?.getBookmark(itemKey: self.itemKey ?? "", dataSource: self.dataSource ?? "")
bookmark = repository.getBookmark(itemKey: self.itemKey ?? "", dataSource: self.dataSource ?? "")
self.isBookmarked = bookmark != nil
self.bnotes = bookmark?.notes ?? ""
}

func removeBookmark() {
guard let itemKey = itemKey, let dataSource = dataSource, let repository = repository else {
guard let itemKey = itemKey, let dataSource = dataSource else {
return
}
repository.removeBookmark(itemKey: itemKey, dataSource: dataSource)
Expand Down
12 changes: 6 additions & 6 deletions Marlin/Marlin/UI/Bookmark/BookmarksViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ class BookmarksViewModel: ObservableObject {
@Published var loaded: Bool = false
private var disposables = Set<AnyCancellable>()

var repository: BookmarkRepository? {
didSet {
fetchBookmarks()
}
}
@Injected(\.bookmarkRepository)
private var repository: BookmarkRepository

var publisher: AnyPublisher<CollectionDifference<BookmarkModel>, Never>?

Expand All @@ -38,6 +35,10 @@ class BookmarksViewModel: ObservableObject {
}
}
}

init() {
fetchBookmarks()
}

private enum TriggerId: Hashable {
case reload
Expand All @@ -56,7 +57,6 @@ class BookmarksViewModel: ObservableObject {
if publisher != nil {
return
}
guard let repository = repository else { return }
Publishers.PublishAndRepeat(
onOutputFrom: trigger.signal(activatedBy: TriggerId.reload)
) { [trigger, repository] in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct DGPSStationSummaryView: DataSourceSummaryView {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter
var showBookmarkNotes: Bool = false

Expand Down Expand Up @@ -66,7 +65,6 @@ struct DGPSStationSummaryView: DataSourceSummaryView {
)
}
.onAppear {
bookmarkViewModel.repository = bookmarkRepository
bookmarkViewModel.getBookmark(itemKey: dgpsStation.id, dataSource: DataSources.dgps.key)
}
}
Expand Down
1 change: 0 additions & 1 deletion Marlin/Marlin/UI/Light/LightDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import MapKit
import CoreData

struct LightDetailView: View {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter
@StateObject var viewModel: LightViewModel = LightViewModel()
@State var featureNumber: String
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/Light/LightSummaryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct LightSummaryView: DataSourceSummaryView {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter

var showSectionHeader: Bool = false
Expand Down Expand Up @@ -54,7 +53,6 @@ struct LightSummaryView: DataSourceSummaryView {
)
}
.onAppear {
bookmarkViewModel.repository = bookmarkRepository
bookmarkViewModel.getBookmark(itemKey: light.itemKey, dataSource: DataSources.light.key)
}
}
Expand Down
2 changes: 0 additions & 2 deletions Marlin/Marlin/UI/MODU/ModuDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import MapKit
import CoreData

struct ModuDetailView: View {
@EnvironmentObject var bookmarkRepository: BookmarkRepository
@EnvironmentObject var router: MarlinRouter
@StateObject var viewModel: ModuViewModel = ModuViewModel()
@State var name: String
Expand Down Expand Up @@ -95,7 +94,6 @@ struct ModuDetailView: View {
viewModel.getModu(name: name, waypointURI: waypointURI)
}
.onAppear {
bookmarkViewModel.repository = bookmarkRepository
bookmarkViewModel.getBookmark(itemKey: modu.id, dataSource: DataSources.modu.key)
Metrics.shared.dataSourceDetail(dataSource: DataSources.modu)
}
Expand Down
Loading

0 comments on commit 534c6be

Please sign in to comment.