Merge pull request #1068 from Bnyro/master

Use more ViewModels
This commit is contained in:
Bnyro 2022-08-15 09:51:44 +02:00 committed by GitHub
commit ea377d94f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 81 additions and 58 deletions

View File

@ -4,19 +4,9 @@ package com.github.libretube
* Global variables can be stored here * Global variables can be stored here
*/ */
object Globals { object Globals {
// for the player fragment
var IS_FULL_SCREEN = false
var MINI_PLAYER_VISIBLE = false
// for the data saver mode
var DATA_SAVER_MODE_ENABLED = false
// for downloads // for downloads
var IS_DOWNLOAD_RUNNING = false var IS_DOWNLOAD_RUNNING = false
// for playlists
var SELECTED_PLAYLIST_ID: String? = null
// history of played videos in the current lifecycle // history of played videos in the current lifecycle
val playingQueue = mutableListOf<String>() val playingQueue = mutableListOf<String>()
} }

View File

@ -9,14 +9,17 @@ import android.os.StrictMode
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import coil.ImageLoader
import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.ExceptionHandler import com.github.libretube.util.ExceptionHandler
import com.github.libretube.util.NotificationHelper import com.github.libretube.util.NotificationHelper
@ -30,7 +33,7 @@ class MyApp : Application() {
initializeNotificationChannels() initializeNotificationChannels()
/** /**
* Set the applicationContext as context for the [PreferenceHelper] * Initialize the [PreferenceHelper]
*/ */
PreferenceHelper.setContext(applicationContext) PreferenceHelper.setContext(applicationContext)
@ -40,7 +43,7 @@ class MyApp : Application() {
DatabaseHolder.initializeDatabase(this) DatabaseHolder.initializeDatabase(this)
/** /**
* bypassing fileUriExposedException, see https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed * Bypassing fileUriExposedException, see https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed
*/ */
val builder = VmPolicy.Builder() val builder = VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build()) StrictMode.setVmPolicy(builder.build())
@ -48,7 +51,7 @@ class MyApp : Application() {
/** /**
* Set the api and the auth api url * Set the api and the auth api url
*/ */
setRetrofitApiUrls() initializeRetrofit()
/** /**
* Initialize the notification listener in the background * Initialize the notification listener in the background
@ -76,7 +79,7 @@ class MyApp : Application() {
/** /**
* Set the api urls needed for the [RetrofitInstance] * Set the api urls needed for the [RetrofitInstance]
*/ */
private fun setRetrofitApiUrls() { private fun initializeRetrofit() {
RetrofitInstance.url = RetrofitInstance.url =
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL) PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
// set auth instance // set auth instance
@ -89,6 +92,10 @@ class MyApp : Application() {
} else { } else {
RetrofitInstance.url RetrofitInstance.url
} }
CronetHelper.initCronet(this)
ConnectionHelper.imageLoader = ImageLoader.Builder(this)
.callFactory(CronetHelper.callFactory)
.build()
} }
/** /**

View File

@ -25,15 +25,13 @@ import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import coil.ImageLoader
import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.CronetHelper
import com.github.libretube.databinding.ActivityMainBinding import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.dialogs.ErrorDialog import com.github.libretube.dialogs.ErrorDialog
import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.BaseActivity
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.models.PlayerViewModel
import com.github.libretube.models.SearchViewModel import com.github.libretube.models.SearchViewModel
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
@ -74,17 +72,6 @@ class MainActivity : BaseActivity() {
e.printStackTrace() e.printStackTrace()
} }
CronetHelper.initCronet(this.applicationContext)
ConnectionHelper.imageLoader = ImageLoader.Builder(this.applicationContext)
.callFactory(CronetHelper.callFactory)
.build()
// save whether the data saver mode is enabled
Globals.DATA_SAVER_MODE_ENABLED = PreferenceHelper.getBoolean(
PreferenceKeys.DATA_SAVER_MODE,
false
)
// show noInternet Activity if no internet available on app startup // show noInternet Activity if no internet available on app startup
if (!ConnectionHelper.isNetworkAvailable(this)) { if (!ConnectionHelper.isNetworkAvailable(this)) {
val noInternetIntent = Intent(this, NoInternetActivity::class.java) val noInternetIntent = Intent(this, NoInternetActivity::class.java)
@ -456,7 +443,8 @@ class MainActivity : BaseActivity() {
enableTransition(R.id.yt_transition, true) enableTransition(R.id.yt_transition, true)
} }
findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE
Globals.IS_FULL_SCREEN = false val playerViewModel = ViewModelProvider(this)[PlayerViewModel::class.java]
playerViewModel.isFullscreen.value = false
requestedOrientation = if (autoRotationEnabled) ActivityInfo.SCREEN_ORIENTATION_USER requestedOrientation = if (autoRotationEnabled) ActivityInfo.SCREEN_ORIENTATION_USER
else ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT else ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
} }

View File

@ -7,12 +7,13 @@ import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
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.models.PlaylistViewModel
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ThemeHelper import com.github.libretube.util.ThemeHelper
@ -22,6 +23,7 @@ import java.io.IOException
class AddToPlaylistDialog : DialogFragment() { class AddToPlaylistDialog : DialogFragment() {
private lateinit var binding: DialogAddtoplaylistBinding private lateinit var binding: DialogAddtoplaylistBinding
private val viewModel: PlaylistViewModel by activityViewModels()
private lateinit var videoId: String private lateinit var videoId: String
private lateinit var token: String private lateinit var token: String
@ -67,17 +69,17 @@ class AddToPlaylistDialog : DialogFragment() {
android.R.layout.simple_spinner_dropdown_item android.R.layout.simple_spinner_dropdown_item
) )
binding.playlistsSpinner.adapter = arrayAdapter binding.playlistsSpinner.adapter = arrayAdapter
if (Globals.SELECTED_PLAYLIST_ID != null) { if (viewModel.lastSelectedPlaylistId != null) {
var selectionIndex = 0 var selectionIndex = 0
response.forEachIndexed { index, playlist -> response.forEachIndexed { index, playlist ->
if (playlist.id == Globals.SELECTED_PLAYLIST_ID) selectionIndex = index if (playlist.id == viewModel.lastSelectedPlaylistId) selectionIndex = index
} }
binding.playlistsSpinner.setSelection(selectionIndex) binding.playlistsSpinner.setSelection(selectionIndex)
} }
runOnUiThread { runOnUiThread {
binding.addToPlaylist.setOnClickListener { binding.addToPlaylist.setOnClickListener {
val index = binding.playlistsSpinner.selectedItemPosition val index = binding.playlistsSpinner.selectedItemPosition
Globals.SELECTED_PLAYLIST_ID = response[index].id!! viewModel.lastSelectedPlaylistId = response[index].id!!
addToPlaylist( addToPlaylist(
response[index].id!! response[index].id!!
) )

View File

@ -6,11 +6,11 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.Globals
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.PlaylistsAdapter import com.github.libretube.adapters.PlaylistsAdapter
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
@ -18,6 +18,7 @@ import com.github.libretube.databinding.FragmentLibraryBinding
import com.github.libretube.dialogs.CreatePlaylistDialog import com.github.libretube.dialogs.CreatePlaylistDialog
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.models.PlayerViewModel
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import retrofit2.HttpException import retrofit2.HttpException
@ -27,6 +28,7 @@ class LibraryFragment : BaseFragment() {
lateinit var token: String lateinit var token: String
private lateinit var binding: FragmentLibraryBinding private lateinit var binding: FragmentLibraryBinding
val playerViewModel: PlayerViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -45,6 +47,12 @@ class LibraryFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
// listen for the mini player state changing
playerViewModel.isMiniPlayerVisible.observe(viewLifecycleOwner) {
updateFABMargin()
}
binding.playlistRecView.layoutManager = LinearLayoutManager(requireContext()) binding.playlistRecView.layoutManager = LinearLayoutManager(requireContext())
token = PreferenceHelper.getToken() token = PreferenceHelper.getToken()
@ -76,12 +84,12 @@ class LibraryFragment : BaseFragment() {
} }
} }
override fun onResume() { fun updateFABMargin() {
// optimize CreatePlaylistFab bottom margin if miniPlayer active // optimize CreatePlaylistFab bottom margin if miniPlayer active
val bottomMargin = if (playerViewModel.isMiniPlayerVisible.value == true) 180 else 64
val layoutParams = binding.createPlaylist.layoutParams as ViewGroup.MarginLayoutParams val layoutParams = binding.createPlaylist.layoutParams as ViewGroup.MarginLayoutParams
layoutParams.bottomMargin = if (Globals.MINI_PLAYER_VISIBLE) 180 else 64 layoutParams.bottomMargin = bottomMargin
binding.createPlaylist.layoutParams = layoutParams binding.createPlaylist.layoutParams = layoutParams
super.onResume()
} }
fun fetchPlaylists() { fun fetchPlaylists() {

View File

@ -7,6 +7,7 @@ import android.content.Intent
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Rect import android.graphics.Rect
import android.media.session.PlaybackState
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION.SDK_INT
@ -27,6 +28,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -53,6 +55,7 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.interfaces.DoubleTapInterface import com.github.libretube.interfaces.DoubleTapInterface
import com.github.libretube.interfaces.PlayerOptionsInterface import com.github.libretube.interfaces.PlayerOptionsInterface
import com.github.libretube.models.PlayerViewModel
import com.github.libretube.obj.ChapterSegment import com.github.libretube.obj.ChapterSegment
import com.github.libretube.obj.Segment import com.github.libretube.obj.Segment
import com.github.libretube.obj.Segments import com.github.libretube.obj.Segments
@ -106,6 +109,7 @@ class PlayerFragment : BaseFragment() {
private lateinit var binding: FragmentPlayerBinding private lateinit var binding: FragmentPlayerBinding
private lateinit var playerBinding: ExoStyledPlayerControlViewBinding private lateinit var playerBinding: ExoStyledPlayerControlViewBinding
private lateinit var doubleTapOverlayBinding: DoubleTapOverlayBinding private lateinit var doubleTapOverlayBinding: DoubleTapOverlayBinding
private val viewModel: PlayerViewModel by activityViewModels()
/** /**
* video information * video information
@ -377,11 +381,11 @@ class PlayerFragment : BaseFragment() {
val mainMotionLayout = val mainMotionLayout =
mainActivity.binding.mainMotionLayout mainActivity.binding.mainMotionLayout
if (currentId == eId) { if (currentId == eId) {
Globals.MINI_PLAYER_VISIBLE = true viewModel.isMiniPlayerVisible.value = true
exoPlayerView.useController = false exoPlayerView.useController = false
mainMotionLayout.progress = 1F mainMotionLayout.progress = 1F
} else if (currentId == sId) { } else if (currentId == sId) {
Globals.MINI_PLAYER_VISIBLE = false viewModel.isMiniPlayerVisible.value = false
exoPlayerView.useController = true exoPlayerView.useController = true
mainMotionLayout.progress = 0F mainMotionLayout.progress = 0F
} }
@ -560,7 +564,7 @@ class PlayerFragment : BaseFragment() {
// actions that don't depend on video information // actions that don't depend on video information
private fun initializeOnClickActions(context: Context) { private fun initializeOnClickActions(context: Context) {
binding.closeImageView.setOnClickListener { binding.closeImageView.setOnClickListener {
Globals.MINI_PLAYER_VISIBLE = false viewModel.isMiniPlayerVisible.value = false
binding.playerMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.supportFragmentManager.beginTransaction() mainActivity.supportFragmentManager.beginTransaction()
@ -568,7 +572,7 @@ class PlayerFragment : BaseFragment() {
.commit() .commit()
} }
playerBinding.closeImageButton.setOnClickListener { playerBinding.closeImageButton.setOnClickListener {
Globals.MINI_PLAYER_VISIBLE = false viewModel.isFullscreen.value = false
binding.playerMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.supportFragmentManager.beginTransaction() mainActivity.supportFragmentManager.beginTransaction()
@ -643,7 +647,7 @@ class PlayerFragment : BaseFragment() {
playerBinding.fullscreen.setOnClickListener { playerBinding.fullscreen.setOnClickListener {
// hide player controller // hide player controller
exoPlayerView.hideController() exoPlayerView.hideController()
if (!Globals.IS_FULL_SCREEN) { if (viewModel.isFullscreen.value == false) {
// go to fullscreen mode // go to fullscreen mode
setFullscreen() setFullscreen()
} else { } else {
@ -736,7 +740,7 @@ class PlayerFragment : BaseFragment() {
mainActivity.requestedOrientation = orientation mainActivity.requestedOrientation = orientation
} }
Globals.IS_FULL_SCREEN = true viewModel.isFullscreen.value = true
} }
private fun unsetFullscreen() { private fun unsetFullscreen() {
@ -757,7 +761,7 @@ class PlayerFragment : BaseFragment() {
mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
} }
Globals.IS_FULL_SCREEN = false viewModel.isFullscreen.value = false
} }
private fun toggleDescription() { private fun toggleDescription() {
@ -1300,7 +1304,7 @@ class PlayerFragment : BaseFragment() {
} }
playerBinding.chapterLL.visibility = View.VISIBLE playerBinding.chapterLL.visibility = View.VISIBLE
playerBinding.chapterLL.setOnClickListener { playerBinding.chapterLL.setOnClickListener {
if (Globals.IS_FULL_SCREEN) { if (viewModel.isFullscreen.value!!) {
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.chapters) .setTitle(R.string.chapters)
.setItems(titles.toTypedArray()) { _, index -> .setItems(titles.toTypedArray()) { _, index ->
@ -1526,7 +1530,7 @@ class PlayerFragment : BaseFragment() {
playerBinding.closeImageButton.visibility = visibility playerBinding.closeImageButton.visibility = visibility
playerBinding.exoTitle.visibility = playerBinding.exoTitle.visibility =
if (isLocked && if (isLocked &&
Globals.IS_FULL_SCREEN viewModel.isFullscreen.value == true
) View.VISIBLE else View.INVISIBLE ) View.VISIBLE else View.INVISIBLE
// disable double tap to seek when the player is locked // disable double tap to seek when the player is locked
@ -1626,7 +1630,7 @@ class PlayerFragment : BaseFragment() {
} }
binding.linLayout.visibility = View.GONE binding.linLayout.visibility = View.GONE
Globals.IS_FULL_SCREEN = false viewModel.isFullscreen.value = false
} else { } else {
// enable exoPlayer controls again // enable exoPlayer controls again
exoPlayerView.useController = true exoPlayerView.useController = true
@ -1646,15 +1650,17 @@ class PlayerFragment : BaseFragment() {
} }
private fun shouldStartPiP(): Boolean { private fun shouldStartPiP(): Boolean {
if (!pipEnabled) return false if (!pipEnabled || exoPlayer.playbackState == PlaybackState.STATE_PAUSED) return false
val bounds = Rect() val bounds = Rect()
binding.playerScrollView.getHitRect(bounds) binding.playerScrollView.getHitRect(bounds)
val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java) val backgroundModeRunning = isServiceRunning(requireContext(), BackgroundMode::class.java)
return (binding.playerScrollView.getLocalVisibleRect(bounds) || Globals.IS_FULL_SCREEN) && return (
(exoPlayer.isPlaying || !backgroundModeRunning) binding.playerScrollView.getLocalVisibleRect(bounds) ||
viewModel.isFullscreen.value == true
) && (exoPlayer.isPlaying || !backgroundModeRunning)
} }
private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean { private fun isServiceRunning(context: Context, serviceClass: Class<*>): Boolean {

View File

@ -0,0 +1,13 @@
package com.github.libretube.models
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class PlayerViewModel : ViewModel() {
val isMiniPlayerVisible = MutableLiveData<Boolean>().apply {
value = false
}
val isFullscreen = MutableLiveData<Boolean>().apply {
value = false
}
}

View File

@ -0,0 +1,7 @@
package com.github.libretube.models
import androidx.lifecycle.ViewModel
class PlaylistViewModel : ViewModel() {
var lastSelectedPlaylistId: String? = null
}

View File

@ -4,8 +4,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
class SearchViewModel : ViewModel() { class SearchViewModel : ViewModel() {
var searchQuery = MutableLiveData<String>() val searchQuery = MutableLiveData<String>()
fun setQuery(query: String?) { fun setQuery(query: String?) {
this.searchQuery.value = query this.searchQuery.value = query
} }

View File

@ -5,9 +5,12 @@ import android.net.ConnectivityManager
import android.widget.ImageView import android.widget.ImageView
import coil.ImageLoader import coil.ImageLoader
import coil.load import coil.load
import com.github.libretube.Globals import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
object ConnectionHelper { object ConnectionHelper {
lateinit var imageLoader: ImageLoader
fun isNetworkAvailable(context: Context): Boolean { fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager = val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
@ -38,13 +41,13 @@ object ConnectionHelper {
return connectivityManager.activeNetworkInfo?.isConnected ?: false return connectivityManager.activeNetworkInfo?.isConnected ?: false
} }
lateinit var imageLoader: ImageLoader
// load an image from a url into an imageView // load an image from a url into an imageView
fun loadImage(url: String?, target: ImageView) { fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled // only load the image if the data saver mode is disabled
if (!Globals.DATA_SAVER_MODE_ENABLED) { val dataSaverModeEnabled = PreferenceHelper.getBoolean(
target.load(url, imageLoader) PreferenceKeys.DATA_SAVER_MODE,
} false
)
if (!dataSaverModeEnabled) target.load(url, imageLoader)
} }
} }