2021-07-12 10:14:26 +02:00
|
|
|
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
|
2021-08-09 20:04:33 +02:00
|
|
|
import android.util.Log
|
2019-08-19 16:50:33 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.R
|
|
|
|
import audio.funkwhale.ffa.fragments.BrowseFragment
|
2021-08-22 09:48:33 +02:00
|
|
|
import audio.funkwhale.ffa.model.DownloadInfo
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.repositories.Repository
|
2021-08-10 14:54:37 +02:00
|
|
|
import com.github.kittinunf.fuel.core.FuelError
|
2020-05-30 21:16:28 +02:00
|
|
|
import com.github.kittinunf.fuel.core.Request
|
2020-06-14 14:59:50 +02:00
|
|
|
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
|
2020-06-25 01:26:15 +02:00
|
|
|
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
|
2021-08-22 09:12:57 +02:00
|
|
|
import java.text.SimpleDateFormat
|
2021-09-09 09:56:15 +02:00
|
|
|
import java.util.Date
|
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
|
|
|
|
) {
|
2020-06-25 01:26:15 +02:00
|
|
|
scope.launch(context) {
|
2019-10-31 16:17:37 +01:00
|
|
|
collect { data ->
|
2020-07-08 22:11:50 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 21:56:33 +02:00
|
|
|
fun Picasso.maybeLoad(url: String?): RequestCreator {
|
2020-06-14 14:59:50 +02:00
|
|
|
return if (url == null) load(R.drawable.cover)
|
|
|
|
else load(url)
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
fun Request.authorize(context: Context, oAuth: OAuth): Request {
|
2021-07-23 14:10:13 +02:00
|
|
|
return runBlocking {
|
|
|
|
this@authorize.apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
2021-08-09 06:50:46 +02:00
|
|
|
oAuth.state().let { state ->
|
2021-08-09 20:04:33 +02:00
|
|
|
state.accessTokenExpirationTime?.let {
|
2021-08-22 09:12:57 +02:00
|
|
|
Log.i("Request.authorize()", "Accesstoken expiration: ${Date(it).format()}")
|
2021-08-09 20:04:33 +02:00
|
|
|
}
|
2021-07-23 14:10:13 +02:00
|
|
|
val old = state.accessToken
|
2021-08-09 06:50:46 +02:00
|
|
|
val auth = ClientSecretPost(oAuth.state().clientSecret)
|
2021-07-23 14:10:13 +02:00
|
|
|
val done = CompletableDeferred<Boolean>()
|
2022-06-11 16:37:38 +02:00
|
|
|
val tokenService = oAuth.service(context)
|
2021-07-23 14:10:13 +02:00
|
|
|
|
2022-06-11 16:37:38 +02:00
|
|
|
state.performActionWithFreshTokens(tokenService, auth) { token, _, e ->
|
|
|
|
if (e != null) {
|
|
|
|
Log.e("Request.authorize()", "performActionWithFreshToken failed: ${e}")
|
|
|
|
Log.e("Request.authorize()", Log.getStackTraceString(e))
|
|
|
|
}
|
2021-08-09 20:04:33 +02:00
|
|
|
if (token == old) {
|
|
|
|
Log.i("Request.authorize()", "Accesstoken not renewed")
|
|
|
|
}
|
2021-07-23 14:10:13 +02:00
|
|
|
if (token != old && token != null) {
|
|
|
|
state.save()
|
|
|
|
}
|
2021-08-09 06:50:46 +02:00
|
|
|
header("Authorization", "Bearer ${oAuth.state().accessToken}")
|
2021-07-23 14:10:13 +02:00
|
|
|
done.complete(true)
|
|
|
|
}
|
|
|
|
done.await()
|
2022-06-11 16:37:38 +02:00
|
|
|
tokenService.dispose()
|
2021-07-23 14:10:13 +02:00
|
|
|
return@runBlocking this
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 14:59:50 +02:00
|
|
|
|
2021-08-10 14:54:37 +02:00
|
|
|
fun FuelError.formatResponseMessage(): String {
|
|
|
|
return "${response.statusCode}: ${response.url}"
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
fun Download.getMetadata(): DownloadInfo? =
|
|
|
|
Gson().fromJson(String(this.request.data), DownloadInfo::class.java)
|
2021-08-22 09:12:57 +02:00
|
|
|
|
|
|
|
val ISO_8601_DATE_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
|
|
|
|
|
|
|
|
fun Date.format(): String {
|
|
|
|
return ISO_8601_DATE_TIME_FORMAT.format(this)
|
2021-09-09 09:56:15 +02:00
|
|
|
}
|