mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
Use the toastFromMainThread
extension
This commit is contained in:
parent
50a7acb5d7
commit
42000a74dd
@ -14,7 +14,7 @@ import com.github.libretube.db.obj.LocalPlaylist
|
|||||||
import com.github.libretube.enums.PlaylistType
|
import com.github.libretube.enums.PlaylistType
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.toID
|
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.PreferenceHelper
|
||||||
import com.github.libretube.helpers.ProxyHelper
|
import com.github.libretube.helpers.ProxyHelper
|
||||||
import com.github.libretube.obj.ImportPlaylist
|
import com.github.libretube.obj.ImportPlaylist
|
||||||
@ -68,24 +68,22 @@ object PlaylistsHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun createPlaylist(playlistName: String, appContext: Context?): String? {
|
suspend fun createPlaylist(playlistName: String, appContext: Context?): String? {
|
||||||
if (!loggedIn) {
|
return if (!loggedIn) {
|
||||||
val playlist = LocalPlaylist(name = playlistName, thumbnailUrl = "")
|
val playlist = LocalPlaylist(name = playlistName, thumbnailUrl = "")
|
||||||
DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist)
|
DatabaseHolder.Database.localPlaylistsDao().createPlaylist(playlist).toString()
|
||||||
return DatabaseHolder.Database.localPlaylistsDao().getAll()
|
|
||||||
.last().playlist.id.toString()
|
|
||||||
} else {
|
} else {
|
||||||
return try {
|
try {
|
||||||
RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName))
|
RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName))
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
appContext?.toastFromMainThread(R.string.unknown_error)
|
appContext?.toastFromMainDispatcher(R.string.unknown_error)
|
||||||
return null
|
return null
|
||||||
} catch (e: HttpException) {
|
} catch (e: HttpException) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
appContext?.toastFromMainThread(R.string.server_error)
|
appContext?.toastFromMainDispatcher(R.string.server_error)
|
||||||
return null
|
return null
|
||||||
}.playlistId.also {
|
}.playlistId
|
||||||
appContext?.toastFromMainThread(R.string.playlistCreated)
|
}.also {
|
||||||
}
|
appContext?.toastFromMainDispatcher(R.string.playlistCreated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +204,7 @@ object PlaylistsHelper {
|
|||||||
val playlist = try {
|
val playlist = try {
|
||||||
RetrofitInstance.api.getPlaylist(playlistId)
|
RetrofitInstance.api.getPlaylist(playlistId)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
appContext.toastFromMainThread(R.string.server_error)
|
appContext.toastFromMainDispatcher(R.string.server_error)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext) ?: return null
|
val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext) ?: return null
|
||||||
|
@ -17,7 +17,7 @@ interface LocalPlaylistsDao {
|
|||||||
suspend fun getAll(): List<LocalPlaylistWithVideos>
|
suspend fun getAll(): List<LocalPlaylistWithVideos>
|
||||||
|
|
||||||
@Insert
|
@Insert
|
||||||
suspend fun createPlaylist(playlist: LocalPlaylist)
|
suspend fun createPlaylist(playlist: LocalPlaylist): Long
|
||||||
|
|
||||||
@Update
|
@Update
|
||||||
suspend fun updatePlaylist(playlist: LocalPlaylist)
|
suspend fun updatePlaylist(playlist: LocalPlaylist)
|
||||||
|
@ -21,10 +21,12 @@ fun Context.toastFromMainThread(stringId: Int) {
|
|||||||
toastFromMainThread(getString(stringId))
|
toastFromMainThread(getString(stringId))
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun Context.toastFromMainDispatcher(text: String) = withContext(Dispatchers.Main) {
|
suspend fun Context.toastFromMainDispatcher(text: String, length: Int = Toast.LENGTH_SHORT) = withContext(
|
||||||
Toast.makeText(this@toastFromMainDispatcher, text, Toast.LENGTH_SHORT).show()
|
Dispatchers.Main
|
||||||
|
) {
|
||||||
|
Toast.makeText(this@toastFromMainDispatcher, text, length).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun Context.toastFromMainDispatcher(stringId: Int) {
|
suspend fun Context.toastFromMainDispatcher(stringId: Int, length: Int = Toast.LENGTH_SHORT) {
|
||||||
toastFromMainDispatcher(getString(stringId))
|
toastFromMainDispatcher(getString(stringId), length)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import com.github.libretube.api.PlaylistsHelper
|
|||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
import com.github.libretube.databinding.DialogAddToPlaylistBinding
|
import com.github.libretube.databinding.DialogAddToPlaylistBinding
|
||||||
import com.github.libretube.extensions.TAG
|
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.ui.models.PlaylistViewModel
|
||||||
import com.github.libretube.util.PlayingQueue
|
import com.github.libretube.util.PlayingQueue
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
@ -90,10 +90,10 @@ class AddToPlaylistDialog(
|
|||||||
PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray())
|
PlaylistsHelper.addToPlaylist(playlistId, *streams.toTypedArray())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
appContext.toastFromMainThread(R.string.unknown_error)
|
appContext.toastFromMainDispatcher(R.string.unknown_error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
appContext.toastFromMainThread(
|
appContext.toastFromMainDispatcher(
|
||||||
if (success) R.string.added_to_playlist else R.string.fail
|
if (success) R.string.added_to_playlist else R.string.fail
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import androidx.lifecycle.lifecycleScope
|
|||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.PlaylistsHelper
|
import com.github.libretube.api.PlaylistsHelper
|
||||||
import com.github.libretube.databinding.DialogCreatePlaylistBinding
|
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 com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -34,7 +34,7 @@ class CreatePlaylistDialog(
|
|||||||
if (playlistId != null) {
|
if (playlistId != null) {
|
||||||
onSuccess()
|
onSuccess()
|
||||||
}
|
}
|
||||||
appContext?.toastFromMainThread(
|
appContext?.toastFromMainDispatcher(
|
||||||
if (playlistId != null) R.string.playlistCloned else R.string.server_error
|
if (playlistId != null) R.string.playlistCloned else R.string.server_error
|
||||||
)
|
)
|
||||||
dismiss()
|
dismiss()
|
||||||
|
@ -14,8 +14,11 @@ import com.github.libretube.api.obj.Login
|
|||||||
import com.github.libretube.api.obj.Token
|
import com.github.libretube.api.obj.Token
|
||||||
import com.github.libretube.databinding.DialogLoginBinding
|
import com.github.libretube.databinding.DialogLoginBinding
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
|
import com.github.libretube.extensions.toastFromMainDispatcher
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.decodeFromString
|
import kotlinx.serialization.decodeFromString
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
|
|
||||||
@ -62,7 +65,7 @@ class LoginDialog(
|
|||||||
|
|
||||||
private fun signIn(username: String, password: String, createNewAccount: Boolean = false) {
|
private fun signIn(username: String, password: String, createNewAccount: Boolean = false) {
|
||||||
val login = Login(username, password)
|
val login = Login(username, password)
|
||||||
lifecycleScope.launchWhenCreated {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
val response = try {
|
val response = try {
|
||||||
if (createNewAccount) {
|
if (createNewAccount) {
|
||||||
RetrofitInstance.authApi.register(login)
|
RetrofitInstance.authApi.register(login)
|
||||||
@ -73,25 +76,23 @@ class LoginDialog(
|
|||||||
val errorMessage = e.response()?.errorBody()?.string()?.runCatching {
|
val errorMessage = e.response()?.errorBody()?.string()?.runCatching {
|
||||||
JsonHelper.json.decodeFromString<Token>(this).error
|
JsonHelper.json.decodeFromString<Token>(this).error
|
||||||
}?.getOrNull() ?: context?.getString(R.string.server_error) ?: ""
|
}?.getOrNull() ?: context?.getString(R.string.server_error) ?: ""
|
||||||
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show()
|
context?.toastFromMainDispatcher(errorMessage)
|
||||||
return@launchWhenCreated
|
return@launch
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show()
|
context?.toastFromMainDispatcher(e.localizedMessage.orEmpty())
|
||||||
return@launchWhenCreated
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.error != null) {
|
if (response.error != null) {
|
||||||
Toast.makeText(context, response.error, Toast.LENGTH_SHORT).show()
|
context?.toastFromMainDispatcher(response.error)
|
||||||
return@launchWhenCreated
|
return@launch
|
||||||
}
|
}
|
||||||
if (response.token == null) return@launchWhenCreated
|
if (response.token == null) return@launch
|
||||||
|
|
||||||
Toast.makeText(
|
context?.toastFromMainDispatcher(
|
||||||
context,
|
if (createNewAccount) R.string.registered else R.string.loggedIn
|
||||||
if (createNewAccount) R.string.registered else R.string.loggedIn,
|
)
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
|
|
||||||
PreferenceHelper.setToken(response.token)
|
PreferenceHelper.setToken(response.token)
|
||||||
PreferenceHelper.setUsername(login.username)
|
PreferenceHelper.setUsername(login.username)
|
||||||
|
@ -12,7 +12,7 @@ import com.github.libretube.R
|
|||||||
import com.github.libretube.api.PlaylistsHelper
|
import com.github.libretube.api.PlaylistsHelper
|
||||||
import com.github.libretube.databinding.DialogTextPreferenceBinding
|
import com.github.libretube.databinding.DialogTextPreferenceBinding
|
||||||
import com.github.libretube.extensions.TAG
|
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 com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -57,14 +57,14 @@ class RenamePlaylistDialog(
|
|||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
e.localizedMessage?.let { appContext.toastFromMainThread(it) }
|
e.localizedMessage?.let { appContext.toastFromMainDispatcher(it) }
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
if (success) {
|
if (success) {
|
||||||
appContext.toastFromMainThread(R.string.success)
|
appContext.toastFromMainDispatcher(R.string.success)
|
||||||
onSuccess.invoke(newPlaylistName)
|
onSuccess.invoke(newPlaylistName)
|
||||||
} else {
|
} else {
|
||||||
appContext.toastFromMainThread(R.string.server_error)
|
appContext.toastFromMainDispatcher(R.string.server_error)
|
||||||
}
|
}
|
||||||
dismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@ import com.github.libretube.enums.ShareObjectType
|
|||||||
import com.github.libretube.extensions.formatShort
|
import com.github.libretube.extensions.formatShort
|
||||||
import com.github.libretube.extensions.hideKeyboard
|
import com.github.libretube.extensions.hideKeyboard
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
|
import com.github.libretube.extensions.toastFromMainDispatcher
|
||||||
import com.github.libretube.extensions.updateParameters
|
import com.github.libretube.extensions.updateParameters
|
||||||
import com.github.libretube.helpers.BackgroundHelper
|
import com.github.libretube.helpers.BackgroundHelper
|
||||||
import com.github.libretube.helpers.DashHelper
|
import com.github.libretube.helpers.DashHelper
|
||||||
@ -666,17 +667,13 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
streams = try {
|
streams = try {
|
||||||
RetrofitInstance.api.getStreams(videoId!!)
|
RetrofitInstance.api.getStreams(videoId!!)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
withContext(Dispatchers.Main) {
|
context?.toastFromMainDispatcher(R.string.unknown_error, Toast.LENGTH_LONG)
|
||||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_LONG).show()
|
|
||||||
}
|
|
||||||
return@launch
|
return@launch
|
||||||
} catch (e: HttpException) {
|
} catch (e: HttpException) {
|
||||||
val errorMessage = e.response()?.errorBody()?.string()?.runCatching {
|
val errorMessage = e.response()?.errorBody()?.string()?.runCatching {
|
||||||
JsonHelper.json.decodeFromString<Message>(this).message
|
JsonHelper.json.decodeFromString<Message>(this).message
|
||||||
}?.getOrNull() ?: context?.getString(R.string.server_error) ?: ""
|
}?.getOrNull() ?: context?.getString(R.string.server_error) ?: ""
|
||||||
withContext(Dispatchers.Main) {
|
context?.toastFromMainDispatcher(errorMessage, Toast.LENGTH_LONG)
|
||||||
Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show()
|
|
||||||
}
|
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.PIPED_INSTANCES_URL
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.db.DatabaseHolder.Database
|
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.helpers.PreferenceHelper
|
||||||
import com.github.libretube.ui.base.BasePreferenceFragment
|
import com.github.libretube.ui.base.BasePreferenceFragment
|
||||||
import com.github.libretube.ui.dialogs.CustomInstanceDialog
|
import com.github.libretube.ui.dialogs.CustomInstanceDialog
|
||||||
@ -142,7 +142,7 @@ class InstanceSettings : BasePreferenceFragment() {
|
|||||||
}.getOrNull() ?: runCatching {
|
}.getOrNull() ?: runCatching {
|
||||||
RetrofitInstance.externalApi.getInstances(FALLBACK_INSTANCES_URL).toMutableList()
|
RetrofitInstance.externalApi.getInstances(FALLBACK_INSTANCES_URL).toMutableList()
|
||||||
}.getOrNull() ?: run {
|
}.getOrNull() ?: run {
|
||||||
appContext.toastFromMainThread(R.string.failed_fetching_instances)
|
appContext.toastFromMainDispatcher(R.string.failed_fetching_instances)
|
||||||
val instanceNames = resources.getStringArray(R.array.instances)
|
val instanceNames = resources.getStringArray(R.array.instances)
|
||||||
resources.getStringArray(R.array.instancesValue).mapIndexed { index, instanceValue ->
|
resources.getStringArray(R.array.instancesValue).mapIndexed { index, instanceValue ->
|
||||||
Instances(instanceNames[index], instanceValue)
|
Instances(instanceNames[index], instanceValue)
|
||||||
|
@ -8,7 +8,7 @@ import com.github.libretube.db.DatabaseHolder
|
|||||||
import com.github.libretube.enums.PlaylistType
|
import com.github.libretube.enums.PlaylistType
|
||||||
import com.github.libretube.enums.ShareObjectType
|
import com.github.libretube.enums.ShareObjectType
|
||||||
import com.github.libretube.extensions.toID
|
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.helpers.BackgroundHelper
|
||||||
import com.github.libretube.obj.ShareData
|
import com.github.libretube.obj.ShareData
|
||||||
import com.github.libretube.ui.dialogs.DeletePlaylistDialog
|
import com.github.libretube.ui.dialogs.DeletePlaylistDialog
|
||||||
@ -70,7 +70,7 @@ class PlaylistOptionsBottomSheet(
|
|||||||
val playlistId = withContext(Dispatchers.IO) {
|
val playlistId = withContext(Dispatchers.IO) {
|
||||||
PlaylistsHelper.clonePlaylist(context, playlistId)
|
PlaylistsHelper.clonePlaylist(context, playlistId)
|
||||||
}
|
}
|
||||||
context.toastFromMainThread(
|
context.toastFromMainDispatcher(
|
||||||
if (playlistId != null) R.string.playlistCloned else R.string.server_error
|
if (playlistId != null) R.string.playlistCloned else R.string.server_error
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user