Skip to content

Commit

Permalink
Merge pull request #555 from Automattic/project-eoy-376/remove-share-…
Browse files Browse the repository at this point in the history
…buttons
  • Loading branch information
emilylaguna authored Nov 30, 2022
2 parents a47d377 + d671d36 commit f39fd83
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 44 deletions.
4 changes: 4 additions & 0 deletions PocketCastsTests/Tests/End of Year/StoriesModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class MockStoriesDataSource: StoriesDataSource {
}
}

func shareableStory(for storyNumber: Int) -> (any ShareableStory)? {
nil
}

func isReady() async -> Bool {
true
}
Expand Down
4 changes: 4 additions & 0 deletions podcasts/End of Year/Stories/EndOfYearStoriesDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class EndOfYearStoriesDataSource: StoriesDataSource {
}
}

func shareableStory(for storyNumber: Int) -> (any ShareableStory)? {
story(for: storyNumber) as? (any ShareableStory)
}

/// The only interactive view we have is the last one, with the replay button
func interactiveView(for storyNumber: Int) -> AnyView {
switch stories[storyNumber] {
Expand Down
11 changes: 0 additions & 11 deletions podcasts/End of Year/Stories/EpilogueStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ struct EpilogueStory: StoryView {
func onAppear() {
Analytics.track(.endOfYearStoryShown, story: identifier)
}

func willShare() {
Analytics.track(.endOfYearStoryShare, story: identifier)
}

func sharingAssets() -> [Any] {
[
StoryShareableProvider.new(AnyView(self)),
StoryShareableText("")
]
}
}

struct ReplayButtonStyle: ButtonStyle {
Expand Down
11 changes: 0 additions & 11 deletions podcasts/End of Year/Stories/IntroStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ struct IntroStory: StoryView {
Analytics.track(.endOfYearStoryShown, story: identifier)
}

func willShare() {
Analytics.track(.endOfYearStoryShare, story: identifier)
}

func sharingAssets() -> [Any] {
[
StoryShareableProvider.new(AnyView(self)),
StoryShareableText("")
]
}

private struct Constants {
static let imageVerticalPadding: CGFloat = 60
static let imageHeightInPercentage: CGFloat = 0.54
Expand Down
2 changes: 1 addition & 1 deletion podcasts/End of Year/Stories/ListenedCategoriesStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import PocketCastsServer
import PocketCastsDataModel

struct ListenedCategoriesStory: StoryView {
struct ListenedCategoriesStory: ShareableStory {
@Environment(\.renderForSharing) var renderForSharing: Bool
var duration: TimeInterval = 5.seconds

Expand Down
2 changes: 1 addition & 1 deletion podcasts/End of Year/Stories/ListenedNumbersStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import PocketCastsServer
import PocketCastsDataModel

struct ListenedNumbersStory: StoryView {
struct ListenedNumbersStory: ShareableStory {
@Environment(\.renderForSharing) var renderForSharing: Bool

var duration: TimeInterval = 5.seconds
Expand Down
2 changes: 1 addition & 1 deletion podcasts/End of Year/Stories/ListeningTimeStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import PocketCastsServer
import PocketCastsDataModel

struct ListeningTimeStory: StoryView {
struct ListeningTimeStory: ShareableStory {
var duration: TimeInterval = 5.seconds

let identifier: String = "listening_time"
Expand Down
2 changes: 1 addition & 1 deletion podcasts/End of Year/Stories/LongestEpisodeStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import PocketCastsServer
import PocketCastsDataModel

struct LongestEpisodeStory: StoryView {
struct LongestEpisodeStory: ShareableStory {
let duration: TimeInterval = 5.seconds

var identifier: String = "longest_episode"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI
import PocketCastsDataModel

struct TopListenedCategoriesStory: StoryView {
struct TopListenedCategoriesStory: ShareableStory {
var duration: TimeInterval = 5.seconds

let identifier: String = "top_categories"
Expand Down
2 changes: 1 addition & 1 deletion podcasts/End of Year/Stories/TopFivePodcastsStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import PocketCastsServer
import PocketCastsDataModel

struct TopFivePodcastsStory: StoryView {
struct TopFivePodcastsStory: ShareableStory {
let podcasts: [Podcast]

let identifier: String = "top_five_podcast"
Expand Down
2 changes: 1 addition & 1 deletion podcasts/End of Year/Stories/TopOnePodcastStory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import PocketCastsServer
import PocketCastsDataModel

struct TopOnePodcastStory: StoryView {
struct TopOnePodcastStory: ShareableStory {
var duration: TimeInterval = 5.seconds

let identifier: String = "top_one_podcast"
Expand Down
25 changes: 18 additions & 7 deletions podcasts/End of Year/StoriesDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ protocol StoriesDataSource {
func story(for: Int) -> any StoryView
func storyView(for: Int) -> AnyView

/// Returns a story that supports being shared, or nil if it doesn't
func shareableStory(for: Int) -> (any ShareableStory)?

/// An interactive view that is put on top of the Stories control
///
/// This allows having interactive elements, such as buttons.
Expand Down Expand Up @@ -33,6 +36,7 @@ extension StoriesDataSource {
}
}

// MARK: - Story Views
typealias StoryView = Story & View

protocol Story {
Expand All @@ -50,7 +54,20 @@ protocol Story {
/// This method instead will only be called when the story
/// is being presented.
func onAppear()
}

extension Story {
var identifier: String {
"unknown"
}

func onAppear() {}
}

// MARK: - Shareable Stories
typealias ShareableStory = StoryView & StorySharing

protocol StorySharing {
/// Called when the story will be shared
func willShare()

Expand All @@ -60,13 +77,7 @@ protocol Story {
func sharingAssets() -> [Any]
}

extension Story {
var identifier: String {
"unknown"
}

func onAppear() {}

extension StorySharing {
func willShare() {}

func sharingAssets() -> [Any] {
Expand Down
15 changes: 12 additions & 3 deletions podcasts/End of Year/StoriesModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class StoriesModel: ObservableObject {
return AnyView(story)
}

func storyIsShareable(index: Int) -> Bool {
dataSource.shareableStory(for: index) != nil ? true : false
}

func preload(index: Int) -> AnyView {
if index < numberOfStories {
return AnyView(dataSource.story(for: index))
Expand All @@ -86,8 +90,11 @@ class StoriesModel: ObservableObject {
return AnyView(EmptyView())
}

func sharingAssets() -> [Any] {
let story = dataSource.story(for: currentStory)
func sharingAssets() -> [Any]? {
guard let story = dataSource.shareableStory(for: currentStory) else {
return nil
}

story.willShare()

// If any of the assets have additional handlers then make sure we add them to the array
Expand Down Expand Up @@ -132,8 +139,10 @@ class StoriesModel: ObservableObject {
}

func share() {
guard let assets = sharingAssets() else { return }

pause()
EndOfYear().share(assets: sharingAssets(), storyIdentifier: currentStoryIdentifier, onDismiss: { [weak self] in
EndOfYear().share(assets: assets, storyIdentifier: currentStoryIdentifier, onDismiss: { [weak self] in
self?.start()
})
}
Expand Down
13 changes: 8 additions & 5 deletions podcasts/End of Year/Views/StoriesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ struct StoriesView: View {
header
}

ZStack {}
.frame(height: Constants.spaceBetweenShareAndStory)
// Hide the share button if needed
if model.storyIsShareable(index: model.currentStory) {
ZStack {}
.frame(height: Constants.spaceBetweenShareAndStory)

shareButton
shareButton
}
}
.background(Color.black)
}
Expand Down Expand Up @@ -132,13 +135,13 @@ struct StoriesView: View {
.contentShape(Rectangle())
.onTapGesture {
model.previous()
}
}
Rectangle()
.foregroundColor(.clear)
.contentShape(Rectangle())
.onTapGesture {
model.next()
}
}
}
.simultaneousGesture(
DragGesture(minimumDistance: 0, coordinateSpace: .local)
Expand Down

0 comments on commit f39fd83

Please sign in to comment.