Skip to content

Commit

Permalink
Duplicate widget target
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Oct 31, 2023
1 parent bd332a7 commit 13d4ab1
Show file tree
Hide file tree
Showing 15 changed files with 592 additions and 158 deletions.
243 changes: 239 additions & 4 deletions BookPlayer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

44 changes: 16 additions & 28 deletions BookPlayerWidgetUI/BookPlayerWidgetUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,28 @@
// Copyright © 2020 Tortuga Power. All rights reserved.
//

#if os(watchOS)
import BookPlayerWatchKit
#else
import BookPlayerKit
#endif
import SwiftUI
import WidgetKit

struct SimpleEntry: TimelineEntry {
let date: Date
let title: String?
let relativePath: String?
let theme: SimpleTheme?
let timerSeconds: Double
let autoplay: Bool
}

struct LibraryEntry: TimelineEntry {
let date: Date
let items: [SimpleLibraryItem]
let theme: SimpleTheme?
let timerSeconds: Double
let autoplay: Bool
}

struct TimeListenedEntry: TimelineEntry {
let date: Date
let title: String?
let theme: SimpleTheme?
let timerSeconds: Double
let autoplay: Bool
let playbackRecords: [PlaybackRecordViewer]
}

// TODO: Setup shared watch widgets
#if os(iOS)
struct BookPlayerWidgetUI_Previews: PreviewProvider {
static var previews: some View {
Group {
LastPlayedWidgetView(entry: SimpleEntry(date: Date(), title: "Test Book Title", relativePath: nil, theme: nil, timerSeconds: 300, autoplay: true))
.previewContext(WidgetPreviewContext(family: .systemSmall))

LastPlayedWidgetView(entry: SimpleEntry(
date: Date(),
title: "Test Book Title",
relativePath: nil,
timerSeconds: 300,
autoplay: true
))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
}
Expand All @@ -54,3 +41,4 @@ struct BookPlayerBundle: WidgetBundle {
TimeListenedWidget()
}
}
#endif
File renamed without changes.
181 changes: 102 additions & 79 deletions BookPlayerWidgetUI/LastPlayedWidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct PlayAndSleepProvider: IntentTimelineProvider {
date: Date(),
title: "Last played book title",
relativePath: nil,
theme: SimpleTheme.getDefaultTheme(),
timerSeconds: 300,
autoplay: true
)
Expand Down Expand Up @@ -76,93 +75,117 @@ struct PlayAndSleepProvider: IntentTimelineProvider {
}

struct LastPlayedWidgetView: View {
@Environment(\.colorScheme) var colorScheme
var entry: PlayAndSleepProvider.Entry

var body: some View {
let titleLabel = entry.title ?? "---"

let widgetColors = WidgetUtils.getColors(from: entry.theme, with: colorScheme)

let url = WidgetUtils.getWidgetActionURL(with: entry.relativePath, autoplay: entry.autoplay, timerSeconds: entry.timerSeconds)

let appIconName = WidgetUtils.getAppIconName()

return VStack {
HStack {
if let relativePath = entry.relativePath {
Image(uiImage: UIImage(contentsOfFile: ArtworkService.getCachedImageURL(for: relativePath).path)
?? ArtworkService.generateDefaultArtwork(from: entry.theme?.linkColor)!)
.resizable()
.frame(width: 90, height: 90)
.aspectRatio(1.0, contentMode: .fit)
.cornerRadius(8.0)
} else {
Rectangle()
.fill(Color.secondary)
.frame(width: 90, height: 90)
.aspectRatio(1.0, contentMode: .fit)
.cornerRadius(8.0)
}

VStack {
Image(appIconName)
.accessibility(hidden: true)
.frame(width: 32, height: 32)
.padding([.trailing], 10)
.cornerRadius(8.0)
Spacer()
}
}
.frame(height: 90)
.padding([.leading])
.padding([.top], 8)
.accessibility(label: Text("Last Played Book, \(titleLabel)"))
VStack(alignment: .leading) {
Text(titleLabel)
.fontWeight(.semibold)
.foregroundColor(widgetColors.primaryColor)
.font(.footnote)
.lineLimit(2)
.accessibility(hidden: true)
Spacer()
}
.frame(height: 40)
.padding([.leading, .trailing])
@Environment(\.colorScheme) var colorScheme
var entry: PlayAndSleepProvider.Entry

var body: some View {
let titleLabel = entry.title ?? "---"

let widgetColors = WidgetUtils.getColors(from: entry.theme, with: colorScheme)

let url = WidgetUtils.getWidgetActionURL(with: entry.relativePath, autoplay: entry.autoplay, timerSeconds: entry.timerSeconds)

let appIconName = WidgetUtils.getAppIconName()

return VStack {
HStack {
if let relativePath = entry.relativePath {
Image(uiImage: UIImage(contentsOfFile: ArtworkService.getCachedImageURL(for: relativePath).path)
?? ArtworkService.generateDefaultArtwork(from: entry.theme.linkColor)!)
.resizable()
.frame(width: 90, height: 90)
.aspectRatio(1.0, contentMode: .fit)
.cornerRadius(8.0)
} else {
Rectangle()
.fill(Color.secondary)
.frame(width: 90, height: 90)
.aspectRatio(1.0, contentMode: .fit)
.cornerRadius(8.0)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.widgetBackground(backgroundView: widgetColors.backgroundColor)
.widgetURL(url)

VStack {
Image(appIconName)
.accessibility(hidden: true)
.frame(width: 32, height: 32)
.padding([.trailing], 10)
.cornerRadius(8.0)
Spacer()
}
}
.frame(height: 90)
.padding([.leading])
.padding([.top], 8)
.accessibility(label: Text("Last Played Book, \(titleLabel)"))
VStack(alignment: .leading) {
Text(titleLabel)
.fontWeight(.semibold)
.foregroundColor(widgetColors.primaryColor)
.font(.footnote)
.lineLimit(2)
.accessibility(hidden: true)
Spacer()
}
.frame(height: 40)
.padding([.leading, .trailing])
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.widgetBackground(backgroundView: widgetColors.backgroundColor)
.widgetURL(url)
}
}

struct LastPlayedWidgetView_Previews: PreviewProvider {
static var previews: some View {
Group {
LastPlayedWidgetView(entry: SimpleEntry(date: Date(), title: "Test Book Title", relativePath: nil, theme: nil, timerSeconds: 300, autoplay: true))
.previewContext(WidgetPreviewContext(family: .systemSmall))
LastPlayedWidgetView(entry: SimpleEntry(date: Date(), title: nil, relativePath: nil, theme: nil, timerSeconds: 300, autoplay: true))
.previewContext(WidgetPreviewContext(family: .systemSmall))
LastPlayedWidgetView(entry: SimpleEntry(date: Date(), title: "Test Book Title", relativePath: nil, theme: nil, timerSeconds: 300, autoplay: true))
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
LastPlayedWidgetView(entry: SimpleEntry(date: Date(), title: nil, relativePath: nil, theme: nil, timerSeconds: 300, autoplay: true))
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
}
static var previews: some View {
Group {
LastPlayedWidgetView(entry: SimpleEntry(
date: Date(),
title: "Test Book Title",
relativePath: nil,
timerSeconds: 300,
autoplay: true
))
.previewContext(WidgetPreviewContext(family: .systemSmall))
LastPlayedWidgetView(entry: SimpleEntry(
date: Date(),
title: nil,
relativePath: nil,
timerSeconds: 300,
autoplay: true
))
.previewContext(WidgetPreviewContext(family: .systemSmall))
LastPlayedWidgetView(entry: SimpleEntry(
date: Date(),
title: "Test Book Title",
relativePath: nil,
timerSeconds: 300,
autoplay: true
))
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
LastPlayedWidgetView(entry: SimpleEntry(
date: Date(),
title: nil,
relativePath: nil,
timerSeconds: 300,
autoplay: true
))
.previewContext(WidgetPreviewContext(family: .systemSmall))
.environment(\.colorScheme, .dark)
}
}
}

struct LastPlayedWidget: Widget {
let kind: String = "com.bookplayer.widget.small.lastPlayed"
let kind: String = "com.bookplayer.widget.small.lastPlayed"

var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: PlayAndSleepActionIntent.self, provider: PlayAndSleepProvider()) { entry in
LastPlayedWidgetView(entry: entry)
}
.configurationDisplayName("Last Played Book")
.description("See and play your last played book")
.supportedFamilies([.systemSmall])
.contentMarginsDisabledIfAvailable()
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: PlayAndSleepActionIntent.self, provider: PlayAndSleepProvider()) { entry in
LastPlayedWidgetView(entry: entry)
}
.configurationDisplayName("Last Played Book")
.description("See and play your last played book")
.supportedFamilies([.systemSmall])
.contentMarginsDisabledIfAvailable()
}
}
22 changes: 11 additions & 11 deletions BookPlayerWidgetUI/RecentBooksWidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct RecentBooksProvider: IntentTimelineProvider {
SimpleLibraryItem.previewItem(title: "Book title"),
SimpleLibraryItem.previewItem(title: "Book title")
],
theme: SimpleTheme.getDefaultTheme(),
timerSeconds: 300,
autoplay: true
)
Expand Down Expand Up @@ -100,7 +99,7 @@ struct BookView: View {
return Link(destination: url) {
VStack(spacing: 5) {
Image(uiImage: UIImage(contentsOfFile: cachedImageURL.path)
?? ArtworkService.generateDefaultArtwork(from: entry.theme?.linkColor)!)
?? ArtworkService.generateDefaultArtwork(from: entry.theme.linkColor)!)
.resizable()
.frame(minWidth: 60, maxWidth: 60, minHeight: 60, maxHeight: 60)
.aspectRatio(1.0, contentMode: .fit)
Expand Down Expand Up @@ -163,15 +162,16 @@ 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")],
theme: nil,
timerSeconds: 300,
autoplay: true))
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")
],
timerSeconds: 300,
autoplay: true))
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
Expand Down
14 changes: 12 additions & 2 deletions BookPlayerWidgetUI/TimeListened/BarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import SwiftUI
import WidgetKit
import BookPlayerKit

struct BarView: View {
var currentTime: Double
Expand Down Expand Up @@ -52,7 +53,16 @@ struct BarView: View {

struct BarView_Previews: PreviewProvider {
static var previews: some View {
BarView(currentTime: 20, date: Date(), maxTime: 70, cornerRadius: CGFloat(integerLiteral: 7), widgetColors: WidgetUtils.getColors(from: nil, with: .light))
.previewContext(WidgetPreviewContext(family: .systemMedium))
BarView(
currentTime: 20,
date: Date(),
maxTime: 70,
cornerRadius: CGFloat(integerLiteral: 7),
widgetColors: WidgetUtils.getColors(
from: SimpleTheme.getDefaultTheme(),
with: .light
)
)
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
10 changes: 8 additions & 2 deletions BookPlayerWidgetUI/TimeListened/TimeListenedMediumView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ struct TimeListenedMediumView: View {

struct TimeListenedMediumView_Previews: PreviewProvider {
static var previews: some View {
TimeListenedMediumView(entry: TimeListenedEntry(date: Date(), title: nil, theme: nil, timerSeconds: 300, autoplay: true, playbackRecords: WidgetUtils.getTestDataPlaybackRecords(.systemMedium)))
.previewContext(WidgetPreviewContext(family: .systemMedium))
TimeListenedMediumView(entry: TimeListenedEntry(
date: Date(),
title: nil,
timerSeconds: 300,
autoplay: true,
playbackRecords: WidgetUtils.getTestDataPlaybackRecords(.systemMedium)
))
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
10 changes: 8 additions & 2 deletions BookPlayerWidgetUI/TimeListened/TimeListenedSmallView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ struct TimeListenedSmallView: View {

struct TimeListenedSmallView_Previews: PreviewProvider {
static var previews: some View {
TimeListenedSmallView(entry: TimeListenedEntry(date: Date(), title: nil, theme: nil, timerSeconds: 300, autoplay: true, playbackRecords: WidgetUtils.getTestDataPlaybackRecords(.systemSmall)))
.previewContext(WidgetPreviewContext(family: .systemSmall))
TimeListenedSmallView(entry: TimeListenedEntry(
date: Date(),
title: nil,
timerSeconds: 300,
autoplay: true,
playbackRecords: WidgetUtils.getTestDataPlaybackRecords(.systemSmall)
))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
Loading

0 comments on commit 13d4ab1

Please sign in to comment.