Merge branch 'githubAccount' of https://github.com/antonydp/cloudstream into githubAccount

This commit is contained in:
antonydp 2023-01-23 22:10:36 +01:00
commit 4dd205c549
5 changed files with 74 additions and 50 deletions

View file

@ -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.appStringResumeWatching
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringSearch import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringSearch
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi 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.syncproviders.AccountManager.Companion.inAppAuths
import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.WatchType import com.lagradost.cloudstream3.ui.WatchType
@ -447,6 +448,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
//private var mCastSession: CastSession? = null //private var mCastSession: CastSession? = null
lateinit var mSessionManager: SessionManager lateinit var mSessionManager: SessionManager
private val mSessionManagerListener: SessionManagerListener<Session> by lazy { SessionManagerListenerImpl() } private val mSessionManagerListener: SessionManagerListener<Session> by lazy { SessionManagerListenerImpl() }
private val accountsLoginLock = Mutex()
private inner class SessionManagerListenerImpl : SessionManagerListener<Session> { private inner class SessionManagerListenerImpl : SessionManagerListener<Session> {
override fun onSessionStarting(session: Session) { override fun onSessionStarting(session: Session) {
@ -508,7 +510,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
logError(e) logError(e)
} }
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) 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() 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? { override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
viewModel = viewModel =
ViewModelProvider(this)[ResultViewModel2::class.java] ViewModelProvider(this)[ResultViewModel2::class.java]
return super.onCreateView(name, context, attrs) return super.onCreateView(name, context, attrs)
} }
private fun hidePreviewPopupDialog() { private fun hidePreviewPopupDialog() {
viewModel.clear() viewModel.clear()
bottomPreviewPopup.dismissSafe(this) bottomPreviewPopup.dismissSafe(this)
} }
var bottomPreviewPopup: BottomSheetDialog? = null var bottomPreviewPopup: BottomSheetDialog? = null
private fun showPreviewPopupDialog(): BottomSheetDialog { private fun showPreviewPopupDialog(): BottomSheetDialog {
val ret = (bottomPreviewPopup ?: run { val ret = (bottomPreviewPopup ?: run {
val builder = val builder =
BottomSheetDialog(this) BottomSheetDialog(this)
builder.setContentView(R.layout.bottom_resultview_preview) builder.setContentView(R.layout.bottom_resultview_preview)
builder.setOnDismissListener { builder.setOnDismissListener {
bottomPreviewPopup = null bottomPreviewPopup = null
viewModel.clear() 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?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -853,15 +870,17 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
// init accounts // init accounts
ioSafe { ioSafe {
for (api in accountManagers) { accountsLoginLock.withLock{
api.init() for (api in accountManagers) {
} api.init()
}
inAppAuths.amap { api -> inAppAuths.amap { api ->
try { try {
api.initialize() api.initialize()
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
}
} }
} }
} }
@ -872,10 +891,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
initAll() initAll()
// No duplicates (which can happen by registerMainAPI) // No duplicates (which can happen by registerMainAPI)
apis = allProviders.distinctBy { it } 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) // val navView: BottomNavigationView = findViewById(R.id.nav_view)

View file

@ -84,7 +84,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
if (repo?.isEmpty() == true){ if (repo?.isEmpty() == true){
val backupData = context?.getBackup() val backupData = context?.getBackup()
val gitresponse = app.post("https://api.github.com/gists", val gitResponse = app.post("https://api.github.com/gists",
headers= mapOf( headers= mapOf(
Pair("Accept" , "application/vnd.github+json"), Pair("Accept" , "application/vnd.github+json"),
Pair("Authorization", "token $githubToken"), 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( requestBody = GistRequestBody("Cloudstream private backup gist", false, FilesGist(ContentFilesGist(backupData?.toJson()))).toJson().toRequestBody(
RequestBodyTypes.JSON.toMediaTypeOrNull())) RequestBodyTypes.JSON.toMediaTypeOrNull()))
if (!gitresponse.isSuccessful) {return false} if (!gitResponse.isSuccessful) {return false}
tryParseJson<GistsElements>(gitresponse.text).let { tryParseJson<GistsElements>(gitResponse.text).let {
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
token = githubToken, token = githubToken,
gistId = it?.gistId?: run { gistId = it?.gistId?: run {

View file

@ -93,6 +93,7 @@ class HomeFragment : Fragment() {
val configEvent = Event<Int>() val configEvent = Event<Int>()
var currentSpan = 1 var currentSpan = 1
val listHomepageItems = mutableListOf<SearchResponse>() val listHomepageItems = mutableListOf<SearchResponse>()
val reloadStoredDataEvent = Event<Unit>()
private val errorProfilePics = listOf( private val errorProfilePics = listOf(
R.drawable.monke_benene, R.drawable.monke_benene,
@ -486,6 +487,10 @@ class HomeFragment : Fragment() {
super.onStop() super.onStop()
} }
private fun reloadStoredEvent(input: Unit) {
reloadStored()
}
private fun reloadStored() { private fun reloadStored() {
homeViewModel.loadResumeWatching() homeViewModel.loadResumeWatching()
val list = EnumSet.noneOf(WatchType::class.java) val list = EnumSet.noneOf(WatchType::class.java)

View file

@ -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_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.home.HomeFragment
import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.AppUtils.toJson import com.lagradost.cloudstream3.utils.AppUtils.toJson
@ -83,7 +84,7 @@ object BackupUtils {
/** false if blacklisted key */ /** false if blacklisted key */
private fun String.isTransferable(): Boolean { private fun String.isTransferable(): Boolean {
return !nonTransferableKeys.contains(this) return !nonTransferableKeys.contains(this) and !nonTransferableKeys.any { this.endsWith(it) }
} }
private var restoreFileSelector: ActivityResultLauncher<Array<String>>? = null private var restoreFileSelector: ActivityResultLauncher<Array<String>>? = null
@ -300,7 +301,7 @@ object BackupUtils {
fun FragmentActivity.backupGithub(){ fun FragmentActivity.backupGithub(){
val backup = this.getBackup() val backup = this.getBackup().toJson()
val gistId = githubApi.getLatestLoginData()?.server ?: throw IllegalArgumentException ("Requires Username") val gistId = githubApi.getLatestLoginData()?.server ?: throw IllegalArgumentException ("Requires Username")
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
@ -314,7 +315,7 @@ object BackupUtils {
requestBody = GithubApi.GistRequestBody( requestBody = GithubApi.GistRequestBody(
"Cloudstream private backup gist", "Cloudstream private backup gist",
false, false,
GithubApi.FilesGist(GithubApi.ContentFilesGist(backup.toJson()))) GithubApi.FilesGist(GithubApi.ContentFilesGist(backup)))
.toJson() .toJson()
.toRequestBody(RequestBodyTypes.JSON.toMediaTypeOrNull()) .toRequestBody(RequestBodyTypes.JSON.toMediaTypeOrNull())
) )
@ -327,15 +328,16 @@ object BackupUtils {
} }
suspend fun Context.restorePromptGithub() { suspend fun Context.restorePromptGithub() {
val gistId = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException() val gistId = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException()
val jsondata = app.get(" https://api.github.com/gists/$gistId").text val jsonData = app.get("https://api.github.com/gists/$gistId").text
val dataraw = val dataRaw =
parseJson<GithubApi.GistsElements>(jsondata ?: "").files.values.first().dataRaw parseJson<GithubApi.GistsElements>(jsonData ?: "").files.values.first().dataRaw
?: throw IllegalAccessException() ?: throw IllegalAccessException()
val data = parseJson<BackupFile>(dataraw) val data = parseJson<BackupFile>(dataRaw)
restore( restore(
data, data,
restoreSettings = true, restoreSettings = true,
restoreDataStore = true restoreDataStore = true
) )
HomeFragment.reloadStoredDataEvent.invoke(Unit)
} }
} }

View file

@ -141,6 +141,8 @@
<!--<string name="episode_sync_settings">Update watch progress</string>--> <!--<string name="episode_sync_settings">Update watch progress</string>-->
<string name="episode_sync_settings_des">Sincronizza automaticamente gli episodi guardati</string> <string name="episode_sync_settings_des">Sincronizza automaticamente gli episodi guardati</string>
<string name="restore_settings">Ripristinare i dati da backup</string> <string name="restore_settings">Ripristinare i dati da backup</string>
<string name="automatic_restore_settings">Backup automatico su Github</string>
<string name="backup_settings">Backup data</string> <string name="backup_settings">Backup data</string>
<string name="restore_success">File di backup caricato</string> <string name="restore_success">File di backup caricato</string>
<string name="restore_failed_format" formatted="true">Impossibile ripristinare i dati dal file %s</string> <string name="restore_failed_format" formatted="true">Impossibile ripristinare i dati dal file %s</string>
@ -343,7 +345,7 @@
<string name="subtitle_offset_extra_hint_none_format">Nessun ritardo dei sottotitoli</string> <string name="subtitle_offset_extra_hint_none_format">Nessun ritardo dei sottotitoli</string>
<!-- <!--
Example text (pangram) can optionally be translated; if you do, include all the letters in the alphabet, Example text (pangram) can optionally be translated; if you do, include all the letters in the alphabet,
see: see:
https://en.wikipedia.org/w/index.php?title=Pangram&oldid=225849300 https://en.wikipedia.org/w/index.php?title=Pangram&oldid=225849300
https://en.wikipedia.org/wiki/The_quick_brown_fox_jumps_over_the_lazy_dog https://en.wikipedia.org/wiki/The_quick_brown_fox_jumps_over_the_lazy_dog
--> -->
@ -507,4 +509,4 @@
<string name="delayed_update_notice">L\'app verrà aggiornata all\'uscita</string> <string name="delayed_update_notice">L\'app verrà aggiornata all\'uscita</string>
<string name="update_started">Aggiornamento avviato</string> <string name="update_started">Aggiornamento avviato</string>
<string name="plugin_downloaded">Plugin scaricato</string> <string name="plugin_downloaded">Plugin scaricato</string>
</resources> </resources>