fix navigation

This commit is contained in:
Denys Vitali 2020-09-16 01:52:36 +02:00
parent d2160405a8
commit d99a45accf
No known key found for this signature in database
GPG Key ID: 8883F38950E654A4
3 changed files with 45 additions and 4 deletions

View File

@ -0,0 +1,24 @@
package com.vanced.manager.ui.events
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}

View File

@ -8,7 +8,10 @@ import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.vanced.manager.R
@ -16,6 +19,7 @@ import com.vanced.manager.adapter.SectionPageAdapter
import com.vanced.manager.adapter.SectionPageRootAdapter
import com.vanced.manager.databinding.FragmentHomeBinding
import com.vanced.manager.ui.dialogs.DialogContainer.installAlertBuilder
import com.vanced.manager.ui.events.Event
import com.vanced.manager.ui.viewmodels.HomeViewModel
import com.vanced.manager.utils.AppUtils.installing
@ -42,6 +46,13 @@ open class HomeFragment : Fragment(), View.OnClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.navigateDestination.observe(this, Observer<Event<Int>> {
val content = it.getContentIfNotHandled()
if(content != null){
view.findNavController().navigate(content)
}
})
with(binding) {
includeChangelogsLayout.changelogButton.setOnClickListener(this@HomeFragment)

View File

@ -10,7 +10,8 @@ import androidx.core.content.ContextCompat.startActivity
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.lifecycle.AndroidViewModel
import androidx.navigation.fragment.NavHostFragment.findNavController
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.crowdin.platform.Crowdin
import com.vanced.manager.R
import com.vanced.manager.core.downloader.MicrogDownloader.downloadMicrog
@ -18,7 +19,7 @@ import com.vanced.manager.core.downloader.MusicDownloader.downloadMusic
import com.vanced.manager.core.downloader.VancedDownloader.downloadVanced
import com.vanced.manager.model.DataModel
import com.vanced.manager.model.ProgressModel
import com.vanced.manager.ui.fragments.MainFragment
import com.vanced.manager.ui.events.Event
import com.vanced.manager.utils.AppUtils.installing
import com.vanced.manager.utils.InternetTools
import com.vanced.manager.utils.PackageHelper.uninstallApk
@ -39,6 +40,11 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
val manager = ObservableField<DataModel>()
val fetching = ObservableBoolean()
var _navigateDestination = MutableLiveData<Event<Int>>()
val navigateDestination : LiveData<Event<Int>>
get() = _navigateDestination
fun fetchData() {
CoroutineScope(Dispatchers.IO).launch {
fetching.set(true)
@ -88,7 +94,7 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
InternetTools.openUrl(url, color, getApplication())
}
fun installVanced() {
if (!installing) {
if (!fetching.get()) {
@ -98,7 +104,7 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
if (app.getSharedPreferences("installPrefs", Context.MODE_PRIVATE).getBoolean("valuesModified", false)) {
downloadVanced(app)
} else {
findNavController(MainFragment()).navigate(R.id.toInstallThemeFragment)
_navigateDestination.value = Event(R.id.toInstallThemeFragment)
}
}
}