2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.utils
|
|
|
|
|
2019-10-21 19:49:48 +02:00
|
|
|
import android.content.Context
|
2019-08-19 16:50:33 +02:00
|
|
|
import android.os.Build
|
2019-10-21 19:49:48 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2019-08-19 16:50:33 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
2019-10-22 21:56:33 +02:00
|
|
|
import com.github.apognu.otter.R
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.github.apognu.otter.fragments.BrowseFragment
|
|
|
|
import com.github.apognu.otter.repositories.Repository
|
2019-10-22 21:56:33 +02:00
|
|
|
import com.squareup.picasso.Picasso
|
|
|
|
import com.squareup.picasso.RequestCreator
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
|
2019-10-21 19:49:48 +02:00
|
|
|
fun Context.getColor(colorRes: Int): Int {
|
|
|
|
return ContextCompat.getColor(this, colorRes)
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
inline fun <D> Channel<Repository.Response<D>>.await(context: CoroutineContext = Main, crossinline callback: (data: List<D>) -> Unit) {
|
|
|
|
GlobalScope.launch(context) {
|
|
|
|
this@await.receive().also {
|
|
|
|
callback(it.data)
|
|
|
|
close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:20:12 +01:00
|
|
|
inline fun <D> Channel<Repository.Response<D>>.untilNetwork(context: CoroutineContext = Main, crossinline callback: (data: List<D>, isCache: Boolean, hasMore: Boolean) -> Unit) {
|
2019-08-19 16:50:33 +02:00
|
|
|
GlobalScope.launch(context) {
|
|
|
|
for (data in this@untilNetwork) {
|
2019-10-31 11:20:12 +01:00
|
|
|
callback(data.data, data.origin == Repository.Origin.Cache, data.hasMore)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2019-10-29 23:41:44 +01:00
|
|
|
if (data.origin == Repository.Origin.Network && !data.hasMore) {
|
2019-08-19 16:50:33 +02:00
|
|
|
close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if (url == null) return load(R.drawable.cover)
|
|
|
|
else return load(url)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|