diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index 0b2ac21a..6aa33e44 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -60,6 +60,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStri import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringResumeWatching import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringSearch import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi +import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringSearch import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.inAppAuths import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.WatchType @@ -447,6 +448,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { //private var mCastSession: CastSession? = null lateinit var mSessionManager: SessionManager private val mSessionManagerListener: SessionManagerListener by lazy { SessionManagerListenerImpl() } + private val accountsLoginLock = Mutex() private inner class SessionManagerListenerImpl : SessionManagerListener { override fun onSessionStarting(session: Session) { @@ -508,7 +510,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { logError(e) } val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) - if (githubApi.getLatestLoginData() != null && settingsManager.getBoolean(getString(R.string.automatic_cloud_backups), false)) { + if (githubApi.getLatestLoginData() != null && settingsManager.getBoolean(getString(R.string.automatic_cloud_backups), true)) { this@MainActivity.backupGithub() } } @@ -648,36 +650,51 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { } } - lateinit var viewModel: ResultViewModel2 +lateinit var viewModel: ResultViewModel2 - override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? { - viewModel = - ViewModelProvider(this)[ResultViewModel2::class.java] +override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? { + viewModel = + ViewModelProvider(this)[ResultViewModel2::class.java] - return super.onCreateView(name, context, attrs) - } + return super.onCreateView(name, context, attrs) +} - private fun hidePreviewPopupDialog() { - viewModel.clear() - bottomPreviewPopup.dismissSafe(this) - } +private fun hidePreviewPopupDialog() { + viewModel.clear() + bottomPreviewPopup.dismissSafe(this) +} - var bottomPreviewPopup: BottomSheetDialog? = null - private fun showPreviewPopupDialog(): BottomSheetDialog { - val ret = (bottomPreviewPopup ?: run { - val builder = - BottomSheetDialog(this) - builder.setContentView(R.layout.bottom_resultview_preview) - builder.setOnDismissListener { - bottomPreviewPopup = null - viewModel.clear() +var bottomPreviewPopup: BottomSheetDialog? = null +private fun showPreviewPopupDialog(): BottomSheetDialog { + val ret = (bottomPreviewPopup ?: run { + val builder = + BottomSheetDialog(this) + builder.setContentView(R.layout.bottom_resultview_preview) + builder.setOnDismissListener { + bottomPreviewPopup = null + viewModel.clear() + } + builder.setCanceledOnTouchOutside(true) + builder.show() + builder + }) + bottomPreviewPopup = ret + return ret + + override fun onStart() { + val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) + super.onStart() + ioSafe { + accountsLoginLock.withLock { + if (githubApi.getLatestLoginData() != null && settingsManager.getBoolean( + getString(R.string.automatic_cloud_backups), + true + ) + ) { + context?.restorePromptGithub() + } } - builder.setCanceledOnTouchOutside(true) - builder.show() - builder - }) - bottomPreviewPopup = ret - return ret + } } override fun onCreate(savedInstanceState: Bundle?) { @@ -853,15 +870,17 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { // init accounts ioSafe { - for (api in accountManagers) { - api.init() - } + accountsLoginLock.withLock{ + for (api in accountManagers) { + api.init() + } - inAppAuths.amap { api -> - try { - api.initialize() - } catch (e: Exception) { - logError(e) + inAppAuths.amap { api -> + try { + api.initialize() + } catch (e: Exception) { + logError(e) + } } } } @@ -872,10 +891,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { initAll() // No duplicates (which can happen by registerMainAPI) apis = allProviders.distinctBy { it } - - if (githubApi.getLatestLoginData() != null && settingsManager.getBoolean(getString(R.string.automatic_cloud_backups), false)){ - context?.restorePromptGithub() - } } // val navView: BottomNavigationView = findViewById(R.id.nav_view) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 42bf2b02..06137a57 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -84,7 +84,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ if (repo?.isEmpty() == true){ val backupData = context?.getBackup() - val gitresponse = app.post("https://api.github.com/gists", + val gitResponse = app.post("https://api.github.com/gists", headers= mapOf( Pair("Accept" , "application/vnd.github+json"), Pair("Authorization", "token $githubToken"), @@ -92,8 +92,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ requestBody = GistRequestBody("Cloudstream private backup gist", false, FilesGist(ContentFilesGist(backupData?.toJson()))).toJson().toRequestBody( RequestBodyTypes.JSON.toMediaTypeOrNull())) - if (!gitresponse.isSuccessful) {return false} - tryParseJson(gitresponse.text).let { + if (!gitResponse.isSuccessful) {return false} + tryParseJson(gitResponse.text).let { setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( token = githubToken, gistId = it?.gistId?: run { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt index 8a8f90b4..da8a0f8b 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeFragment.kt @@ -93,6 +93,7 @@ class HomeFragment : Fragment() { val configEvent = Event() var currentSpan = 1 val listHomepageItems = mutableListOf() + val reloadStoredDataEvent = Event() private val errorProfilePics = listOf( R.drawable.monke_benene, @@ -486,6 +487,10 @@ class HomeFragment : Fragment() { super.onStop() } + private fun reloadStoredEvent(input: Unit) { + reloadStored() + } + private fun reloadStored() { homeViewModel.loadResumeWatching() val list = EnumSet.noneOf(WatchType::class.java) diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index a27c1c12..892defef 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -33,6 +33,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_USER_KEY import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY +import com.lagradost.cloudstream3.ui.home.HomeFragment import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.toJson @@ -83,7 +84,7 @@ object BackupUtils { /** false if blacklisted key */ private fun String.isTransferable(): Boolean { - return !nonTransferableKeys.contains(this) + return !nonTransferableKeys.contains(this) and !nonTransferableKeys.any { this.endsWith(it) } } private var restoreFileSelector: ActivityResultLauncher>? = null @@ -300,7 +301,7 @@ object BackupUtils { fun FragmentActivity.backupGithub(){ - val backup = this.getBackup() + val backup = this.getBackup().toJson() val gistId = githubApi.getLatestLoginData()?.server ?: throw IllegalArgumentException ("Requires Username") val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") @@ -314,7 +315,7 @@ object BackupUtils { requestBody = GithubApi.GistRequestBody( "Cloudstream private backup gist", false, - GithubApi.FilesGist(GithubApi.ContentFilesGist(backup.toJson()))) + GithubApi.FilesGist(GithubApi.ContentFilesGist(backup))) .toJson() .toRequestBody(RequestBodyTypes.JSON.toMediaTypeOrNull()) ) @@ -327,15 +328,16 @@ object BackupUtils { } suspend fun Context.restorePromptGithub() { val gistId = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException() - val jsondata = app.get(" https://api.github.com/gists/$gistId").text - val dataraw = - parseJson(jsondata ?: "").files.values.first().dataRaw + val jsonData = app.get("https://api.github.com/gists/$gistId").text + val dataRaw = + parseJson(jsonData ?: "").files.values.first().dataRaw ?: throw IllegalAccessException() - val data = parseJson(dataraw) + val data = parseJson(dataRaw) restore( data, restoreSettings = true, restoreDataStore = true ) + HomeFragment.reloadStoredDataEvent.invoke(Unit) } } diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index f27f7c13..b0863ae5 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -141,6 +141,8 @@ Sincronizza automaticamente gli episodi guardati Ripristinare i dati da backup + Backup automatico su Github + Backup data File di backup caricato Impossibile ripristinare i dati dal file %s @@ -343,7 +345,7 @@ Nessun ritardo dei sottotitoli @@ -507,4 +509,4 @@ L\'app verrĂ  aggiornata all\'uscita Aggiornamento avviato Plugin scaricato - \ No newline at end of file +