Skip to content
Permalink
Browse files

Selection on watched now works

  • Loading branch information...
chrisbanes committed Aug 26, 2019
1 parent c9854ef commit 12f3b5ea0ca30356490101050288adc40f2fcc4e
@@ -17,6 +17,7 @@
<module fileurl="file://$PROJECT_DIR$/domain/domain.iml" filepath="$PROJECT_DIR$/domain/domain.iml" />
<module fileurl="file://$PROJECT_DIR$/tasks/tasks.iml" filepath="$PROJECT_DIR$/tasks/tasks.iml" />
<module fileurl="file://$PROJECT_DIR$/tivi.iml" filepath="$PROJECT_DIR$/tivi.iml" />
<module fileurl="file://$PROJECT_DIR$/ui-recommended/tivi-ui-recommended.iml" filepath="$PROJECT_DIR$/ui-recommended/tivi-ui-recommended.iml" />
<module fileurl="file://$PROJECT_DIR$/tmdb/tmdb.iml" filepath="$PROJECT_DIR$/tmdb/tmdb.iml" />
<module fileurl="file://$PROJECT_DIR$/trakt/trakt.iml" filepath="$PROJECT_DIR$/trakt/trakt.iml" />
<module fileurl="file://$PROJECT_DIR$/trakt-auth/trakt-auth.iml" filepath="$PROJECT_DIR$/trakt-auth/trakt-auth.iml" />
@@ -21,7 +21,7 @@ import app.tivi.R
enum class HomeNavigationItem(val destinationId: Int, val labelResId: Int, val iconResId: Int) {
DISCOVER(R.id.navigation_discover, R.string.discover_title, R.drawable.ic_popular),
WATCHED(R.id.navigation_watched, R.string.library_watched, R.drawable.ic_eye_24dp),
FOLLOWED(R.id.navigation_followed, R.string.library_followed_shows, R.drawable.ic_heart_24dp),
FOLLOWED(R.id.navigation_followed, R.string.library_followed_shows, R.drawable.ic_heart_border),
SETTINGS(R.id.settings, R.string.settings_title, R.drawable.ic_settings_black_24dp)
}

@@ -24,6 +24,8 @@
<style name="Base.Theme.Tivi" parent="Platform.Theme.Tivi">
<item name="actionBarTheme">@style/ThemeOverlay.Tivi.ActionBar</item>

<item name="windowActionModeOverlay">true</item>

<!-- Default widget styles -->
<item name="toolbarStyle">@style/Widget.Tivi.Toolbar</item>

@@ -79,6 +79,9 @@
<string name="follow_show_add">Follow show</string>
<string name="follow_show_remove">Un-follow show</string>

<string name="follow_shows_add">Follow shows</string>
<string name="follow_shows_remove">Un-follow shows</string>

<string name="episode_watches">Episode watches</string>

<string name="first_aired">First aired: %1$s</string>
@@ -78,9 +78,6 @@ class FollowedShowsRepository @Inject constructor(
val newEntryId = followedShowsStore.save(newEntry)

logger.v("addFollowedShow. Entry saved with ID: %s - %s", newEntryId, newEntry)

// Now sync it up
syncFollowedShows()
}
}

@@ -90,8 +87,6 @@ class FollowedShowsRepository @Inject constructor(
if (entry != null) {
// Mark the show as pending deletion
followedShowsStore.save(entry.copy(pendingAction = PendingAction.DELETE))
// Now sync it up
syncFollowedShows()
}
}

@@ -16,6 +16,7 @@

package app.tivi.domain.interactors

import app.tivi.actions.ShowTasks
import app.tivi.data.repositories.episodes.SeasonsEpisodesRepository
import app.tivi.data.repositories.followedshows.FollowedShowsRepository
import app.tivi.domain.Interactor
@@ -29,6 +30,7 @@ class ChangeShowFollowStatus @Inject constructor(
private val followedShowsRepository: FollowedShowsRepository,
private val seasonsEpisodesRepository: SeasonsEpisodesRepository,
dispatchers: AppCoroutineDispatchers,
private val showTasks: ShowTasks,
@ProcessLifetime val processScope: CoroutineScope
) : Interactor<ChangeShowFollowStatus.Params>() {
override val scope: CoroutineScope = processScope + dispatchers.io
@@ -42,28 +44,43 @@ class ChangeShowFollowStatus @Inject constructor(

suspend fun follow(showId: Long) {
followedShowsRepository.addFollowedShow(showId)

// Update seasons, episodes and watches
if (seasonsEpisodesRepository.needShowSeasonsUpdate(showId)) {
seasonsEpisodesRepository.updateSeasonsEpisodes(showId)
if (!params.deferDataFetch) {
if (seasonsEpisodesRepository.needShowSeasonsUpdate(showId)) {
seasonsEpisodesRepository.updateSeasonsEpisodes(showId)
}
seasonsEpisodesRepository.updateShowEpisodeWatchesIfNeeded(showId)
}
seasonsEpisodesRepository.updateShowEpisodeWatchesIfNeeded(showId)
}

when (params.action) {
Action.TOGGLE -> {
if (followedShowsRepository.isShowFollowed(params.showId)) {
unfollow(params.showId)
} else {
follow(params.showId)
for (showId in params.showIds) {
when (params.action) {
Action.TOGGLE -> {
if (followedShowsRepository.isShowFollowed(showId)) {
unfollow(showId)
} else {
follow(showId)
}
}
Action.FOLLOW -> follow(showId)
Action.UNFOLLOW -> unfollow(showId)
}
Action.FOLLOW -> follow(params.showId)
Action.UNFOLLOW -> unfollow(params.showId)
}
// Finally, sync the changes to Trakt
followedShowsRepository.syncFollowedShows()

if (params.deferDataFetch) {
showTasks.syncFollowedShows()
}
}

data class Params(val showId: Long, val action: Action)
data class Params(
val showIds: Collection<Long>,
val action: Action,
val deferDataFetch: Boolean = false
) {
constructor(showId: Long, action: Action) : this(listOf(showId), action)
}

enum class Action { FOLLOW, UNFOLLOW, TOGGLE }
}
@@ -86,7 +86,7 @@ class WatchedEpoxyController @Inject constructor(
tiviShow(item.show)
posterImage(item.images.findHighestRatedPoster())
posterTransitionName("show_${item.show.homepage}")
selected(item.entry.id in viewState.selectedEntryIds)
selected(item.show.id in viewState.selectedShowIds)
callbacks?.also { cb ->
clickListener(View.OnClickListener { cb.onItemClicked(item) })
longClickListener(View.OnLongClickListener { cb.onItemLongClicked(item) })
@@ -17,7 +17,10 @@
package app.tivi.home.watched

import android.os.Bundle
import android.view.ActionMode
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
@@ -43,6 +46,8 @@ class WatchedFragment : TiviMvRxFragment() {

@Inject lateinit var controller: WatchedEpoxyController

private var currentActionMode: ActionMode? = null

private val listItemSharedElementHelper by lazy(LazyThreadSafetyMode.NONE) {
ListItemSharedElementHelper(binding.watchedRv)
}
@@ -60,7 +65,7 @@ class WatchedFragment : TiviMvRxFragment() {
controller.callbacks = object : WatchedEpoxyController.Callbacks {
override fun onItemClicked(item: WatchedShowEntryWithShow) {
// Let the ViewModel have the first go
if (viewModel.onItemClick(item.entry)) {
if (viewModel.onItemClick(item.show)) {
return
}

@@ -77,7 +82,7 @@ class WatchedFragment : TiviMvRxFragment() {
}

override fun onItemLongClicked(item: WatchedShowEntryWithShow): Boolean {
return viewModel.onItemLongClick(item.entry)
return viewModel.onItemLongClick(item.show)
}

override fun onFilterChanged(filter: String) = viewModel.setFilter(filter)
@@ -101,6 +106,12 @@ class WatchedFragment : TiviMvRxFragment() {
scheduleStartPostponedTransitions()
}

if (state.selectionOpen && currentActionMode == null) {
startSelectionActionMode()
} else if (!state.selectionOpen && currentActionMode != null) {
currentActionMode?.finish()
}

binding.state = state

if (state.watchedShows != null) {
@@ -109,4 +120,31 @@ class WatchedFragment : TiviMvRxFragment() {
controller.submitList(state.watchedShows)
}
}

private fun startSelectionActionMode() {
currentActionMode = requireActivity().startActionMode(object : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_follow -> viewModel.followSelectedShows()
R.id.menu_unfollow -> viewModel.unfollowSelectedShows()
}
return true
}

override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
mode.menuInflater.inflate(R.menu.action_mode_watched, menu)
return true
}

override fun onPrepareActionMode(mode: ActionMode, menu: Menu) = true

override fun onDestroyActionMode(mode: ActionMode) {
viewModel.clearSelection()

if (mode == currentActionMode) {
currentActionMode = null
}
}
})
}
}
@@ -20,8 +20,9 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.PagedList
import app.tivi.TiviMvRxViewModel
import app.tivi.data.entities.SortOption
import app.tivi.data.entities.WatchedShowEntry
import app.tivi.data.entities.TiviShow
import app.tivi.data.resultentities.WatchedShowEntryWithShow
import app.tivi.domain.interactors.ChangeShowFollowStatus
import app.tivi.domain.interactors.UpdateWatchedShows
import app.tivi.domain.invoke
import app.tivi.domain.launchObserve
@@ -45,6 +46,7 @@ import kotlinx.coroutines.launch
class WatchedViewModel @AssistedInject constructor(
@Assisted initialState: WatchedViewState,
private val updateWatchedShows: UpdateWatchedShows,
private val changeShowFollowStatus: ChangeShowFollowStatus,
private val observePagedWatchedShows: ObservePagedWatchedShows,
private val observeTraktAuthState: ObserveTraktAuthState,
tmdbManager: TmdbManager
@@ -65,7 +67,7 @@ class WatchedViewModel @AssistedInject constructor(

private val loadingState = ObservableLoadingCounter()

private var selectionOpen = false
private var isSelectionOpen = false

init {
viewModelScope.launch {
@@ -133,40 +135,67 @@ class WatchedViewModel @AssistedInject constructor(
setState { copy(sort = sort) }
}

fun onItemClick(entry: WatchedShowEntry): Boolean {
if (selectionOpen) {
fun clearSelection() {
isSelectionOpen = false
setState {
copy(selectionOpen = false, selectedShowIds = emptySet())
}
}

fun onItemClick(show: TiviShow): Boolean {
if (isSelectionOpen) {
setState {
val currentSelection = when {
entry.id in selectedEntryIds -> selectedEntryIds.minus(entry.id)
else -> selectedEntryIds.plus(entry.id)
show.id in selectedShowIds -> selectedShowIds.minus(show.id)
else -> selectedShowIds.plus(show.id)
}
this@WatchedViewModel.selectionOpen = currentSelection.isNotEmpty()
copy(
selectionOpen = this@WatchedViewModel.selectionOpen,
selectedEntryIds = currentSelection
)
isSelectionOpen = currentSelection.isNotEmpty()
copy(selectionOpen = isSelectionOpen, selectedShowIds = currentSelection)
}
return true
}
return false
}

fun onItemLongClick(entry: WatchedShowEntry): Boolean {
if (!selectionOpen) {
selectionOpen = true
fun onItemLongClick(show: TiviShow): Boolean {
if (!isSelectionOpen) {
isSelectionOpen = true

setState {
var currentSelection = selectedEntryIds
if (entry.id !in currentSelection) {
currentSelection = currentSelection.plus(entry.id)
var currentSelection = selectedShowIds
if (show.id !in currentSelection) {
currentSelection = currentSelection.plus(show.id)
}
copy(selectionOpen = selectionOpen, selectedEntryIds = currentSelection)
copy(selectionOpen = isSelectionOpen, selectedShowIds = currentSelection)
}
return true
}
return false
}

fun followSelectedShows() {
withState {
changeShowFollowStatus(
ChangeShowFollowStatus.Params(
it.selectedShowIds,
ChangeShowFollowStatus.Action.FOLLOW,
deferDataFetch = true)
)
clearSelection()
}
}

fun unfollowSelectedShows() {
withState {
changeShowFollowStatus(
ChangeShowFollowStatus.Params(
it.selectedShowIds,
ChangeShowFollowStatus.Action.UNFOLLOW)
)
clearSelection()
}
}

private fun refreshWatched(fromUser: Boolean) {
updateWatchedShows(UpdateWatchedShows.Params(fromUser)).also {
viewModelScope.launch {
@@ -28,7 +28,7 @@ data class WatchedViewState(
val isEmpty: Boolean = false,
val watchedShows: PagedList<WatchedShowEntryWithShow>? = null,
val selectionOpen: Boolean = false,
val selectedEntryIds: List<Long> = emptyList(),
val selectedShowIds: Set<Long> = emptySet(),
val filterActive: Boolean = false,
val filter: String? = null,
val availableSorts: List<SortOption> = emptyList(),
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2019 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/menu_follow"
android:title="@string/follow_shows_add"
android:icon="@drawable/ic_favorite"
app:showAsAction="ifRoom" />

<item android:id="@+id/menu_unfollow"
android:title="@string/follow_shows_remove"
android:icon="@drawable/ic_favorite_outline"
app:showAsAction="ifRoom" />

</menu>

0 comments on commit 12f3b5e

Please sign in to comment.
You can’t perform that action at this time.