Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessy Catterwaul committed Apr 1, 2022
1 parent aca8c17 commit 3db7bbb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
8 changes: 5 additions & 3 deletions Emitron/Emitron/Data/Repository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ extension Repository {
return fromCache
.combineLatest(download)
.map { cachedState, download in
DynamicContentState(download: download,
progression: cachedState.progression,
bookmark: cachedState.bookmark)
DynamicContentState(
download: download,
progression: cachedState.progression,
bookmark: cachedState.bookmark
)
}
.removeDuplicates()
.eraseToAnyPublisher()
Expand Down
6 changes: 4 additions & 2 deletions Emitron/Emitron/Data/ViewModels/ChildContentsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ extension ChildContentsViewModel {
.sink(
receiveCompletion: { [weak self] completion in
guard let self = self else { return }
if case .failure(let error) = completion, (error as? DataCacheError) == DataCacheError.cacheMiss {

switch completion {
case .failure(let error as DataCacheError) where error == .cacheMiss:
self.loadContentDetailsIntoCache()
} else {
default:
self.state = .failed
Failure
.repositoryLoad(from: Self.self, reason: "Unable to retrieve download content detail: \(completion)")
Expand Down
75 changes: 34 additions & 41 deletions Emitron/Emitron/Models/DataCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ extension DataCache {

extension DataCache {
private func cachedContentSummaryState(for contentID: Int) throws -> CachedContentSummaryState {
guard let content = contents[contentID],
let contentDomains = contentDomains[contentID]
guard
let content = contents[contentID],
let contentDomains = contentDomains[contentID]
else {
throw DataCacheError.cacheMiss
}

let contentCategories = self.contentCategories[contentID] ?? []
let contentCategories = contentCategories[contentID] ?? []

return try CachedContentSummaryState(
content: content,
Expand Down Expand Up @@ -227,34 +228,31 @@ extension DataCache {
throw DataCacheError.cacheMiss
}

let contentDomains = self.contentDomains[contentID] ?? []
let contentCategories = self.contentCategories[contentID] ?? []

if content.contentType != .episode {
if contentDomains.isEmpty {
throw DataCacheError.cacheMiss
}
}

let bookmark = self.bookmarks[contentID]
let progression = self.progressions[contentID]
let groups = self.contentIndexedGroups[contentID] ?? []
let groupIDs = groups.map(\.id)
let childContents = self.contents.values.filter { content in
guard let groupID = content.groupID else { return false }
return groupIDs.contains(groupID)
}

return try ContentPersistableState(
content: content,
contentDomains: contentDomains,
contentCategories: contentCategories,
bookmark: bookmark,
parentContent: parentContent(for: content),
progression: progression,
groups: groups,
childContents: childContents
)
let contentDomains = self.contentDomains[contentID] ?? []
let contentCategories = self.contentCategories[contentID] ?? []

if content.contentType != .episode, contentDomains.isEmpty {
throw DataCacheError.cacheMiss
}

let bookmark = bookmarks[contentID]
let progression = progressions[contentID]
let groups = contentIndexedGroups[contentID] ?? []
let groupIDs = groups.map(\.id)
let childContents = contents.values.filter { content in
content.groupID.map(groupIDs.contains) == true
}

return try .init(
content: content,
contentDomains: contentDomains,
contentCategories: contentCategories,
bookmark: bookmark,
parentContent: parentContent(for: content),
progression: progression,
groups: groups,
childContents: childContents
)
}

func videoPlaylist(for contentID: Int) throws -> [CachedVideoPlaybackState] {
Expand Down Expand Up @@ -295,7 +293,7 @@ extension DataCache {
}

private func cachedDynamicContentState(for contentID: Int) -> CachedDynamicContentState {
CachedDynamicContentState(
.init(
progression: progressions[contentID],
bookmark: bookmarks[contentID]
)
Expand All @@ -304,7 +302,7 @@ extension DataCache {
private func parentContent(for content: Content) throws -> Content? {
guard let groupID = content.groupID else { return nil }
guard let group = groupIndexedGroups[groupID]
else { throw DataCacheError.cacheMiss }
else { throw DataCacheError.cacheMiss }

return contents[group.contentID]
}
Expand All @@ -326,10 +324,7 @@ extension DataCache {
}

private func siblingContents(for content: Content) throws -> [Content] {
guard let parentContent = try parentContent(for: content) else {
return []
}
return try childContents(for: parentContent)
try parentContent(for: content).map(childContents) ?? []
}

private func nextToPlay(for contentList: [Content]) throws -> Content {
Expand All @@ -339,10 +334,8 @@ extension DataCache {
let orderedProgressions = contentList.map { progressions[$0.id] }

// Find the first index where there's a missing or incomplete progression
guard let incompleteOrNotStartedIndex = orderedProgressions.firstIndex(where: { progression in
guard let progression = progression else { return true }

return !progression.finished
guard let incompleteOrNotStartedIndex = (orderedProgressions.firstIndex { progression in
progression.map { !$0.finished } ?? true
}) else {
// If we didn't find one, start at the beginning
return contentList[0]
Expand Down

0 comments on commit 3db7bbb

Please sign in to comment.