mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Add some string normalization to cover an edge case and rename callbacks
This commit is contained in:
parent
fc168c0ab0
commit
4f91641e80
1 changed files with 36 additions and 23 deletions
|
@ -815,7 +815,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
status: WatchType,
|
status: WatchType,
|
||||||
context: Context?,
|
context: Context?,
|
||||||
loadResponse: LoadResponse? = null,
|
loadResponse: LoadResponse? = null,
|
||||||
statusChangeCallback: ((Boolean) -> Unit)? = null
|
statusChangedCallback: ((statusChanged: Boolean) -> Unit)? = null
|
||||||
) {
|
) {
|
||||||
val response = loadResponse ?: currentResponse ?: return
|
val response = loadResponse ?: currentResponse ?: return
|
||||||
|
|
||||||
|
@ -831,8 +831,8 @@ class ResultViewModel2 : ViewModel() {
|
||||||
response.name,
|
response.name,
|
||||||
response.year,
|
response.year,
|
||||||
getAllBookmarkedDataByWatchType()[status] ?: emptyList()
|
getAllBookmarkedDataByWatchType()[status] ?: emptyList()
|
||||||
) { userResponse: Boolean, duplicateId: Int? ->
|
) { shouldContinue: Boolean, duplicateId: Int? ->
|
||||||
if (!userResponse) return@checkAndWarnDuplicates
|
if (!shouldContinue) return@checkAndWarnDuplicates
|
||||||
|
|
||||||
if (duplicateId != null) {
|
if (duplicateId != null) {
|
||||||
deleteBookmarkedData(duplicateId)
|
deleteBookmarkedData(duplicateId)
|
||||||
|
@ -860,7 +860,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
|
|
||||||
_watchStatus.postValue(status)
|
_watchStatus.postValue(status)
|
||||||
|
|
||||||
statusChangeCallback?.invoke(true)
|
statusChangedCallback?.invoke(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -880,7 +880,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
**/
|
**/
|
||||||
fun toggleSubscriptionStatus(
|
fun toggleSubscriptionStatus(
|
||||||
context: Context?,
|
context: Context?,
|
||||||
statusChangeCallback: ((Boolean?) -> Unit)? = null
|
statusChangedCallback: ((newStatus: Boolean?) -> Unit)? = null
|
||||||
): Boolean? {
|
): Boolean? {
|
||||||
val isSubscribed = _subscribeStatus.value ?: return null
|
val isSubscribed = _subscribeStatus.value ?: return null
|
||||||
val response = currentResponse ?: return null
|
val response = currentResponse ?: return null
|
||||||
|
@ -890,7 +890,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
|
|
||||||
if (isSubscribed) {
|
if (isSubscribed) {
|
||||||
removeSubscribedData(currentId)
|
removeSubscribedData(currentId)
|
||||||
statusChangeCallback?.invoke(false)
|
statusChangedCallback?.invoke(false)
|
||||||
_subscribeStatus.postValue(false)
|
_subscribeStatus.postValue(false)
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
|
@ -900,9 +900,9 @@ class ResultViewModel2 : ViewModel() {
|
||||||
response.name,
|
response.name,
|
||||||
response.year,
|
response.year,
|
||||||
getAllSubscriptions(),
|
getAllSubscriptions(),
|
||||||
) { userResponse: Boolean, duplicateId: Int? ->
|
) { shouldContinue: Boolean, duplicateId: Int? ->
|
||||||
if (!userResponse) {
|
if (!shouldContinue) {
|
||||||
statusChangeCallback?.invoke(null)
|
statusChangedCallback?.invoke(null)
|
||||||
return@checkAndWarnDuplicates
|
return@checkAndWarnDuplicates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -930,7 +930,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
|
|
||||||
_subscribeStatus.postValue(true)
|
_subscribeStatus.postValue(true)
|
||||||
|
|
||||||
statusChangeCallback?.invoke(true)
|
statusChangedCallback?.invoke(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return _subscribeStatus.value
|
return _subscribeStatus.value
|
||||||
|
@ -942,7 +942,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
**/
|
**/
|
||||||
fun toggleFavoriteStatus(
|
fun toggleFavoriteStatus(
|
||||||
context: Context?,
|
context: Context?,
|
||||||
statusChangeCallback: ((Boolean?) -> Unit)? = null
|
statusChangedCallback: ((newStatus: Boolean?) -> Unit)? = null
|
||||||
): Boolean? {
|
): Boolean? {
|
||||||
val isFavorite = _favoriteStatus.value ?: return null
|
val isFavorite = _favoriteStatus.value ?: return null
|
||||||
val response = currentResponse ?: return null
|
val response = currentResponse ?: return null
|
||||||
|
@ -951,7 +951,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
|
|
||||||
if (isFavorite) {
|
if (isFavorite) {
|
||||||
removeFavoritesData(currentId)
|
removeFavoritesData(currentId)
|
||||||
statusChangeCallback?.invoke(false)
|
statusChangedCallback?.invoke(false)
|
||||||
_favoriteStatus.postValue(false)
|
_favoriteStatus.postValue(false)
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
|
@ -961,9 +961,9 @@ class ResultViewModel2 : ViewModel() {
|
||||||
response.name,
|
response.name,
|
||||||
response.year,
|
response.year,
|
||||||
getAllFavorites(),
|
getAllFavorites(),
|
||||||
) { userResponse: Boolean, duplicateId: Int? ->
|
) { shouldContinue: Boolean, duplicateId: Int? ->
|
||||||
if (!userResponse) {
|
if (!shouldContinue) {
|
||||||
statusChangeCallback?.invoke(null)
|
statusChangedCallback?.invoke(null)
|
||||||
return@checkAndWarnDuplicates
|
return@checkAndWarnDuplicates
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -990,7 +990,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
|
|
||||||
_favoriteStatus.postValue(true)
|
_favoriteStatus.postValue(true)
|
||||||
|
|
||||||
statusChangeCallback?.invoke(true)
|
statusChangedCallback?.invoke(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return _favoriteStatus.value
|
return _favoriteStatus.value
|
||||||
|
@ -1003,12 +1003,25 @@ class ResultViewModel2 : ViewModel() {
|
||||||
name: String,
|
name: String,
|
||||||
year: Int?,
|
year: Int?,
|
||||||
data: List<DataStoreHelper.BaseSearchResponse>,
|
data: List<DataStoreHelper.BaseSearchResponse>,
|
||||||
alertCallback: (Boolean, Int?) -> Unit
|
checkDuplicatesCallback: (shouldContinue: Boolean, duplicateId: Int?) -> Unit
|
||||||
) {
|
) {
|
||||||
|
fun normalizeString(input: String): String {
|
||||||
|
/**
|
||||||
|
* Trim the input string and replace consecutive spaces with a single space.
|
||||||
|
* This covers some edge-cases where the title does not match exactly across providers,
|
||||||
|
* and one provider has the title with an extra whitespace. This is minor enough that
|
||||||
|
* it should still match in this case.
|
||||||
|
*/
|
||||||
|
return input.trim().replace("\\s+".toRegex(), " ")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO support checking IMDB ID rather than name + year when available
|
// TODO support checking IMDB ID rather than name + year when available
|
||||||
val duplicateEntry = data.find { it.name == name && it.year == year }
|
val duplicateEntry = data.find {
|
||||||
|
// Normalize both strings for comparison
|
||||||
|
normalizeString(it.name) == normalizeString(name) && it.year == year
|
||||||
|
}
|
||||||
if (duplicateEntry == null || context == null) {
|
if (duplicateEntry == null || context == null) {
|
||||||
alertCallback.invoke(true, null)
|
checkDuplicatesCallback.invoke(true, null)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1018,13 +1031,13 @@ class ResultViewModel2 : ViewModel() {
|
||||||
DialogInterface.OnClickListener { _, which ->
|
DialogInterface.OnClickListener { _, which ->
|
||||||
when (which) {
|
when (which) {
|
||||||
DialogInterface.BUTTON_POSITIVE -> {
|
DialogInterface.BUTTON_POSITIVE -> {
|
||||||
alertCallback.invoke(true, null)
|
checkDuplicatesCallback.invoke(true, null)
|
||||||
}
|
}
|
||||||
DialogInterface.BUTTON_NEGATIVE -> {
|
DialogInterface.BUTTON_NEGATIVE -> {
|
||||||
alertCallback.invoke(false, null)
|
checkDuplicatesCallback.invoke(false, null)
|
||||||
}
|
}
|
||||||
DialogInterface.BUTTON_NEUTRAL -> {
|
DialogInterface.BUTTON_NEUTRAL -> {
|
||||||
alertCallback.invoke(true, duplicateEntry.id)
|
checkDuplicatesCallback.invoke(true, duplicateEntry.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1032,7 +1045,7 @@ class ResultViewModel2 : ViewModel() {
|
||||||
builder.setTitle(R.string.duplicate_title)
|
builder.setTitle(R.string.duplicate_title)
|
||||||
.setMessage(
|
.setMessage(
|
||||||
context.getString(message).format(
|
context.getString(message).format(
|
||||||
duplicateEntry.name,
|
normalizeString(duplicateEntry.name),
|
||||||
context.getString(
|
context.getString(
|
||||||
getResultWatchState(duplicateEntry.id ?: 0).stringRes
|
getResultWatchState(duplicateEntry.id ?: 0).stringRes
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue