Use cronet with coil.

This commit is contained in:
Kavin 2022-07-21 23:28:38 +05:30
parent 56b7a8e5cc
commit f0ab7888fb
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
4 changed files with 14 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import coil.ImageLoader
import com.github.libretube.Globals
import com.github.libretube.PIPED_API_URL
import com.github.libretube.R
@ -69,6 +70,9 @@ class MainActivity : AppCompatActivity() {
startService(Intent(this, ClosingService::class.java))
CronetHelper.initCronet(this.applicationContext)
ConnectionHelper.imageLoader = ImageLoader.Builder(this.applicationContext)
.callFactory(CronetHelper.callFactory)
.build()
RetrofitInstance.url =
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)!!

View File

@ -3,6 +3,7 @@ package com.github.libretube.util
import android.content.Context
import android.net.ConnectivityManager
import android.widget.ImageView
import coil.ImageLoader
import coil.load
import com.github.libretube.Globals
@ -37,11 +38,13 @@ object ConnectionHelper {
return connectivityManager.activeNetworkInfo?.isConnected ?: false
}
lateinit var imageLoader: ImageLoader
// load an image from a url into an imageView
fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled
if (!Globals.DATA_SAVER_MODE_ENABLED) {
target.load(url)
target.load(url, imageLoader)
}
}
}

View File

@ -1,16 +1,20 @@
package com.github.libretube.util
import android.content.Context
import com.google.net.cronet.okhttptransport.CronetCallFactory
import org.chromium.net.CronetEngine
class CronetHelper {
companion object {
private lateinit var engine: CronetEngine
lateinit var callFactory: CronetCallFactory
fun initCronet(context: Context) {
this.engine = CronetEngine.Builder(context)
.enableBrotli(true)
.build()
callFactory = CronetCallFactory.newBuilder(this.engine)
.build()
}
fun getCronetEngine(): CronetEngine {

View File

@ -1,6 +1,5 @@
package com.github.libretube.util
import com.google.net.cronet.okhttptransport.CronetCallFactory
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
@ -8,13 +7,10 @@ object RetrofitInstance {
lateinit var url: String
lateinit var authUrl: String
val lazyMgr = resettableManager()
private val callFactory: CronetCallFactory =
CronetCallFactory.newBuilder(CronetHelper.getCronetEngine())
.build()
val api: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(url)
.callFactory(callFactory)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(JacksonConverterFactory.create())
.build()
.create(PipedApi::class.java)
@ -22,7 +18,7 @@ object RetrofitInstance {
val authApi: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(authUrl)
.callFactory(callFactory)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(JacksonConverterFactory.create())
.build()
.create(PipedApi::class.java)