Skip to content

Commit

Permalink
Rework recent books widget to use same provider as last played
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Oct 1, 2024
1 parent 658f982 commit 1fed5e4
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 74 deletions.
8 changes: 4 additions & 4 deletions BookPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@
639720752CAAFB010045A4DB /* WidgetLibraryItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639720732CAAFB010045A4DB /* WidgetLibraryItem.swift */; };
639720832CAB0C380045A4DB /* LastPlayedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639720822CAB0C380045A4DB /* LastPlayedView.swift */; };
639720852CABB0D00045A4DB /* RecentBooksProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639720842CABB0D00045A4DB /* RecentBooksProvider.swift */; };
639720862CABB0D00045A4DB /* RecentBooksProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639720842CABB0D00045A4DB /* RecentBooksProvider.swift */; };
6397208A2CAC5C870045A4DB /* LastPlayedModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639720892CAC5C870045A4DB /* LastPlayedModel.swift */; };
6399F94D2AA03C6C00A5C8EA /* BPSKANManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6399F94C2AA03C6C00A5C8EA /* BPSKANManager.swift */; };
639AC9892AD9F1D50053AFC6 /* BPDownloadURLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639AC9882AD9F1D50053AFC6 /* BPDownloadURLSession.swift */; };
639AC98A2AD9F1D50053AFC6 /* BPDownloadURLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639AC9882AD9F1D50053AFC6 /* BPDownloadURLSession.swift */; };
Expand Down Expand Up @@ -417,7 +417,6 @@
63B760FC2C33B77F00AA98C7 /* SupportProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63B760FB2C33B77F00AA98C7 /* SupportProfileView.swift */; };
63C1A8AF2B09158600C4B418 /* BookStartPlaybackIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 634E67432AFB2DF500595BAC /* BookStartPlaybackIntent.swift */; };
63C1A8B02B0915EE00C4B418 /* WidgetUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 418445C2258AE11E0072DD13 /* WidgetUtils.swift */; };
63C1A8B12B09165400C4B418 /* RecentBooksWidgetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41ADD6D92570AC6300660C64 /* RecentBooksWidgetView.swift */; };
63C1A8B22B09166F00C4B418 /* WidgetEntries.swift in Sources */ = {isa = PBXBuildFile; fileRef = 637DAB092AEB3E0D006DC2D1 /* WidgetEntries.swift */; };
63C6C2E62B5029BC00FFE0D8 /* SettingsAutolockView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63C6C2E52B5029BC00FFE0D8 /* SettingsAutolockView.swift */; };
63C6C2E82B5029FE00FFE0D8 /* SettingsAutolockViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63C6C2E72B5029FE00FFE0D8 /* SettingsAutolockViewModel.swift */; };
Expand Down Expand Up @@ -1170,6 +1169,7 @@
639720732CAAFB010045A4DB /* WidgetLibraryItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetLibraryItem.swift; sourceTree = "<group>"; };
639720822CAB0C380045A4DB /* LastPlayedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LastPlayedView.swift; sourceTree = "<group>"; };
639720842CABB0D00045A4DB /* RecentBooksProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecentBooksProvider.swift; sourceTree = "<group>"; };
639720892CAC5C870045A4DB /* LastPlayedModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LastPlayedModel.swift; sourceTree = "<group>"; };
6399F94C2AA03C6C00A5C8EA /* BPSKANManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BPSKANManager.swift; sourceTree = "<group>"; };
639AC9882AD9F1D50053AFC6 /* BPDownloadURLSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BPDownloadURLSession.swift; sourceTree = "<group>"; };
639E12C52B85AACF00C875F7 /* SyncTasksObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncTasksObject.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2236,6 +2236,7 @@
children = (
417D9993256DE3FB00C3B753 /* LastPlayedWidgetView.swift */,
639720822CAB0C380045A4DB /* LastPlayedView.swift */,
639720892CAC5C870045A4DB /* LastPlayedModel.swift */,
639720712CAAF8290045A4DB /* LastPlayedProvider.swift */,
);
path = LastPlayed;
Expand Down Expand Up @@ -3455,6 +3456,7 @@
630826062AF525F1002ACE0D /* SharedWidgetContainerView.swift in Sources */,
4106414925872699008EB8D0 /* TimeListenedSmallView.swift in Sources */,
417D996F256D73B400C3B753 /* Intents.intentdefinition in Sources */,
6397208A2CAC5C870045A4DB /* LastPlayedModel.swift in Sources */,
417D9994256DE3FB00C3B753 /* LastPlayedWidgetView.swift in Sources */,
41A359C7276232E00020D5F5 /* MappingModel_v7_to_v8.xcmappingmodel in Sources */,
416A29AA2569658100605395 /* BookPlayerWidgets.swift in Sources */,
Expand Down Expand Up @@ -3629,7 +3631,6 @@
9F5FBB08293EDCD8009F4B0E /* ItemDetailsViewController.swift in Sources */,
634BA5AD2C180F5E0015314D /* StoryViewModel.swift in Sources */,
9F2681B628898A7300359BD3 /* LoginDisclaimerView.swift in Sources */,
63C1A8B12B09165400C4B418 /* RecentBooksWidgetView.swift in Sources */,
9FD8D95829DC53750074C2D8 /* CoreServices.swift in Sources */,
9F4691F22800D58A00A8F0E8 /* CompleteAccountCoordinator.swift in Sources */,
9F00A5F6294F793F005EA316 /* ItemDetailsView.swift in Sources */,
Expand Down Expand Up @@ -3675,7 +3676,6 @@
41188D2A26ED4D8E0017124E /* ItemListViewModel.swift in Sources */,
C398559C20C492FF00BE9EC0 /* AddButton.swift in Sources */,
9F89D89C27EDFCA400F73947 /* SceneDelegate.swift in Sources */,
639720862CABB0D00045A4DB /* RecentBooksProvider.swift in Sources */,
9F3D0CE928C2BFC600E9E8A3 /* ButtonFreeCoordinator.swift in Sources */,
416AAC3323F51031005AD04F /* LocalizableButton.swift in Sources */,
63B50F052B692E4200BCABBA /* ListSyncRefreshService.swift in Sources */,
Expand Down
40 changes: 29 additions & 11 deletions BookPlayer/Services/ActionParserService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class ActionParserService {
}

if let valueString = action.getQueryValue(for: "interval"),
let interval = Double(valueString) {
let interval = Double(valueString)
{
playerManager.skip(-interval)
} else {
playerManager.rewind()
Expand All @@ -98,7 +99,8 @@ class ActionParserService {
}

if let valueString = action.getQueryValue(for: "interval"),
let interval = Double(valueString) {
let interval = Double(valueString)
{
playerManager.skip(interval)
} else {
playerManager.forward()
Expand Down Expand Up @@ -169,7 +171,8 @@ class ActionParserService {

private class func handleSleepAction(_ action: Action) {
guard let value = action.getQueryValue(for: "seconds"),
let seconds = Double(value) else {
let seconds = Double(value)
else {
return
}

Expand All @@ -190,8 +193,17 @@ class ActionParserService {
return
}

self.removeAction(action)
playerManager.playPause()
if let bookIdentifier = action.getQueryValue(for: "identifier"),
playerManager.currentItem?.relativePath != bookIdentifier
{
if let libraryCoordinator = AppDelegate.shared?.activeSceneDelegate?.mainCoordinator?.getLibraryCoordinator() {
self.removeAction(action)
libraryCoordinator.loadPlayer(bookIdentifier)
}
} else {
self.removeAction(action)
playerManager.playPause()
}
}

private class func handlePauseAction(_ action: Action) {
Expand All @@ -213,14 +225,16 @@ class ActionParserService {
}

if let value = action.getQueryValue(for: "showPlayer"),
let showPlayer = Bool(value),
showPlayer {
let showPlayer = Bool(value),
showPlayer
{
AppDelegate.shared?.showPlayer()
}

if let value = action.getQueryValue(for: "autoplay"),
let autoplay = Bool(value),
!autoplay {
let autoplay = Bool(value),
!autoplay
{
return
}

Expand All @@ -231,7 +245,8 @@ class ActionParserService {
}

if let loadedItem = playerManager.currentItem,
loadedItem.relativePath == bookIdentifier {
loadedItem.relativePath == bookIdentifier
{
self.removeAction(action)
playerManager.play()
return
Expand All @@ -254,7 +269,10 @@ class ActionParserService {
}

guard let url = URL(string: urlString) else {
libraryCoordinator.showAlert("error_title".localized, message: String.localizedStringWithFormat("invalid_url_title".localized, urlString))
libraryCoordinator.showAlert(
"error_title".localized,
message: String.localizedStringWithFormat("invalid_url_title".localized, urlString)
)
return
}

Expand Down
1 change: 0 additions & 1 deletion BookPlayerWidgets/BookPlayerWidgets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ struct BookPlayerBundle: WidgetBundle {
var body: some Widget {
#if os(iOS)
LastPlayedWidget()
RecentBooksWidget()
TimeListenedWidget()
if #available(iOSApplicationExtension 16.1, *) {
SharedWidget()
Expand Down
24 changes: 24 additions & 0 deletions BookPlayerWidgets/Phone/LastPlayed/LastPlayedModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// LastPlayedModel.swift
// BookPlayerWidgetsPhone
//
// Created by Gianni Carlo on 1/10/24.
// Copyright © 2024 Tortuga Power. All rights reserved.
//

import BookPlayerKit
import Foundation

struct LastPlayedModel {
let relativePath: String?
let title: String?
let isPlaying: Bool
let theme: SimpleTheme

init(item: WidgetLibraryItem?, isPlaying: Bool, theme: SimpleTheme) {
self.relativePath = item?.relativePath
self.title = item?.title
self.isPlaying = isPlaying
self.theme = theme
}
}
16 changes: 1 addition & 15 deletions BookPlayerWidgets/Phone/LastPlayed/LastPlayedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ import BookPlayerKit
import SwiftUI
import WidgetKit

struct LastPlayedModel {
let relativePath: String?
let title: String?
let isPlaying: Bool
let theme: SimpleTheme

init(item: WidgetLibraryItem?, isPlaying: Bool, theme: SimpleTheme) {
self.relativePath = item?.relativePath
self.title = item?.title
self.isPlaying = isPlaying
self.theme = theme
}
}

struct LastPlayedView: View {
@Environment(\.colorScheme) var colorScheme
var model: LastPlayedModel
Expand Down Expand Up @@ -66,7 +52,7 @@ struct LastPlayedView: View {
.aspectRatio(contentMode: .fit)
.foregroundColor(.black)
.frame(width: 11, height: 11)
.offset(x: 1)
.offset(x: model.isPlaying ? 0 : 1)
}
}
.buttonStyle(.plain)
Expand Down
6 changes: 3 additions & 3 deletions BookPlayerWidgets/Phone/LastPlayed/LastPlayedWidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct LastPlayedWidgetView: View {
var body: some View {
switch widgetFamily {
case .systemMedium:
EmptyView()
RecentBooksWidgetView(entry: entry)
default:
LastPlayedView(
model: .init(
Expand All @@ -42,8 +42,8 @@ struct LastPlayedWidget: Widget {
LastPlayedWidgetView(entry: entry)
}
)
.configurationDisplayName("Last Played Book")
.description("See and play your last played book")
.configurationDisplayName("Last Played Books")
.description("See and play your last played books")
.supportedFamilies([.systemSmall, .systemMedium])
.contentMarginsDisabledIfAvailable()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,40 @@ import SwiftUI
import WidgetKit

struct BookView: View {
var item: SimpleLibraryItem
var item: WidgetLibraryItem
var titleColor: Color
var theme: SimpleTheme?
var entry: RecentBooksProvider.Entry
var theme: SimpleTheme
var isPlaying: Bool

var body: some View {
let title = item.title
let identifier = item.relativePath
let pauseImage: String? = isPlaying ? "pause.fill" : nil

let cachedImageURL = ArtworkService.getCachedImageURL(for: identifier)

return VStack(spacing: 5) {
Image(
uiImage: UIImage(contentsOfFile: cachedImageURL.path)
?? ArtworkService.generateDefaultArtwork(from: entry.theme.linkColor)!
)
.resizable()
.frame(minWidth: 60, maxWidth: 60, minHeight: 60, maxHeight: 60)
.aspectRatio(1.0, contentMode: .fit)
.cornerRadius(8.0)
ZStack {
Image(
uiImage: UIImage(contentsOfFile: cachedImageURL.path)
?? ArtworkService.generateDefaultArtwork(from: theme.linkColor)!
)
.resizable()
.frame(minWidth: 60, maxWidth: 60, minHeight: 60, maxHeight: 60)
.aspectRatio(1.0, contentMode: .fit)
.cornerRadius(8.0)
if let pauseImage {
Circle()
.foregroundColor(.white)
.frame(width: 30, height: 30)
.opacity(0.8)
Image(systemName: pauseImage)
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(.black)
.frame(width: 11, height: 11)
}
}

Text(title)
.fontWeight(.semibold)
Expand All @@ -45,7 +59,7 @@ struct BookView: View {

struct RecentBooksWidgetView: View {
@Environment(\.colorScheme) var colorScheme
var entry: RecentBooksProvider.Entry
var entry: LastPlayedProvider.Entry

var body: some View {
let items = Array(entry.items.prefix(4))
Expand Down Expand Up @@ -73,12 +87,12 @@ struct RecentBooksWidgetView: View {
HStack {
ForEach(items, id: \.relativePath) { item in
if #available(iOSApplicationExtension 17.0, iOS 17.0, *) {
Button(intent: BookStartPlaybackIntent(relativePath: item.relativePath)) {
Button(intent: BookPlaybackToggleIntent(relativePath: item.relativePath)) {
BookView(
item: item,
titleColor: widgetColors.primaryColor,
theme: entry.theme,
entry: entry
isPlaying: item.relativePath == entry.currentlyPlaying
)
.frame(minWidth: 0, maxWidth: .infinity)
}
Expand All @@ -88,7 +102,7 @@ struct RecentBooksWidgetView: View {
item: item,
titleColor: widgetColors.primaryColor,
theme: entry.theme,
entry: entry
isPlaying: item.relativePath == entry.currentlyPlaying
)
.frame(minWidth: 0, maxWidth: .infinity)
}
Expand All @@ -103,32 +117,13 @@ struct RecentBooksWidgetView: View {
}
}

struct RecentBooksWidgetView_Previews: PreviewProvider {
static var previews: some View {
Group {
RecentBooksWidgetView(
entry: LibraryEntry(
date: Date(),
items: [
.previewItem(title: "a very very very long title"),
.previewItem(title: "a short title"),
.previewItem(title: "a short title"),
.previewItem(title: "a short title"),
]
)
)
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
}

struct RecentBooksWidget: Widget {
let kind: String = "com.bookplayer.widget.medium.recentBooks"

var body: some WidgetConfiguration {
StaticConfiguration(
kind: kind,
provider: RecentBooksProvider(),
provider: LastPlayedProvider(),
content: { entry in
RecentBooksWidgetView(entry: entry)
}
Expand Down
Loading

0 comments on commit 1fed5e4

Please sign in to comment.