From 0537247f43c5b18a2a75d6b674dcc06c19204c02 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 1 Jul 2023 05:10:53 +0530 Subject: [PATCH 1/3] Accept application parameter in WelcomeModel. --- .../com/github/libretube/ui/models/WelcomeModel.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/models/WelcomeModel.kt b/app/src/main/java/com/github/libretube/ui/models/WelcomeModel.kt index 9941a17169..97db0a1eb9 100644 --- a/app/src/main/java/com/github/libretube/ui/models/WelcomeModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/WelcomeModel.kt @@ -1,8 +1,8 @@ package com.github.libretube.ui.models -import android.content.Context +import android.app.Application +import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.github.libretube.api.InstanceHelper import com.github.libretube.api.obj.Instances @@ -10,19 +10,19 @@ import com.github.libretube.extensions.toastFromMainDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -class WelcomeModel : ViewModel() { +class WelcomeModel(private val application: Application) : AndroidViewModel(application) { val selectedInstanceIndex = MutableLiveData() var instances = MutableLiveData>() - fun fetchInstances(context: Context) { + fun fetchInstances() { if (!instances.value.isNullOrEmpty()) return viewModelScope.launch(Dispatchers.IO) { val instances = try { - InstanceHelper.getInstances(context) + InstanceHelper.getInstances(application) } catch (e: Exception) { - context.applicationContext.toastFromMainDispatcher(e.message.orEmpty()) - InstanceHelper.getInstancesFallback(context) + application.toastFromMainDispatcher(e.message.orEmpty()) + InstanceHelper.getInstancesFallback(application) } this@WelcomeModel.instances.postValue(instances) } From 81735de2fa55c97e447119801805216ba6e25009 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 1 Jul 2023 05:11:04 +0530 Subject: [PATCH 2/3] Use viewModels extension. --- .../ui/activities/WelcomeActivity.kt | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt index ef2cc37f2c..ea69196271 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt @@ -4,9 +4,8 @@ import android.content.Intent import android.os.Bundle import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts +import androidx.activity.viewModels import androidx.core.view.isGone -import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.get import androidx.recyclerview.widget.LinearLayoutManager import com.github.libretube.R import com.github.libretube.constants.PreferenceKeys @@ -24,7 +23,7 @@ import kotlinx.coroutines.withContext class WelcomeActivity : BaseActivity() { private lateinit var binding: ActivityWelcomeBinding - private var viewModel: WelcomeModel? = null + private val viewModel: WelcomeModel by viewModels() private val restoreFilePicker = registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> @@ -41,27 +40,26 @@ class WelcomeActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - viewModel = ViewModelProvider(this).get() binding = ActivityWelcomeBinding.inflate(layoutInflater) setContentView(binding.root) // ALl the binding values are optional due to two different possible layouts (normal, landscape) - viewModel!!.instances.observe(this) { instances -> + viewModel.instances.observe(this) { instances -> binding.instancesRecycler?.layoutManager = LinearLayoutManager(this@WelcomeActivity) - binding.instancesRecycler?.adapter = InstancesAdapter(instances, viewModel!!) { index -> - viewModel!!.selectedInstanceIndex.value = index + binding.instancesRecycler?.adapter = InstancesAdapter(instances, viewModel) { index -> + viewModel.selectedInstanceIndex.value = index binding.okay?.alpha = 1f } binding.progress?.isGone = true } - viewModel!!.fetchInstances(this) + viewModel.fetchInstances() - binding.okay?.alpha = if (viewModel!!.selectedInstanceIndex.value != null) 1f else 0.5f + binding.okay?.alpha = if (viewModel.selectedInstanceIndex.value != null) 1f else 0.5f binding.okay?.setOnClickListener { - if (viewModel!!.selectedInstanceIndex.value != null) { + if (viewModel.selectedInstanceIndex.value != null) { val selectedInstance = - viewModel!!.instances.value!![viewModel!!.selectedInstanceIndex.value!!] + viewModel.instances.value!![viewModel.selectedInstanceIndex.value!!] PreferenceHelper.putString(PreferenceKeys.FETCH_INSTANCE, selectedInstance.apiUrl) startMainActivity() } else { From 8a9de45f83ded5b7318aa34e17d462dcd72abd85 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 3 Jul 2023 05:00:49 +0530 Subject: [PATCH 3/3] Remove view binding class variable. --- .../java/com/github/libretube/ui/activities/WelcomeActivity.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt index ea69196271..4ce8ae1785 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/WelcomeActivity.kt @@ -22,7 +22,6 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class WelcomeActivity : BaseActivity() { - private lateinit var binding: ActivityWelcomeBinding private val viewModel: WelcomeModel by viewModels() private val restoreFilePicker = @@ -41,7 +40,7 @@ class WelcomeActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = ActivityWelcomeBinding.inflate(layoutInflater) + val binding = ActivityWelcomeBinding.inflate(layoutInflater) setContentView(binding.root) // ALl the binding values are optional due to two different possible layouts (normal, landscape)