Skip to content

Commit

Permalink
Merge pull request #558 from Automattic/task/410-refactor-datasource-…
Browse files Browse the repository at this point in the history
…to-datamanager

End of Year: Refactor datasource
  • Loading branch information
geekygecko authored Nov 10, 2022
2 parents e07d885 + 5b05d2e commit ffc26ca
Show file tree
Hide file tree
Showing 31 changed files with 124 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import au.com.shiftyjelly.pocketcasts.analytics.FirebaseAnalyticsTracker
import au.com.shiftyjelly.pocketcasts.compose.AppTheme
import au.com.shiftyjelly.pocketcasts.databinding.ActivityMainBinding
import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesDataSource
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesPage
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesViewModel
import au.com.shiftyjelly.pocketcasts.endofyear.views.EndOfYearLaunchBottomSheet
Expand Down Expand Up @@ -164,7 +163,6 @@ class MainActivity :
@Inject lateinit var userEpisodeManager: UserEpisodeManager
@Inject lateinit var warningsHelper: WarningsHelper
@Inject lateinit var analyticsTracker: AnalyticsTrackerWrapper
@Inject lateinit var storiesDataSource: StoriesDataSource

private lateinit var bottomNavHideManager: BottomNavHideManager
private lateinit var observeUpNext: LiveData<UpNextQueue.State>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package au.com.shiftyjelly.pocketcasts.ui
import androidx.lifecycle.LiveData
import androidx.lifecycle.LiveDataReactiveStreams
import androidx.lifecycle.ViewModel
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesDataSource
import au.com.shiftyjelly.pocketcasts.models.to.SignInState
import au.com.shiftyjelly.pocketcasts.models.to.SubscriptionStatus
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.EndOfYearManager
import au.com.shiftyjelly.pocketcasts.repositories.playback.PlaybackManager
import au.com.shiftyjelly.pocketcasts.repositories.playback.PlaybackState
import au.com.shiftyjelly.pocketcasts.repositories.user.UserManager
Expand All @@ -22,7 +22,7 @@ class MainActivityViewModel
playbackManager: PlaybackManager,
userManager: UserManager,
val settings: Settings,
private val storiesDataSource: StoriesDataSource,
private val endOfYearManager: EndOfYearManager
) : ViewModel() {

var isPlayerOpen: Boolean = false
Expand Down Expand Up @@ -51,5 +51,5 @@ class MainActivityViewModel
return signInState.isExpiredTrial && !settings.getTrialFinishedSeen()
}

fun isEndOfYearStoriesEligible() = storiesDataSource.isEligibleForStories()
fun isEndOfYearStoriesEligible() = endOfYearManager.isEligibleForStories()
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ import au.com.shiftyjelly.pocketcasts.compose.buttons.RowOutlinedButton
import au.com.shiftyjelly.pocketcasts.compose.components.TextP50
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesViewModel.State
import au.com.shiftyjelly.pocketcasts.endofyear.stories.Story
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryEpilogue
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryIntro
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryListenedCategories
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryListenedNumbers
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryListeningTime
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryLongestEpisode
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryTopFivePodcasts
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryTopListenedCategories
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryTopPodcast
import au.com.shiftyjelly.pocketcasts.endofyear.views.convertibleToBitmap
import au.com.shiftyjelly.pocketcasts.endofyear.views.stories.StoryEpilogueView
import au.com.shiftyjelly.pocketcasts.endofyear.views.stories.StoryIntroView
Expand All @@ -78,6 +68,16 @@ import au.com.shiftyjelly.pocketcasts.endofyear.views.stories.StoryTopFivePodcas
import au.com.shiftyjelly.pocketcasts.endofyear.views.stories.StoryTopListenedCategoriesView
import au.com.shiftyjelly.pocketcasts.endofyear.views.stories.StoryTopPodcastView
import au.com.shiftyjelly.pocketcasts.models.db.helper.ListenedNumbers
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.Story
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryEpilogue
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryIntro
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryListenedCategories
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryListenedNumbers
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryListeningTime
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryLongestEpisode
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryTopFivePodcasts
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryTopListenedCategories
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryTopPodcast
import au.com.shiftyjelly.pocketcasts.ui.helper.FragmentHostListener
import au.com.shiftyjelly.pocketcasts.ui.helper.StatusBarColor
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import androidx.annotation.FloatRange
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesViewModel.State.Loaded.SegmentsData
import au.com.shiftyjelly.pocketcasts.endofyear.stories.Story
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.EndOfYearManager
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.Story
import au.com.shiftyjelly.pocketcasts.utils.FileUtilWrapper
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -22,7 +23,7 @@ import kotlin.math.roundToInt

@HiltViewModel
class StoriesViewModel @Inject constructor(
private val storiesDataSource: StoriesDataSource,
private val endOfYearManager: EndOfYearManager,
private val fileUtilWrapper: FileUtilWrapper,
) : ViewModel() {
private val mutableState = MutableStateFlow<State>(State.Loading)
Expand All @@ -49,7 +50,7 @@ class StoriesViewModel @Inject constructor(

init {
viewModelScope.launch {
storiesDataSource.hasFullListeningHistory().stateIn(viewModelScope)
endOfYearManager.hasFullListeningHistory().stateIn(viewModelScope)
.collect { isFullListeningHistory ->
if (!isFullListeningHistory) {
// TODO: Integrate listening history sync endpoint
Expand All @@ -60,7 +61,7 @@ class StoriesViewModel @Inject constructor(
}

private suspend fun loadStories() {
storiesDataSource.loadStories().stateIn(viewModelScope).collect { result ->
endOfYearManager.loadStories().stateIn(viewModelScope).collect { result ->
cancelTimer()
if (result.size != stories.value.size) resetProgressAndCurrentIndex()
stories.value = result
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import androidx.compose.ui.unit.sp
import au.com.shiftyjelly.pocketcasts.compose.AppTheme
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryEpilogue
import au.com.shiftyjelly.pocketcasts.localization.R
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryEpilogue
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import au.com.shiftyjelly.pocketcasts.compose.AppTheme
import au.com.shiftyjelly.pocketcasts.compose.components.TextH20
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.R
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryIntro
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryIntro
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme
import au.com.shiftyjelly.pocketcasts.localization.R as LR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryListenedCategories
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryListenedCategories

@Composable
fun StoryListenedCategoriesView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import androidx.compose.ui.unit.dp
import au.com.shiftyjelly.pocketcasts.compose.AppTheme
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryListenedNumbers
import au.com.shiftyjelly.pocketcasts.models.db.helper.ListenedNumbers
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryListenedNumbers
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import au.com.shiftyjelly.pocketcasts.compose.components.PodcastImage
import au.com.shiftyjelly.pocketcasts.compose.components.TextH20
import au.com.shiftyjelly.pocketcasts.compose.components.TextP40
import au.com.shiftyjelly.pocketcasts.endofyear.R
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryListeningTime
import au.com.shiftyjelly.pocketcasts.models.entity.Podcast
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryListeningTime
import au.com.shiftyjelly.pocketcasts.settings.stats.StatsHelper
import au.com.shiftyjelly.pocketcasts.settings.util.FunnyTimeConverter
import au.com.shiftyjelly.pocketcasts.utils.extensions.dpToPx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import au.com.shiftyjelly.pocketcasts.compose.components.PodcastImage
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.compose.components.TextP50
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryLongestEpisode
import au.com.shiftyjelly.pocketcasts.models.db.helper.LongestEpisode
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryLongestEpisode
import au.com.shiftyjelly.pocketcasts.settings.stats.StatsHelper
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import au.com.shiftyjelly.pocketcasts.compose.components.PodcastItem
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryTopFivePodcasts
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryTopFivePodcasts

@Composable
fun StoryTopFivePodcastsView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import au.com.shiftyjelly.pocketcasts.compose.AppTheme
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.compose.components.TextP40
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryTopListenedCategories
import au.com.shiftyjelly.pocketcasts.models.db.helper.ListenedCategory
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryTopListenedCategories
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme
import au.com.shiftyjelly.pocketcasts.images.R as IR

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import au.com.shiftyjelly.pocketcasts.compose.components.PodcastImage
import au.com.shiftyjelly.pocketcasts.compose.components.TextH30
import au.com.shiftyjelly.pocketcasts.compose.components.TextP50
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryTopPodcast
import au.com.shiftyjelly.pocketcasts.models.db.helper.TopPodcast
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.StoryTopPodcast
import au.com.shiftyjelly.pocketcasts.settings.stats.StatsHelper
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.shiftyjelly.pocketcasts.endofyear

import au.com.shiftyjelly.pocketcasts.endofyear.stories.Story
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.EndOfYearManager
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.stories.Story
import au.com.shiftyjelly.pocketcasts.utils.FileUtilWrapper
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -29,7 +30,7 @@ class StoriesViewModelTest {
private lateinit var fileUtilWrapper: FileUtilWrapper

@Mock
private lateinit var storiesDataSource: StoriesDataSource
private lateinit var endOfYearManager: EndOfYearManager

@Before
fun setUp() {
Expand All @@ -56,7 +57,7 @@ class StoriesViewModelTest {
fun `when vm starts, then stories are loaded`() = runTest {
initViewModel(emptyList())

verify(storiesDataSource).loadStories()
verify(endOfYearManager).loadStories()
}

@Test
Expand Down Expand Up @@ -108,10 +109,10 @@ class StoriesViewModelTest {
}

private fun initViewModel(mockStories: List<Story>, hasFullListeningHistory: Boolean = true): StoriesViewModel {
whenever(storiesDataSource.loadStories()).thenReturn(flowOf(mockStories))
whenever(storiesDataSource.hasFullListeningHistory()).thenReturn(flowOf(hasFullListeningHistory))
whenever(endOfYearManager.loadStories()).thenReturn(flowOf(mockStories))
whenever(endOfYearManager.hasFullListeningHistory()).thenReturn(flowOf(hasFullListeningHistory))
return StoriesViewModel(
storiesDataSource = storiesDataSource,
endOfYearManager = endOfYearManager,
fileUtilWrapper = fileUtilWrapper
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.LiveDataReactiveStreams
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesDataSource
import au.com.shiftyjelly.pocketcasts.models.to.RefreshState
import au.com.shiftyjelly.pocketcasts.models.to.SignInState
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.repositories.endofyear.EndOfYearManager
import au.com.shiftyjelly.pocketcasts.repositories.podcast.PodcastManager
import au.com.shiftyjelly.pocketcasts.repositories.user.StatsManager
import au.com.shiftyjelly.pocketcasts.repositories.user.UserManager
Expand All @@ -21,7 +21,7 @@ class ProfileViewModel @Inject constructor(
val podcastManager: PodcastManager,
val statsManager: StatsManager,
val userManager: UserManager,
private val storiesDataSource: StoriesDataSource,
private val endOfYearManager: EndOfYearManager
) : ViewModel() {
var isFragmentChangingConfigurations: Boolean = false
val podcastCount: LiveData<Int> = LiveDataReactiveStreams.fromPublisher(podcastManager.observeCountSubscribed())
Expand All @@ -37,7 +37,7 @@ class ProfileViewModel @Inject constructor(
settings.refreshStateObservable.toFlowable(BackpressureStrategy.LATEST)
)

fun isEndOfYearStoriesEligible() = storiesDataSource.isEligibleForStories()
fun isEndOfYearStoriesEligible() = endOfYearManager.isEligibleForStories()

fun clearFailedRefresh() {
val lastSuccess = settings.getLastSuccessRefreshState()
Expand Down
Loading

0 comments on commit ffc26ca

Please sign in to comment.