-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1060 from UweTrottmann/discover-add-to-watchlist
Discover: consistently provide add show and add to watchlist menu items
- Loading branch information
Showing
15 changed files
with
412 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/battlelancer/seriesguide/shows/search/discover/AddShowPopupMenu.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Uwe Trottmann | ||
|
||
package com.battlelancer.seriesguide.shows.search.discover | ||
|
||
import android.content.Context | ||
import android.os.AsyncTask | ||
import android.view.MenuItem | ||
import android.view.View | ||
import androidx.appcompat.widget.PopupMenu | ||
import com.battlelancer.seriesguide.R | ||
import com.battlelancer.seriesguide.shows.search.discover.AddFragment.OnAddingShowEvent | ||
import com.battlelancer.seriesguide.util.TaskManager | ||
import com.battlelancer.seriesguide.util.tasks.AddShowToWatchlistTask | ||
import com.battlelancer.seriesguide.util.tasks.RemoveShowFromWatchlistTask | ||
import org.greenrobot.eventbus.EventBus | ||
|
||
/** | ||
* A [PopupMenu] with menu items to add a show and add to or remove a show from the Trakt watchlist. | ||
* | ||
* Hides add show item by default based on [show] state. | ||
* Use methods to hide not useful watchlist options. | ||
*/ | ||
class AddShowPopupMenu( | ||
private val context: Context, | ||
private val show: SearchResult, | ||
anchor: View | ||
) : PopupMenu(anchor.context, anchor), PopupMenu.OnMenuItemClickListener { | ||
|
||
init { | ||
inflate(R.menu.add_show_popup_menu) | ||
if (show.state != SearchResult.STATE_ADD) { | ||
menu.findItem(R.id.menu_action_add_show_add).isVisible = false | ||
} | ||
setOnMenuItemClickListener(this) | ||
} | ||
|
||
override fun onMenuItemClick(item: MenuItem): Boolean { | ||
return when (item.itemId) { | ||
R.id.menu_action_add_show_add -> { | ||
// post so other fragments can display a progress indicator for that show | ||
EventBus.getDefault().post(OnAddingShowEvent(show.tmdbId)) | ||
TaskManager.getInstance().performAddTask(context, show) | ||
true | ||
} | ||
|
||
R.id.menu_action_add_show_watchlist_add -> { | ||
@Suppress("DEPRECATION") // AsyncTask | ||
AddShowToWatchlistTask(context, show.tmdbId) | ||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) | ||
true | ||
} | ||
|
||
R.id.menu_action_add_show_watchlist_remove -> { | ||
@Suppress("DEPRECATION") // AsyncTask | ||
RemoveShowFromWatchlistTask(context, show.tmdbId) | ||
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) | ||
true | ||
} | ||
|
||
else -> false | ||
} | ||
} | ||
|
||
fun hideAddToWatchlistAction() { | ||
menu.findItem(R.id.menu_action_add_show_watchlist_add).isVisible = false | ||
} | ||
|
||
fun hideRemoveFromWatchlistAction() { | ||
menu.findItem(R.id.menu_action_add_show_watchlist_remove).isVisible = false | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
.../main/java/com/battlelancer/seriesguide/shows/search/discover/ItemAddShowClickListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Uwe Trottmann | ||
|
||
package com.battlelancer.seriesguide.shows.search.discover | ||
|
||
import android.content.Context | ||
import android.view.View | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.coroutineScope | ||
import com.battlelancer.seriesguide.provider.SgRoomDatabase | ||
import com.battlelancer.seriesguide.traktapi.TraktCredentials | ||
import com.battlelancer.seriesguide.ui.OverviewActivity | ||
import com.battlelancer.seriesguide.util.TaskManager | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import org.greenrobot.eventbus.EventBus | ||
|
||
open class ItemAddShowClickListener( | ||
private val context: Context, | ||
private val lifecycle: Lifecycle, | ||
private val fragmentManager: FragmentManager | ||
) : ItemAddShowViewHolder.ClickListener { | ||
|
||
override fun onItemClick(item: SearchResult) { | ||
if (item.state != SearchResult.STATE_ADDING) { | ||
if (item.state == SearchResult.STATE_ADDED) { | ||
// Already in library, open it. | ||
lifecycle.coroutineScope.launch { | ||
val showId = withContext(Dispatchers.IO) { | ||
SgRoomDatabase.getInstance(context).sgShow2Helper() | ||
.getShowIdByTmdbId(item.tmdbId) | ||
} | ||
if (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) { | ||
context.startActivity(OverviewActivity.intentShow(context, showId)) | ||
} | ||
} | ||
} else { | ||
// Display more details in a dialog. | ||
AddShowDialogFragment.show(fragmentManager, item) | ||
} | ||
} | ||
} | ||
|
||
override fun onAddClick(item: SearchResult) { | ||
EventBus.getDefault().post(AddFragment.OnAddingShowEvent(item.tmdbId)) | ||
TaskManager.getInstance().performAddTask(context, item) | ||
} | ||
|
||
override fun onMoreOptionsClick(view: View, show: SearchResult) { | ||
val isTraktConnected = TraktCredentials.get(context).hasCredentials() | ||
AddShowPopupMenu(context, show, view).apply { | ||
// this does not know watchlist state, so if at all only show the add to item | ||
hideRemoveFromWatchlistAction() | ||
if (!isTraktConnected) hideAddToWatchlistAction() | ||
}.show() | ||
} | ||
|
||
} |
Oops, something went wrong.