diff --git a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/SourceDetailFragment.kt b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/SourceDetailFragment.kt index 5c2a8085..0452395f 100644 --- a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/SourceDetailFragment.kt +++ b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/SourceDetailFragment.kt @@ -25,6 +25,7 @@ import com.futo.platformplayer.api.media.platforms.js.SourcePluginConfig import com.futo.platformplayer.logging.Logger import com.futo.platformplayer.states.StateApp import com.futo.platformplayer.states.StateDeveloper +import com.futo.platformplayer.states.StateHistory import com.futo.platformplayer.states.StatePlatform import com.futo.platformplayer.states.StatePlugins import com.futo.platformplayer.views.buttons.BigButton @@ -152,11 +153,50 @@ class SourceDetailFragment : MainFragment() { if(field is View) field.isVisible = false; } - if(!source.capabilities.hasGetUserHistory) { + if(!source.capabilities.hasGetUserHistory || !source.isLoggedIn) { val field = _settingsAppForm.findField("sync"); if(field is View) field.isVisible = false; } + else { + val field = _settingsAppForm.findField("syncHistory"); + field?.onChanged?.subscribe { field, new, old -> + if(old != new && new == true && StatePlatform.instance.isClientEnabled(config.id)) { + UIDialogs.showDialog(context, R.drawable.ic_sources, "Would you like to sync now?", + "This will attempt to update your history from the platform, when this setting is enabled, it is done during startup.", null, 0, + UIDialogs.Action("No", { + + }), + UIDialogs.Action("Yes", { + UIDialogs.showDialogProgress(context, { + it.setText("Importing history.."); + fragment.lifecycleScope.launch(Dispatchers.IO) { + try { + val client = StatePlatform.instance.getClient(config.id); + if (client != null && client is JSClient) { + val count = StateHistory.instance.syncRemoteHistory(client); + withContext(Dispatchers.Main) { + it.hide(); + if(count > 0) + UIDialogs.showDialogOk(context, R.drawable.ic_pair_success, "Imported ${count} history items"); + else + UIDialogs.showDialogOk(context, R.drawable.ic_help, "Imported no history items"); + } + + } + } + catch(ex: Throwable) { + withContext(Dispatchers.Main) { + UIDialogs.appToast("Sync History failed due to:\n" + ex.message); + it.hide(); + } + } + } + }); + }, UIDialogs.ActionStyle.PRIMARY)); + } + } + } _settingsAppForm.onChanged.clear(); _settingsAppForm.onChanged.subscribe { field, value -> _settingsAppChanged = true; diff --git a/app/src/main/java/com/futo/platformplayer/states/StateHistory.kt b/app/src/main/java/com/futo/platformplayer/states/StateHistory.kt index f3bcca55..26fc3170 100644 --- a/app/src/main/java/com/futo/platformplayer/states/StateHistory.kt +++ b/app/src/main/java/com/futo/platformplayer/states/StateHistory.kt @@ -194,17 +194,18 @@ class StateHistory { _remoteHistoryDatesStore.save(); } - fun syncRemoteHistory(plugin: JSClient) { + fun syncRemoteHistory(plugin: JSClient): Int { if (plugin.capabilities.hasGetUserHistory && plugin.isLoggedIn) { Logger.i(TAG, "Syncing remote history for plugin [${plugin.name}]"); val hist = StatePlatform.instance.getUserHistory(plugin.id); - syncRemoteHistory(plugin.id, hist, 100, 3); + return syncRemoteHistory(plugin.id, hist, 100, 3); } + return 0; } - fun syncRemoteHistory(pluginId: String, videos: IPager, maxVideos: Int, maxPages: Int) { + fun syncRemoteHistory(pluginId: String, videos: IPager, maxVideos: Int, maxPages: Int): Int { val lastDate = _remoteHistoryDatesStore.get(pluginId) ?: OffsetDateTime.MIN; val maxVideosCount = if(maxVideos <= 0) 500 else maxVideos; val maxPageCount = if(maxPages <= 0) 3 else maxPages; @@ -272,12 +273,14 @@ class StateHistory { } catch(ex: Throwable){} } + return updated; } } catch(ex: Throwable) { val plugin = if(pluginId != StateDeveloper.DEV_ID) StatePlugins.instance.getPlugin(pluginId) else null; Logger.e(TAG, "Sync Remote History failed for [${plugin?.config?.name}] due to: " + ex.message) } + return 0; } companion object {