Also support bookmarks

This commit is contained in:
Luna712 2023-10-14 16:57:53 -06:00
parent 67c1ac3da4
commit e1f833ad3a
6 changed files with 64 additions and 29 deletions

View file

@ -1306,7 +1306,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
this@MainActivity.getString(R.string.action_add_to_bookmarks),
showApply = false,
{}) {
viewModel.updateWatchStatus(WatchType.values()[it])
viewModel.updateWatchStatus(WatchType.values()[it], this@MainActivity)
}
}

View file

@ -974,7 +974,7 @@ open class ResultFragmentPhone : FullScreenPlayer() {
fab.context.getString(R.string.action_add_to_bookmarks),
showApply = false,
{}) {
viewModel.updateWatchStatus(WatchType.values()[it])
viewModel.updateWatchStatus(WatchType.values()[it], context)
}
}
}

View file

@ -531,7 +531,7 @@ class ResultFragmentTv : Fragment() {
view.context.getString(R.string.action_add_to_bookmarks),
showApply = false,
{}) {
viewModel.updateWatchStatus(WatchType.values()[it])
viewModel.updateWatchStatus(WatchType.values()[it], context)
}
}
}

View file

@ -53,8 +53,11 @@ import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.ioWork
import com.lagradost.cloudstream3.utils.Coroutines.ioWorkSafe
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStoreHelper.deleteBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllBookmarkedDataByWatchType
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllFavorites
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllSubscriptions
import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getDub
import com.lagradost.cloudstream3.utils.DataStoreHelper.getFavoritesData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultEpisode
@ -64,17 +67,20 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.getSubscribedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
import com.lagradost.cloudstream3.utils.DataStoreHelper.removeFavoritesData
import com.lagradost.cloudstream3.utils.DataStoreHelper.removeSubscribedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.setBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.setDub
import com.lagradost.cloudstream3.utils.DataStoreHelper.setFavoritesData
import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultEpisode
import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultSeason
import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultWatchState
import com.lagradost.cloudstream3.utils.DataStoreHelper.updateSubscribedData
import com.lagradost.cloudstream3.utils.UIHelper.navigate
import kotlinx.coroutines.*
import java.io.File
import java.util.concurrent.TimeUnit
interface AlertDialogResponseCallback {
fun onUserResponse(response: Boolean)
fun onUserResponse(action: Boolean)
fun onUserResponseReplace(duplicateId: Int)
}
@ -458,10 +464,10 @@ class ResultViewModel2 : ViewModel() {
val currentWatchType = getResultWatchState(currentId)
DataStoreHelper.setResultWatchState(currentId, status.internalId)
val current = DataStoreHelper.getBookmarkedData(currentId)
setResultWatchState(currentId, status.internalId)
val current = getBookmarkedData(currentId)
val currentTime = System.currentTimeMillis()
DataStoreHelper.setBookmarkedData(
setBookmarkedData(
currentId,
DataStoreHelper.BookmarkedData(
current?.bookmarkedTime ?: currentTime,
@ -834,9 +840,31 @@ class ResultViewModel2 : ViewModel() {
val selectPopup: LiveData<SelectPopup?> = _selectPopup
fun updateWatchStatus(status: WatchType) {
updateWatchStatus(currentResponse ?: return, status)
_watchStatus.postValue(status)
fun updateWatchStatus(status: WatchType, context: Context?) {
val currentStatus = _watchStatus.value ?: return
if (status == currentStatus) return
val response = currentResponse ?: return
checkAndWarnDuplicates(
context,
R.string.bookmarks_duplicate_title,
R.string.bookmarks_duplicate_message,
response.name,
getAllBookmarkedDataByWatchType()[status] ?: emptyList(),
object : AlertDialogResponseCallback {
override fun onUserResponseReplace(duplicateId: Int) {
deleteBookmarkedData(duplicateId)
}
override fun onUserResponse(action: Boolean) {
if (!action) return
updateWatchStatus(response, status)
_watchStatus.postValue(status)
}
}
)
}
private fun startChromecast(
@ -863,13 +891,13 @@ class ResultViewModel2 : ViewModel() {
if (isSubscribed) {
removeSubscribedData(currentId)
_subscribeStatus.postValue(!isSubscribed)
return !isSubscribed
_subscribeStatus.postValue(false)
return false
} else {
checkAndWarnDuplicates(
context,
R.string.subscription_duplicate_title,
R.string.subscription_duplicate_message,
R.string.subscriptions_duplicate_title,
R.string.subscriptions_duplicate_message,
response.name,
getAllSubscriptions(),
object : AlertDialogResponseCallback {
@ -878,10 +906,7 @@ class ResultViewModel2 : ViewModel() {
}
override fun onUserResponse(action: Boolean) {
if (!action) {
_subscribeStatus.postValue(false)
return
}
if (!action) return
val current = getSubscribedData(currentId)
@ -928,8 +953,8 @@ class ResultViewModel2 : ViewModel() {
if (isFavorite) {
removeFavoritesData(currentId)
_favoriteStatus.postValue(!isFavorite)
return !isFavorite
_favoriteStatus.postValue(false)
return false
} else {
checkAndWarnDuplicates(
context,
@ -943,10 +968,7 @@ class ResultViewModel2 : ViewModel() {
}
override fun onUserResponse(action: Boolean) {
if (!action) {
_favoriteStatus.postValue(false)
return
}
if (!action) return
val current = getFavoritesData(currentId)
@ -1895,8 +1917,8 @@ class ResultViewModel2 : ViewModel() {
private fun postSubscription(loadResponse: LoadResponse) {
if (loadResponse.isEpisodeBased()) {
val id = loadResponse.getId()
val data = DataStoreHelper.getSubscribedData(id)
DataStoreHelper.updateSubscribedData(id, data, loadResponse as? EpisodeResponse)
val data = getSubscribedData(id)
updateSubscribedData(id, data, loadResponse as? EpisodeResponse)
val isSubscribed = data != null
_subscribeStatus.postValue(isSubscribed)
}
@ -2274,7 +2296,7 @@ class ResultViewModel2 : ViewModel() {
postResume()
}
fun postResume() {
private fun postResume() {
_resumeWatching.postValue(resume())
}

View file

@ -583,6 +583,17 @@ object DataStoreHelper {
return getKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())
}
fun getAllBookmarkedDataByWatchType(): Map<WatchType, List<BookmarkedData>> {
val allBookmarkedData: List<BookmarkedData> =
getKeys("$currentAccount/$RESULT_WATCH_STATE_DATA")?.mapNotNull {
getKey(it)
} ?: emptyList()
return allBookmarkedData
.groupBy { getResultWatchState(it.id ?: return emptyMap()) }
.mapValues { it.value }
}
fun getAllSubscriptions(): List<SubscribedData> {
return getKeys("$currentAccount/$RESULT_SUBSCRIBED_STATE_DATA")?.mapNotNull {
getKey(it)

View file

@ -695,10 +695,12 @@
<string name="favorite_removed">%s removed from favorites</string>
<string name="action_add_to_favorites">Add to favorites</string>
<string name="action_remove_from_favorites">Remove from favorites</string>
<string name="subscription_duplicate_title">Possible Duplicate Subscription</string>
<string name="subscription_duplicate_message">A possible duplicate with the same name already exists in your subscrptions.</string>
<string name="subscriptions_duplicate_title">Possible Duplicate Subscription</string>
<string name="subscriptions_duplicate_message">A possible duplicate with the same name already exists in your subscrptions.</string>
<string name="favorites_duplicate_title">Possible Duplicate Found</string>
<string name="favorites_duplicate_message">A possible duplicate with the same name already exists in your favorites.</string>
<string name="add_anyway">Add Anyway</string>
<string name="replace">Replace</string>
<string name="bookmarks_duplicate_title">Possible Duplicate Found</string>
<string name="bookmarks_duplicate_message">A possible duplicate with the same name and watch status already exists in your library.</string>
</resources>