Skip to content

Commit

Permalink
Merge branch 'clean-up-searchresult' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
UweTrottmann committed Oct 17, 2024
2 parents 062fc14 + 6d756bf commit 7af9144
Show file tree
Hide file tree
Showing 42 changed files with 780 additions and 913 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Uwe Trottmann
// SPDX-License-Identifier: Apache-2.0
// Copyright 2013-2024 Uwe Trottmann

package com.battlelancer.seriesguide.dataliberation

Expand Down Expand Up @@ -63,7 +63,7 @@ class AutoBackupFragment : Fragment() {
}

binding.buttonAutoBackupNow.setOnClickListener {
if (TaskManager.getInstance().tryBackupTask(requireContext())) {
if (TaskManager.tryBackupTask(requireContext())) {
setProgressLock(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class JsonExportTask(
}

private fun onPostExecute(result: Int) {
TaskManager.getInstance().releaseBackupTaskRef()
TaskManager.releaseBackupTaskRef()

if (!isAutoBackupMode) {
val messageId: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Uwe Trottmann
// SPDX-License-Identifier: Apache-2.0
// Copyright 2013-2024 Uwe Trottmann

package com.battlelancer.seriesguide.dataliberation

Expand Down Expand Up @@ -120,8 +120,7 @@ class JsonImportTask(

private fun doInBackground(coroutineScope: CoroutineScope): Int {
// Ensure no large database ops are running
val tm = TaskManager.getInstance()
if (SgSyncAdapter.isSyncActive(context, false) || tm.isAddTaskRunning) {
if (SgSyncAdapter.isSyncActive(context, false) || TaskManager.isAddTaskRunning) {
return ERROR_LARGE_DB_OP
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ import android.os.Bundle
import androidx.fragment.app.Fragment
import com.battlelancer.seriesguide.BuildConfig
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.shows.search.discover.AddShowDialogFragment.OnAddShowListener
import com.battlelancer.seriesguide.shows.search.discover.SearchResult
import com.battlelancer.seriesguide.ui.BaseActivity
import com.battlelancer.seriesguide.ui.SinglePaneActivity
import com.battlelancer.seriesguide.util.TaskManager
import com.battlelancer.seriesguide.util.commitReorderingAllowed
import timber.log.Timber

/**
* Displays history of watched episodes or movies.
*/
class HistoryActivity : BaseActivity(), OnAddShowListener {
class HistoryActivity : BaseActivity() {

interface InitBundle {
companion object {
Expand Down Expand Up @@ -60,13 +57,6 @@ class HistoryActivity : BaseActivity(), OnAddShowListener {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

/**
* Called if the user adds a show from a trakt stream fragment.
*/
override fun onAddShow(show: SearchResult) {
TaskManager.getInstance().performAddTask(this, show)
}

companion object {
const val EPISODES_LOADER_ID = 100
const val MOVIES_LOADER_ID = 101
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ class UserEpisodeStreamFragment : StreamFragment() {
)
} else {
// Offer to add the show if not in database.
AddShowDialogFragment.show(parentFragmentManager, showTmdbId)
AddShowDialogFragment.show(
requireContext(),
parentFragmentManager,
showTmdbId
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Uwe Trottmann
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2024 Uwe Trottmann

package com.battlelancer.seriesguide.movies.similar

Expand All @@ -14,11 +14,11 @@ class SimilarMoviesActivity : BaseSimilarActivity() {

override val liftOnScrollTargetViewId: Int = SimilarShowsFragment.liftOnScrollTargetViewId
override val titleStringRes: Int = R.string.title_similar_movies
override fun createFragment(tmdbId: Int, title: String?): Fragment =
override fun createFragment(tmdbId: Int, title: String): Fragment =
SimilarMoviesFragment.newInstance(tmdbId, title)

companion object {
fun intent(context: Context, movieTmdbId: Int, title: String?): Intent {
fun intent(context: Context, movieTmdbId: Int, title: String): Intent {
return Intent(context, SimilarMoviesActivity::class.java)
.putExtras(movieTmdbId, title)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023-2024 Uwe Trottmann
// Copyright 2019-2024 Uwe Trottmann

package com.battlelancer.seriesguide.movies.similar

Expand Down Expand Up @@ -119,7 +119,7 @@ class SimilarMoviesFragment : Fragment() {
private const val ARG_TMDB_ID = "ARG_TMDB_ID"
private const val ARG_TITLE = "ARG_TITLE"

fun newInstance(tmdbId: Int, title: String?): SimilarMoviesFragment {
fun newInstance(tmdbId: Int, title: String): SimilarMoviesFragment {
return SimilarMoviesFragment().apply {
arguments = Bundle().apply {
putInt(ARG_TMDB_ID, tmdbId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023 Uwe Trottmann
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018-2024 Uwe Trottmann

package com.battlelancer.seriesguide.shows

Expand Down Expand Up @@ -63,7 +63,7 @@ class FirstRunView @JvmOverloads constructor(context: Context, attrs: AttributeS
putBoolean(DisplaySettings.KEY_PREVENT_SPOILERS, noSpoilers)
}
// update next episode strings right away
TaskManager.getInstance().tryNextEpisodeUpdateTask(v.context)
TaskManager.tryNextEpisodeUpdateTask(v.context)
// show
binding.checkboxNoSpoilers.isChecked = noSpoilers
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.battlelancer.seriesguide.shows.calendar.UpcomingFragment
import com.battlelancer.seriesguide.shows.episodes.EpisodesActivity
import com.battlelancer.seriesguide.shows.history.ShowsHistoryFragment
import com.battlelancer.seriesguide.shows.search.discover.AddShowDialogFragment
import com.battlelancer.seriesguide.shows.search.discover.SearchResult
import com.battlelancer.seriesguide.shows.search.discover.ShowsDiscoverFragment
import com.battlelancer.seriesguide.shows.search.discover.ShowsDiscoverPagingActivity
import com.battlelancer.seriesguide.sync.AccountUtils
Expand All @@ -47,7 +46,7 @@ import kotlinx.coroutines.launch
* Provides the apps main screen, displays tabs for shows, discover, history,
* recent and upcoming episodes. Runs upgrade code and checks billing state.
*/
open class ShowsActivityImpl : BaseTopActivity(), AddShowDialogFragment.OnAddShowListener {
open class ShowsActivityImpl : BaseTopActivity() {

private lateinit var tabsAdapter: TabStripAdapter
private lateinit var viewPager: ViewPager2
Expand Down Expand Up @@ -154,7 +153,7 @@ open class ShowsActivityImpl : BaseTopActivity(), AddShowDialogFragment.OnAddSho
}
} else {
// Show not added, offer to.
AddShowDialogFragment.show(supportFragmentManager, showTmdbId)
AddShowDialogFragment.show(this, supportFragmentManager, showTmdbId)
}
}
} else if (Intents.ACTION_VIEW_SHOW == action) {
Expand All @@ -170,7 +169,7 @@ open class ShowsActivityImpl : BaseTopActivity(), AddShowDialogFragment.OnAddSho
viewIntent = OverviewActivity.intentShow(this, showId)
} else {
// no such show, offer to add it
AddShowDialogFragment.show(supportFragmentManager, showTmdbId)
AddShowDialogFragment.show(this, supportFragmentManager, showTmdbId)
}
}

Expand Down Expand Up @@ -312,7 +311,7 @@ open class ShowsActivityImpl : BaseTopActivity(), AddShowDialogFragment.OnAddSho
}

// update next episodes
TaskManager.getInstance().tryNextEpisodeUpdateTask(this)
TaskManager.tryNextEpisodeUpdateTask(this)
}

override fun onPause() {
Expand All @@ -332,13 +331,6 @@ open class ShowsActivityImpl : BaseTopActivity(), AddShowDialogFragment.OnAddSho
return keyCode == KeyEvent.KEYCODE_BACK
}

/**
* Called if the user adds a show from a trakt stream fragment.
*/
override fun onAddShow(show: SearchResult) {
TaskManager.getInstance().performAddTask(this, show)
}

override val snackbarParentView: View
get() = findViewById(R.id.coordinatorLayoutShows)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ShowsDistillationFragment : AppCompatDialogFragment() {

override fun onNoReleasedChanged(value: Boolean) {
DisplaySettings.setNoReleasedEpisodes(requireContext(), value)
TaskManager.getInstance().tryNextEpisodeUpdateTask(requireContext())
TaskManager.tryNextEpisodeUpdateTask(requireContext())
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class ShowsHistoryFragment : Fragment() {
showDetails(view, episodeRowId)
} else if (showTmdbId != null && showTmdbId > 0) {
// episode missing: show likely not in database, suggest adding it
AddShowDialogFragment.show(parentFragmentManager, showTmdbId)
AddShowDialogFragment.show(requireContext(), parentFragmentManager, showTmdbId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import android.view.inputmethod.EditorInfo
import androidx.viewpager2.widget.ViewPager2
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.databinding.ActivitySearchBinding
import com.battlelancer.seriesguide.shows.search.discover.AddShowDialogFragment
import com.battlelancer.seriesguide.shows.search.discover.SearchResult
import com.battlelancer.seriesguide.ui.BaseMessageActivity
import com.battlelancer.seriesguide.ui.TabStripAdapter
import com.battlelancer.seriesguide.util.TabClickEvent
import com.battlelancer.seriesguide.util.TaskManager
import com.battlelancer.seriesguide.util.ThemeUtils
import com.battlelancer.seriesguide.util.ViewTools
import com.google.android.gms.actions.SearchIntents
Expand All @@ -33,7 +30,7 @@ import org.greenrobot.eventbus.EventBus
* When [SearchManager.APP_DATA] contains a [EpisodeSearchFragment.ARG_SHOW_TITLE] switches to the
* episodes tab.
*/
open class SearchActivityImpl : BaseMessageActivity(), AddShowDialogFragment.OnAddShowListener {
open class SearchActivityImpl : BaseMessageActivity() {

private lateinit var binding: ActivitySearchBinding

Expand Down Expand Up @@ -193,11 +190,6 @@ open class SearchActivityImpl : BaseMessageActivity(), AddShowDialogFragment.OnA
EventBus.getDefault().removeStickyEvent(SearchQueryEvent::class.java)
}

override fun onAddShow(show: SearchResult) {
TaskManager.getInstance().performAddTask(this, show)
}


override val snackbarParentView: View
get() = findViewById(R.id.coordinatorLayoutSearch)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.databinding.DialogAddshowBinding
import com.battlelancer.seriesguide.shows.ShowsSettings
import com.battlelancer.seriesguide.shows.search.similar.SimilarShowsFragment
import com.battlelancer.seriesguide.shows.tools.AddShowTask
import com.battlelancer.seriesguide.shows.tools.ShowStatus
import com.battlelancer.seriesguide.streaming.StreamingSearch
import com.battlelancer.seriesguide.ui.OverviewActivity
Expand All @@ -28,6 +29,7 @@ import com.battlelancer.seriesguide.util.LanguageTools
import com.battlelancer.seriesguide.util.RatingsTools.initialize
import com.battlelancer.seriesguide.util.RatingsTools.setRatingValues
import com.battlelancer.seriesguide.util.ServiceUtils
import com.battlelancer.seriesguide.util.TaskManager
import com.battlelancer.seriesguide.util.TextTools
import com.battlelancer.seriesguide.util.TimeTools
import com.battlelancer.seriesguide.util.ViewTools
Expand All @@ -47,12 +49,7 @@ import timber.log.Timber
*/
class AddShowDialogFragment : AppCompatDialogFragment() {

interface OnAddShowListener {
fun onAddShow(show: SearchResult)
}

private var binding: DialogAddshowBinding? = null
private lateinit var addShowListener: OnAddShowListener
private var showTmdbId: Int = 0
private lateinit var languageCode: String
private val model by viewModels<AddShowDialogViewModel> {
Expand All @@ -61,20 +58,9 @@ class AddShowDialogFragment : AppCompatDialogFragment() {

override fun onAttach(context: Context) {
super.onAttach(context)
try {
addShowListener = context as OnAddShowListener
} catch (e: ClassCastException) {
throw ClassCastException("$context must implement OnAddShowListener")
}

showTmdbId = requireArguments().getInt(ARG_INT_SHOW_TMDBID)
val languageCodeOrNull = requireArguments().getString(ARG_STRING_LANGUAGE_CODE)
if (languageCodeOrNull.isNullOrEmpty()) {
// Use search language.
this.languageCode = ShowsSettings.getShowsSearchLanguage(context)
} else {
this.languageCode = languageCodeOrNull
}
languageCode = requireArguments().getString(ARG_STRING_LANGUAGE_CODE)
?: throw IllegalArgumentException("Language code must not be null")
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -132,10 +118,12 @@ class AddShowDialogFragment : AppCompatDialogFragment() {
val details = model.showDetails.value
if (details?.show != null) {
dismissAllowingStateLoss()
SimilarShowsFragment.displaySimilarShowsEventLiveData.postValue(SearchResult().also {
it.tmdbId = showTmdbId
it.title = details.show.title
})
SimilarShowsFragment.displaySimilarShowsEventLiveData.postValue(
SimilarShowsFragment.SimilarShowEvent(
tmdbId = showTmdbId,
title = details.show.title
)
)
}
}
}
Expand Down Expand Up @@ -236,11 +224,10 @@ class AddShowDialogFragment : AppCompatDialogFragment() {
binding.buttonPositive.setText(R.string.action_shows_add)
binding.buttonPositive.setOnClickListener {
EventBus.getDefault().post(OnAddingShowEvent(showTmdbId))
addShowListener.onAddShow(SearchResult().also {
it.tmdbId = showTmdbId
it.title = show.title
it.language = languageCode
})
TaskManager.performAddTask(
requireContext(),
AddShowTask.Show(showTmdbId, languageCode, show.title)
)
dismiss()
}
}
Expand Down Expand Up @@ -308,32 +295,27 @@ class AddShowDialogFragment : AppCompatDialogFragment() {
private const val ARG_STRING_LANGUAGE_CODE = "language"

/**
* Display a [AddShowDialogFragment] for the given show. The language of the show should
* be set.
* Display an [AddShowDialogFragment] for the given show. The language of the show should
* be set, otherwise uses [ShowsSettings.getShowsSearchLanguage].
*/
@JvmStatic
fun show(fm: FragmentManager, show: SearchResult) {
fun show(fm: FragmentManager, showTmdbId: Int, languageCode: String) {
// Replace any currently showing add dialog (do not add it to the back stack).
val ft = fm.beginTransaction()
val prev = fm.findFragmentByTag(TAG)
if (prev != null) {
ft.remove(prev)
}
newInstance(show.tmdbId, show.language).safeShow(fm, ft, TAG)
newInstance(showTmdbId, languageCode).safeShow(fm, ft, TAG)
}

/**
* Display a [AddShowDialogFragment] for the given show.
* Display an [AddShowDialogFragment] for the given show.
*
* Use if there is no actual search result but just an id available. Uses the search
* or fall back language.
* Use if there is just an id available. The language code is always
* [ShowsSettings.getShowsSearchLanguage].
*/
@JvmStatic
fun show(fm: FragmentManager, showTmdbId: Int) {
val fakeResult = SearchResult().apply {
tmdbId = showTmdbId
}
show(fm, fakeResult)
fun show(context: Context, fm: FragmentManager, showTmdbId: Int) {
show(fm, showTmdbId, ShowsSettings.getShowsSearchLanguage(context))
}

private fun newInstance(showTmdbId: Int, languageCode: String?): AddShowDialogFragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.MenuItem
import android.view.View
import androidx.appcompat.widget.PopupMenu
import com.battlelancer.seriesguide.R
import com.battlelancer.seriesguide.shows.tools.AddShowTask
import com.battlelancer.seriesguide.util.TaskManager
import com.battlelancer.seriesguide.util.tasks.AddShowToWatchlistTask
import com.battlelancer.seriesguide.util.tasks.RemoveShowFromWatchlistTask
Expand Down Expand Up @@ -39,7 +40,9 @@ class AddShowPopupMenu(
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)
TaskManager.performAddTask(
context, AddShowTask.Show(show.tmdbId, show.languageCode, show.title)
)
true
}

Expand Down
Loading

0 comments on commit 7af9144

Please sign in to comment.