diff --git a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt index cb70d77ff..a2fe5ba2a 100644 --- a/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt +++ b/app/src/main/java/com/github/libretube/api/PlaylistsHelper.kt @@ -14,7 +14,7 @@ import com.github.libretube.db.obj.LocalPlaylist import com.github.libretube.enums.PlaylistType import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toID -import com.github.libretube.extensions.toastFromMainThread +import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.ProxyHelper import com.github.libretube.obj.ImportPlaylist @@ -68,24 +68,22 @@ object PlaylistsHelper { } suspend fun createPlaylist(playlistName: String, appContext: Context?): String? { - if (!loggedIn) { + return if (!loggedIn) { val playlist = LocalPlaylist(name = playlistName, thumbnailUrl = "") - DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist) - return DatabaseHolder.Database.localPlaylistsDao().getAll() - .last().playlist.id.toString() + DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist).toString() } else { - return try { + try { RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName)) } catch (e: IOException) { - appContext?.toastFromMainThread(R.string.unknown_error) + appContext?.toastFromMainDispatcher(R.string.unknown_error) return null } catch (e: HttpException) { Log.e(TAG(), e.toString()) - appContext?.toastFromMainThread(R.string.server_error) + appContext?.toastFromMainDispatcher(R.string.server_error) return null - }.playlistId.also { - appContext?.toastFromMainThread(R.string.playlistCreated) - } + }.playlistId + }.also { + appContext?.toastFromMainDispatcher(R.string.playlistCreated) } } @@ -206,7 +204,7 @@ object PlaylistsHelper { val playlist = try { RetrofitInstance.api.getPlaylist(playlistId) } catch (e: Exception) { - appContext.toastFromMainThread(R.string.server_error) + appContext.toastFromMainDispatcher(R.string.server_error) return null } val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext) ?: return null diff --git a/app/src/main/java/com/github/libretube/db/dao/LocalPlaylistsDao.kt b/app/src/main/java/com/github/libretube/db/dao/LocalPlaylistsDao.kt index bba3429fe..112855374 100644 --- a/app/src/main/java/com/github/libretube/db/dao/LocalPlaylistsDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/LocalPlaylistsDao.kt @@ -17,7 +17,7 @@ interface LocalPlaylistsDao { suspend fun getAll(): List @Insert - suspend fun createPlaylist(playlist: LocalPlaylist) + suspend fun createPlaylist(playlist: LocalPlaylist): Long @Update suspend fun updatePlaylist(playlist: LocalPlaylist) diff --git a/app/src/main/java/com/github/libretube/extensions/ToastFromMainThread.kt b/app/src/main/java/com/github/libretube/extensions/ToastFromMainThread.kt index 22d6337ab..32d36f87f 100644 --- a/app/src/main/java/com/github/libretube/extensions/ToastFromMainThread.kt +++ b/app/src/main/java/com/github/libretube/extensions/ToastFromMainThread.kt @@ -21,10 +21,12 @@ fun Context.toastFromMainThread(stringId: Int) { toastFromMainThread(getString(stringId)) } -suspend fun Context.toastFromMainDispatcher(text: String) = withContext(Dispatchers.Main) { - Toast.makeText(this@toastFromMainDispatcher, text, Toast.LENGTH_SHORT).show() +suspend fun Context.toastFromMainDispatcher(text: String, length: Int = Toast.LENGTH_SHORT) = withContext( + Dispatchers.Main +) { + Toast.makeText(this@toastFromMainDispatcher, text, length).show() } -suspend fun Context.toastFromMainDispatcher(stringId: Int) { - toastFromMainDispatcher(getString(stringId)) +suspend fun Context.toastFromMainDispatcher(stringId: Int, length: Int = Toast.LENGTH_SHORT) { + toastFromMainDispatcher(getString(stringId), length) } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt index da9ad2a70..2be47250c 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt @@ -13,7 +13,7 @@ import com.github.libretube.api.PlaylistsHelper import com.github.libretube.api.RetrofitInstance import com.github.libretube.databinding.DialogAddToPlaylistBinding import com.github.libretube.extensions.TAG -import com.github.libretube.extensions.toastFromMainThread +import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.ui.models.PlaylistViewModel import com.github.libretube.util.PlayingQueue import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -90,10 +90,10 @@ class AddToPlaylistDialog( PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray()) } catch (e: Exception) { Log.e(TAG(), e.toString()) - appContext.toastFromMainThread(R.string.unknown_error) + appContext.toastFromMainDispatcher(R.string.unknown_error) return } - appContext.toastFromMainThread( + appContext.toastFromMainDispatcher( if (success) R.string.added_to_playlist else R.string.fail ) } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt index 1bd9f8d36..753907389 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt @@ -8,7 +8,7 @@ import androidx.lifecycle.lifecycleScope import com.github.libretube.R import com.github.libretube.api.PlaylistsHelper import com.github.libretube.databinding.DialogCreatePlaylistBinding -import com.github.libretube.extensions.toastFromMainThread +import com.github.libretube.extensions.toastFromMainDispatcher import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -34,7 +34,7 @@ class CreatePlaylistDialog( if (playlistId != null) { onSuccess() } - appContext?.toastFromMainThread( + appContext?.toastFromMainDispatcher( if (playlistId != null) R.string.playlistCloned else R.string.server_error ) dismiss() diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt index 324f174e1..422cef983 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt @@ -14,8 +14,11 @@ import com.github.libretube.api.obj.Login import com.github.libretube.api.obj.Token import com.github.libretube.databinding.DialogLoginBinding import com.github.libretube.extensions.TAG +import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.helpers.PreferenceHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.serialization.decodeFromString import retrofit2.HttpException @@ -62,7 +65,7 @@ class LoginDialog( private fun signIn(username: String, password: String, createNewAccount: Boolean = false) { val login = Login(username, password) - lifecycleScope.launchWhenCreated { + lifecycleScope.launch(Dispatchers.IO) { val response = try { if (createNewAccount) { RetrofitInstance.authApi.register(login) @@ -73,25 +76,23 @@ class LoginDialog( val errorMessage = e.response()?.errorBody()?.string()?.runCatching { JsonHelper.json.decodeFromString(this).error }?.getOrNull() ?: context?.getString(R.string.server_error) ?: "" - Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show() - return@launchWhenCreated + context?.toastFromMainDispatcher(errorMessage) + return@launch } catch (e: Exception) { Log.e(TAG(), e.toString()) - Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show() - return@launchWhenCreated + context?.toastFromMainDispatcher(e.localizedMessage.orEmpty()) + return@launch } if (response.error != null) { - Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show() - return@launchWhenCreated + context?.toastFromMainDispatcher(response.error) + return@launch } - if (response.token == null) return@launchWhenCreated + if (response.token == null) return@launch - Toast.makeText( - context, - if (createNewAccount) R.string.registered else R.string.loggedIn, - Toast.LENGTH_SHORT - ).show() + context?.toastFromMainDispatcher( + if (createNewAccount) R.string.registered else R.string.loggedIn + ) PreferenceHelper.setToken(response.token) PreferenceHelper.setUsername(login.username) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt index 3bcd1b1c3..a8783f341 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt @@ -12,7 +12,7 @@ import com.github.libretube.R import com.github.libretube.api.PlaylistsHelper import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.extensions.TAG -import com.github.libretube.extensions.toastFromMainThread +import com.github.libretube.extensions.toastFromMainDispatcher import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -57,14 +57,14 @@ class RenamePlaylistDialog( } } catch (e: Exception) { Log.e(TAG(), e.toString()) - e.localizedMessage?.let { appContext.toastFromMainThread(it) } + e.localizedMessage?.let { appContext.toastFromMainDispatcher(it) } return@launch } if (success) { - appContext.toastFromMainThread(R.string.success) + appContext.toastFromMainDispatcher(R.string.success) onSuccess.invoke(newPlaylistName) } else { - appContext.toastFromMainThread(R.string.server_error) + appContext.toastFromMainDispatcher(R.string.server_error) } dismiss() } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 6a8ed8955..d7546eb00 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -60,6 +60,7 @@ import com.github.libretube.enums.ShareObjectType import com.github.libretube.extensions.formatShort import com.github.libretube.extensions.hideKeyboard import com.github.libretube.extensions.toID +import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.extensions.updateParameters import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.DashHelper @@ -666,17 +667,13 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { streams = try { RetrofitInstance.api.getStreams(videoId!!) } catch (e: IOException) { - withContext(Dispatchers.Main) { - Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_LONG).show() - } + context?.toastFromMainDispatcher(R.string.unknown_error, Toast.LENGTH_LONG) return@launch } catch (e: HttpException) { val errorMessage = e.response()?.errorBody()?.string()?.runCatching { JsonHelper.json.decodeFromString(this).message }?.getOrNull() ?: context?.getString(R.string.server_error) ?: "" - withContext(Dispatchers.Main) { - Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show() - } + context?.toastFromMainDispatcher(errorMessage, Toast.LENGTH_LONG) return@launch } diff --git a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt index ebf95a81f..91b67eab4 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt @@ -14,7 +14,7 @@ import com.github.libretube.constants.FALLBACK_INSTANCES_URL import com.github.libretube.constants.PIPED_INSTANCES_URL import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder.Database -import com.github.libretube.extensions.toastFromMainThread +import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.dialogs.CustomInstanceDialog @@ -142,7 +142,7 @@ class InstanceSettings : BasePreferenceFragment() { }.getOrNull() ?: runCatching { RetrofitInstance.externalApi.getInstances(FALLBACK_INSTANCES_URL).toMutableList() }.getOrNull() ?: run { - appContext.toastFromMainThread(R.string.failed_fetching_instances) + appContext.toastFromMainDispatcher(R.string.failed_fetching_instances) val instanceNames = resources.getStringArray(R.array.instances) resources.getStringArray(R.array.instancesValue).mapIndexed { index, instanceValue -> Instances(instanceNames[index], instanceValue) diff --git a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt index 88c587257..d96e4ff76 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt @@ -8,7 +8,7 @@ import com.github.libretube.db.DatabaseHolder import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.ShareObjectType import com.github.libretube.extensions.toID -import com.github.libretube.extensions.toastFromMainThread +import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.obj.ShareData import com.github.libretube.ui.dialogs.DeletePlaylistDialog @@ -70,7 +70,7 @@ class PlaylistOptionsBottomSheet( val playlistId = withContext(Dispatchers.IO) { PlaylistsHelper.clonePlaylist(context, playlistId) } - context.toastFromMainThread( + context.toastFromMainDispatcher( if (playlistId != null) R.string.playlistCloned else R.string.server_error ) }