Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve welcome flow #4152

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,8 +22,7 @@ import kotlinx.coroutines.launch
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 ->
Expand All @@ -41,27 +39,26 @@ class WelcomeActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get()

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)
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 {
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/com/github/libretube/ui/models/WelcomeModel.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
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
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<Int>()

var instances = MutableLiveData<List<Instances>>()

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)
}
Expand Down