Merge pull request #4152 from Isira-Seneviratne/Welcome_improvements

Improve welcome flow
This commit is contained in:
Isira Seneviratne 2023-07-03 11:50:01 +05:30 committed by GitHub
commit 7a34445ca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 20 deletions

View File

@ -4,9 +4,8 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
@ -23,8 +22,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class WelcomeActivity : BaseActivity() { class WelcomeActivity : BaseActivity() {
private lateinit var binding: ActivityWelcomeBinding private val viewModel: WelcomeModel by viewModels()
private var viewModel: WelcomeModel? = null
private val restoreFilePicker = private val restoreFilePicker =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri -> registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
@ -41,27 +39,26 @@ class WelcomeActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get()
binding = ActivityWelcomeBinding.inflate(layoutInflater) val binding = ActivityWelcomeBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
// ALl the binding values are optional due to two different possible layouts (normal, landscape) // 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?.layoutManager = LinearLayoutManager(this@WelcomeActivity)
binding.instancesRecycler?.adapter = InstancesAdapter(instances, viewModel!!) { index -> binding.instancesRecycler?.adapter = InstancesAdapter(instances, viewModel) { index ->
viewModel!!.selectedInstanceIndex.value = index viewModel.selectedInstanceIndex.value = index
binding.okay?.alpha = 1f binding.okay?.alpha = 1f
} }
binding.progress?.isGone = true 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 { binding.okay?.setOnClickListener {
if (viewModel!!.selectedInstanceIndex.value != null) { if (viewModel.selectedInstanceIndex.value != null) {
val selectedInstance = val selectedInstance =
viewModel!!.instances.value!![viewModel!!.selectedInstanceIndex.value!!] viewModel.instances.value!![viewModel.selectedInstanceIndex.value!!]
PreferenceHelper.putString(PreferenceKeys.FETCH_INSTANCE, selectedInstance.apiUrl) PreferenceHelper.putString(PreferenceKeys.FETCH_INSTANCE, selectedInstance.apiUrl)
startMainActivity() startMainActivity()
} else { } else {

View File

@ -1,8 +1,8 @@
package com.github.libretube.ui.models 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.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.github.libretube.api.InstanceHelper import com.github.libretube.api.InstanceHelper
import com.github.libretube.api.obj.Instances import com.github.libretube.api.obj.Instances
@ -10,19 +10,19 @@ import com.github.libretube.extensions.toastFromMainDispatcher
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class WelcomeModel : ViewModel() { class WelcomeModel(private val application: Application) : AndroidViewModel(application) {
val selectedInstanceIndex = MutableLiveData<Int>() val selectedInstanceIndex = MutableLiveData<Int>()
var instances = MutableLiveData<List<Instances>>() var instances = MutableLiveData<List<Instances>>()
fun fetchInstances(context: Context) { fun fetchInstances() {
if (!instances.value.isNullOrEmpty()) return if (!instances.value.isNullOrEmpty()) return
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val instances = try { val instances = try {
InstanceHelper.getInstances(context) InstanceHelper.getInstances(application)
} catch (e: Exception) { } catch (e: Exception) {
context.applicationContext.toastFromMainDispatcher(e.message.orEmpty()) application.toastFromMainDispatcher(e.message.orEmpty())
InstanceHelper.getInstancesFallback(context) InstanceHelper.getInstancesFallback(application)
} }
this@WelcomeModel.instances.postValue(instances) this@WelcomeModel.instances.postValue(instances)
} }