delete resume watching + delete bookmarks buttons. fixed backup crash

This commit is contained in:
LagradOst 2023-08-27 19:07:08 +02:00
parent 7d3b8c464e
commit f01820059b
5 changed files with 72 additions and 66 deletions

View file

@ -658,12 +658,14 @@ class HomeFragment : Fragment() {
return@observeNullable return@observeNullable
} }
bottomSheetDialog = activity?.loadHomepageList(item, expandCallback = { val (items, delete) = item
bottomSheetDialog = activity?.loadHomepageList(items, expandCallback = {
homeViewModel.expandAndReturn(it) homeViewModel.expandAndReturn(it)
}, dismissCallback = { }, dismissCallback = {
homeViewModel.popup(null) homeViewModel.popup(null)
bottomSheetDialog = null bottomSheetDialog = null
}) }, deleteCallback = delete)
} }
homeViewModel.reloadStored() homeViewModel.reloadStored()

View file

@ -283,7 +283,11 @@ class HomeParentItemAdapterPreview(
item.plot ?: "" item.plot ?: ""
homePreviewText.text = item.name homePreviewText.text = item.name
populateChips(homePreviewTags,item.tags ?: emptyList(), R.style.ChipFilledSemiTransparent) populateChips(
homePreviewTags,
item.tags ?: emptyList(),
R.style.ChipFilledSemiTransparent
)
homePreviewTags.isGone = homePreviewTags.isGone =
item.tags.isNullOrEmpty() item.tags.isNullOrEmpty()
@ -422,8 +426,14 @@ class HomeParentItemAdapterPreview(
resumeRecyclerView.adapter = resumeAdapter resumeRecyclerView.adapter = resumeAdapter
bookmarkRecyclerView.adapter = bookmarkAdapter bookmarkRecyclerView.adapter = bookmarkAdapter
resumeRecyclerView.setLinearListLayout(nextLeft = R.id.nav_rail_view, nextRight = FOCUS_SELF) resumeRecyclerView.setLinearListLayout(
bookmarkRecyclerView.setLinearListLayout(nextLeft = R.id.nav_rail_view, nextRight = FOCUS_SELF) nextLeft = R.id.nav_rail_view,
nextRight = FOCUS_SELF
)
bookmarkRecyclerView.setLinearListLayout(
nextLeft = R.id.nav_rail_view,
nextRight = FOCUS_SELF
)
fixPaddingStatusbarMargin(topPadding) fixPaddingStatusbarMargin(topPadding)
@ -547,7 +557,10 @@ class HomeParentItemAdapterPreview(
resumeWatching, resumeWatching,
false false
), 1, false ), 1, false
) ),
deleteCallback = {
viewModel.deleteResumeWatching()
}
) )
} }
} }
@ -572,7 +585,9 @@ class HomeParentItemAdapterPreview(
list, list,
false false
), 1, false ), 1, false
) ), deleteCallback = {
viewModel.deleteBookmarks(list)
}
) )
} }
} }

View file

@ -41,6 +41,8 @@ import com.lagradost.cloudstream3.utils.AppUtils.loadResult
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE import com.lagradost.cloudstream3.utils.DOWNLOAD_HEADER_CACHE
import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.deleteAllBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.deleteAllResumeStateIds
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllResumeStateIds import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllResumeStateIds
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllWatchStateIds import com.lagradost.cloudstream3.utils.DataStoreHelper.getAllWatchStateIds
import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData
@ -92,6 +94,21 @@ class HomeViewModel : ViewModel() {
} }
} }
fun deleteResumeWatching() {
deleteAllResumeStateIds()
loadResumeWatching()
}
fun deleteBookmarks(list: List<SearchResponse>) {
list.forEach { DataStoreHelper.deleteBookmarkedData(it.id) }
loadStoredData()
}
fun deleteBookmarks() {
deleteAllBookmarkedData()
loadStoredData()
}
var repo: APIRepository? = null var repo: APIRepository? = null
private val _apiName = MutableLiveData<String>() private val _apiName = MutableLiveData<String>()
@ -394,11 +411,14 @@ class HomeViewModel : ViewModel() {
} }
private val _popup = MutableLiveData<ExpandableHomepageList?>(null) private val _popup = MutableLiveData<Pair<ExpandableHomepageList, (() -> Unit)?>?>(null)
val popup: LiveData<ExpandableHomepageList?> = _popup val popup: LiveData<Pair<ExpandableHomepageList, (() -> Unit)?>?> = _popup
fun popup(list: ExpandableHomepageList?) { fun popup(list: ExpandableHomepageList?, deleteCallback: (() -> Unit)? = null) {
_popup.postValue(list) if (list == null)
_popup.postValue(null)
else
_popup.postValue(list to deleteCallback)
} }
private fun bookmarksUpdated(unused: Boolean) { private fun bookmarksUpdated(unused: Boolean) {
@ -436,8 +456,7 @@ class HomeViewModel : ViewModel() {
// do nothing // do nothing
} }
fun reloadStored() { fun loadStoredData() {
loadResumeWatching()
val list = EnumSet.noneOf(WatchType::class.java) val list = EnumSet.noneOf(WatchType::class.java)
getKey<IntArray>(HOME_BOOKMARK_VALUE_LIST)?.map { WatchType.fromInternalId(it) }?.let { getKey<IntArray>(HOME_BOOKMARK_VALUE_LIST)?.map { WatchType.fromInternalId(it) }?.let {
list.addAll(it) list.addAll(it)
@ -445,6 +464,11 @@ class HomeViewModel : ViewModel() {
loadStoredData(list) loadStoredData(list)
} }
fun reloadStored() {
loadResumeWatching()
loadStoredData()
}
fun click(load: LoadClickCallback) { fun click(load: LoadClickCallback) {
loadResult(load.response.url, load.response.apiName, load.action) loadResult(load.response.url, load.response.apiName, load.action)
} }

View file

@ -25,6 +25,7 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_T
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY
import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY
import com.lagradost.cloudstream3.ui.result.txt
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
@ -143,66 +144,26 @@ object BackupUtils {
} }
@SuppressLint("SimpleDateFormat") @SuppressLint("SimpleDateFormat")
fun FragmentActivity.backup() { fun FragmentActivity.backup() = ioSafe {
var fileStream: OutputStream? = null var fileStream: OutputStream? = null
var printStream: PrintWriter? = null var printStream: PrintWriter? = null
try { try {
if (!checkWrite()) { if (!checkWrite()) {
showToast(getString(R.string.backup_failed), Toast.LENGTH_LONG) showToast(R.string.backup_failed, Toast.LENGTH_LONG)
requestRW() requestRW()
return return@ioSafe
} }
val date = SimpleDateFormat("yyyy_MM_dd_HH_mm").format(Date(currentTimeMillis())) val date = SimpleDateFormat("yyyy_MM_dd_HH_mm").format(Date(currentTimeMillis()))
val ext = "json" val ext = "txt"
val displayName = "CS3_Backup_${date}" val displayName = "CS3_Backup_${date}"
val backupFile = getBackup() val backupFile = getBackup()
val stream = setupStream(this, displayName, null, ext, false) val stream = setupStream(this@backup, displayName, null, ext, false)
fileStream = stream.openNew() fileStream = stream.openNew()
printStream = PrintWriter(fileStream) printStream = PrintWriter(fileStream)
printStream.print(mapper.writeValueAsString(backupFile)) printStream.print(mapper.writeValueAsString(backupFile))
/*val steam = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
&& subDir?.isDownloadDir() == true
) {
val cr = this.contentResolver
val contentUri =
MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) // USE INSTEAD OF MediaStore.Downloads.EXTERNAL_CONTENT_URI
//val currentMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
val newFile = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, displayName)
put(MediaStore.MediaColumns.TITLE, displayName)
// While it a json file we store as txt because not
// all file managers support mimetype json
put(MediaStore.MediaColumns.MIME_TYPE, "text/plain")
//put(MediaStore.MediaColumns.RELATIVE_PATH, folder)
}
val newFileUri = cr.insert(
contentUri,
newFile
) ?: throw IOException("Error creating file uri")
cr.openOutputStream(newFileUri, "w")
?: throw IOException("Error opening stream")
} else {
val fileName = "$displayName.$ext"
val rFile = subDir?.findFile(fileName)
if (rFile?.exists() == true) {
rFile.delete()
}
val file =
subDir?.createFile(fileName)
?: throw IOException("Error creating file")
if (!file.exists()) throw IOException("File does not exist")
file.openOutputStream()
}
val printStream = PrintWriter(steam)
printStream.print(mapper.writeValueAsString(backupFile))
printStream.close()*/
showToast( showToast(
R.string.backup_success, R.string.backup_success,
Toast.LENGTH_LONG Toast.LENGTH_LONG
@ -211,7 +172,7 @@ object BackupUtils {
logError(e) logError(e)
try { try {
showToast( showToast(
getString(R.string.backup_failed_error_format).format(e.toString()), txt(R.string.backup_failed_error_format, e.toString()),
Toast.LENGTH_LONG Toast.LENGTH_LONG
) )
} catch (e: Exception) { } catch (e: Exception) {

View file

@ -353,6 +353,12 @@ object DataStoreHelper {
removeKeys(folder2) removeKeys(folder2)
} }
fun deleteBookmarkedData(id : Int?) {
if (id == null) return
removeKey("$currentAccount/$RESULT_WATCH_STATE", id.toString())
removeKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())
}
fun getAllResumeStateIds(): List<Int>? { fun getAllResumeStateIds(): List<Int>? {
val folder = "$currentAccount/$RESULT_RESUME_WATCHING" val folder = "$currentAccount/$RESULT_RESUME_WATCHING"
return getKeys(folder)?.mapNotNull { return getKeys(folder)?.mapNotNull {
@ -519,12 +525,10 @@ object DataStoreHelper {
fun setResultWatchState(id: Int?, status: Int) { fun setResultWatchState(id: Int?, status: Int) {
if (id == null) return if (id == null) return
val folder = "$currentAccount/$RESULT_WATCH_STATE"
if (status == WatchType.NONE.internalId) { if (status == WatchType.NONE.internalId) {
removeKey(folder, id.toString()) deleteBookmarkedData(id)
removeKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())
} else { } else {
setKey(folder, id.toString(), status) setKey("$currentAccount/$RESULT_WATCH_STATE", id.toString(), status)
} }
} }