funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt

106 lines
2.9 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.utils
2019-08-19 16:50:33 +02:00
2021-07-23 14:10:13 +02:00
import android.content.Context
2019-08-19 16:50:33 +02:00
import android.os.Build
import androidx.fragment.app.Fragment
import audio.funkwhale.ffa.R
import audio.funkwhale.ffa.fragments.BrowseFragment
import audio.funkwhale.ffa.repositories.Repository
import com.github.kittinunf.fuel.core.Request
import com.google.android.exoplayer2.offline.Download
import com.google.gson.Gson
2019-10-22 21:56:33 +02:00
import com.squareup.picasso.Picasso
import com.squareup.picasso.RequestCreator
2021-07-23 14:10:13 +02:00
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
2019-08-19 16:50:33 +02:00
import kotlinx.coroutines.Dispatchers.Main
2019-10-31 16:17:37 +01:00
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
2019-08-19 16:50:33 +02:00
import kotlinx.coroutines.launch
2021-07-23 14:10:13 +02:00
import kotlinx.coroutines.runBlocking
import net.openid.appauth.ClientSecretPost
2019-08-19 16:50:33 +02:00
import kotlin.coroutines.CoroutineContext
2021-07-23 14:10:13 +02:00
inline fun <D> Flow<Repository.Response<D>>.untilNetwork(
scope: CoroutineScope,
context: CoroutineContext = Main,
crossinline callback: (data: List<D>, isCache: Boolean, page: Int, hasMore: Boolean) -> Unit
) {
scope.launch(context) {
2019-10-31 16:17:37 +01:00
collect { data ->
callback(data.data, data.origin == Repository.Origin.Cache, data.page, data.hasMore)
2019-08-19 16:50:33 +02:00
}
}
}
fun Fragment.onViewPager(block: Fragment.() -> Unit) {
for (f in activity?.supportFragmentManager?.fragments ?: listOf()) {
if (f is BrowseFragment) {
f.block()
}
}
}
fun <T> Int.onApi(block: () -> T) {
if (Build.VERSION.SDK_INT >= this) {
block()
}
}
fun <T, U> Int.onApi(block: () -> T, elseBlock: (() -> U)) {
if (Build.VERSION.SDK_INT >= this) {
block()
} else {
elseBlock()
}
}
fun <T> Int.onApiForResult(block: () -> T, elseBlock: (() -> T)): T {
2019-10-21 11:51:32 +02:00
return if (Build.VERSION.SDK_INT >= this) {
block()
2019-08-19 16:50:33 +02:00
} else {
2019-10-21 11:51:32 +02:00
elseBlock()
2019-08-19 16:50:33 +02:00
}
}
fun <T> T.applyOnApi(api: Int, block: T.() -> T): T {
2019-10-21 11:51:32 +02:00
return if (Build.VERSION.SDK_INT >= api) {
block()
2019-08-19 16:50:33 +02:00
} else {
2019-10-21 11:51:32 +02:00
this
2019-08-19 16:50:33 +02:00
}
2019-10-22 21:56:33 +02:00
}
fun Picasso.maybeLoad(url: String?): RequestCreator {
return if (url == null) load(R.drawable.cover)
else load(url)
}
2021-07-23 14:10:13 +02:00
fun Request.authorize(context: Context): Request {
return runBlocking {
this@authorize.apply {
if (!Settings.isAnonymous()) {
2021-07-30 10:57:49 +02:00
val oauth = OAuthFactory.instance()
oauth.state().let { state ->
2021-07-23 14:10:13 +02:00
val old = state.accessToken
2021-07-30 10:57:49 +02:00
val auth = ClientSecretPost(oauth.state().clientSecret)
2021-07-23 14:10:13 +02:00
val done = CompletableDeferred<Boolean>()
2021-07-30 10:57:49 +02:00
state.performActionWithFreshTokens(oauth.service(context), auth) { token, _, _ ->
2021-07-23 14:10:13 +02:00
if (token != old && token != null) {
state.save()
}
2021-07-30 10:57:49 +02:00
header("Authorization", "Bearer ${oauth.state().accessToken}")
2021-07-23 14:10:13 +02:00
done.complete(true)
}
done.await()
return@runBlocking this
}
}
}
}
}
2021-07-23 14:10:13 +02:00
fun Download.getMetadata(): DownloadInfo? =
Gson().fromJson(String(this.request.data), DownloadInfo::class.java)