Use eventbus to navigate from GoogleFragment
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -19,21 +19,16 @@
|
||||
|
||||
package com.aurora.store.view.ui.account
|
||||
|
||||
import android.app.Activity.RESULT_CANCELED
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.aurora.extensions.browse
|
||||
import com.aurora.extensions.getEmptyActivityAnimation
|
||||
import com.aurora.extensions.load
|
||||
import com.aurora.gplayapi.data.models.AuthData
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.AuthState
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.providers.AccountProvider
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.databinding.FragmentAccountBinding
|
||||
@@ -42,8 +37,6 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners
|
||||
import nl.komponents.kovenant.task
|
||||
import nl.komponents.kovenant.ui.failUi
|
||||
import nl.komponents.kovenant.ui.successUi
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
|
||||
class AccountFragment : Fragment(R.layout.fragment_account) {
|
||||
|
||||
@@ -51,7 +44,7 @@ class AccountFragment : Fragment(R.layout.fragment_account) {
|
||||
private val binding: FragmentAccountBinding
|
||||
get() = _binding!!
|
||||
|
||||
private lateinit var VM: AuthViewModel
|
||||
private val viewModel: AuthViewModel by activityViewModels()
|
||||
|
||||
private lateinit var authData: AuthData
|
||||
private lateinit var accountProvider: AccountProvider
|
||||
@@ -61,22 +54,9 @@ class AccountFragment : Fragment(R.layout.fragment_account) {
|
||||
private val URL_DISCLAIMER =
|
||||
"https://gitlab.com/AuroraOSS/AuroraStore/blob/master/DISCLAIMER.md"
|
||||
|
||||
private val startForResult =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
if (it.resultCode == RESULT_CANCELED) {
|
||||
resetActions()
|
||||
} else {
|
||||
binding.btnGoogle.updateProgress(true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
EventBus.getDefault().register(this)
|
||||
|
||||
_binding = FragmentAccountBinding.bind(view)
|
||||
VM = ViewModelProvider(this)[AuthViewModel::class.java]
|
||||
|
||||
authData = AuthProvider.with(view.context).getAuthData()
|
||||
accountProvider = AccountProvider.with(view.context)
|
||||
@@ -98,7 +78,7 @@ class AccountFragment : Fragment(R.layout.fragment_account) {
|
||||
|
||||
updateContents()
|
||||
|
||||
VM.liveData.observe(viewLifecycleOwner) {
|
||||
viewModel.liveData.observe(viewLifecycleOwner) {
|
||||
when (it) {
|
||||
AuthState.Fetching -> {
|
||||
updateStatus(getString(R.string.requesting_new_session))
|
||||
@@ -138,25 +118,6 @@ class AccountFragment : Fragment(R.layout.fragment_account) {
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
@Subscribe()
|
||||
fun onEventReceived(event: BusEvent) {
|
||||
when (event) {
|
||||
is BusEvent.GoogleAAS -> {
|
||||
if (event.success) {
|
||||
updateStatus(getString(R.string.session_verifying_google))
|
||||
VM.buildGoogleAuthData(event.email, event.aasToken)
|
||||
} else {
|
||||
updateStatus(getString(R.string.session_login_failed_google))
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateContents() {
|
||||
@@ -192,17 +153,20 @@ class AccountFragment : Fragment(R.layout.fragment_account) {
|
||||
binding.btnGoogle.updateProgress(false)
|
||||
|
||||
binding.btnAnonymous.addOnClickListener {
|
||||
if (VM.liveData.value != AuthState.Fetching) {
|
||||
if (viewModel.liveData.value != AuthState.Fetching) {
|
||||
binding.btnAnonymous.updateProgress(true)
|
||||
VM.buildAnonymousAuthData()
|
||||
viewModel.buildAnonymousAuthData()
|
||||
}
|
||||
}
|
||||
|
||||
binding.btnGoogle.addOnClickListener {
|
||||
if (VM.liveData.value != AuthState.Fetching) {
|
||||
if (viewModel.liveData.value != AuthState.Fetching) {
|
||||
binding.btnGoogle.updateProgress(true)
|
||||
val intent = Intent(requireContext(), GoogleFragment::class.java)
|
||||
startForResult.launch(intent, requireContext().getEmptyActivityAnimation())
|
||||
findNavController().navigate(
|
||||
AccountFragmentDirections.actionAccountFragmentToGoogleFragment(
|
||||
R.id.accountFragment
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import android.widget.Toast
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.navigation.fragment.navArgs
|
||||
import com.aurora.Constants
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
@@ -40,9 +41,12 @@ import com.aurora.store.util.Preferences
|
||||
import com.aurora.store.viewmodel.auth.AuthViewModel
|
||||
import nl.komponents.kovenant.task
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import org.greenrobot.eventbus.ThreadMode
|
||||
|
||||
class GoogleFragment : Fragment(R.layout.fragment_google) {
|
||||
|
||||
private val args: GoogleFragmentArgs by navArgs()
|
||||
private val viewModel: AuthViewModel by activityViewModels()
|
||||
|
||||
companion object {
|
||||
@@ -94,6 +98,39 @@ class GoogleFragment : Fragment(R.layout.fragment_google) {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun onEventReceived(event: BusEvent) {
|
||||
when (event) {
|
||||
is BusEvent.GoogleAAS -> {
|
||||
if (event.success) viewModel.buildGoogleAuthData(event.email, event.aasToken)
|
||||
when (args.destination) {
|
||||
R.id.splashFragment -> {
|
||||
findNavController().navigate(
|
||||
GoogleFragmentDirections.actionGoogleFragmentToSplashFragment()
|
||||
)
|
||||
}
|
||||
R.id.accountFragment -> {
|
||||
findNavController().navigate(
|
||||
GoogleFragmentDirections.actionGoogleFragmentToAccountFragment()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildAuthData(email: String, oauthToken: String?) {
|
||||
task {
|
||||
AC2DMTask().getAC2DMResponse(email, oauthToken)
|
||||
@@ -104,7 +141,6 @@ class GoogleFragment : Fragment(R.layout.fragment_google) {
|
||||
Preferences.putString(requireContext(), Constants.ACCOUNT_EMAIL_PLAIN, email)
|
||||
Preferences.putString(requireContext(), Constants.ACCOUNT_AAS_PLAIN, aasToken)
|
||||
EventBus.getDefault().post(BusEvent.GoogleAAS(true, email, aasToken))
|
||||
viewModel.buildGoogleAuthData(email, aasToken)
|
||||
} else {
|
||||
Preferences.putString(requireContext(), Constants.ACCOUNT_EMAIL_PLAIN, "")
|
||||
Preferences.putString(requireContext(), Constants.ACCOUNT_AAS_PLAIN, "")
|
||||
@@ -118,9 +154,6 @@ class GoogleFragment : Fragment(R.layout.fragment_google) {
|
||||
).show()
|
||||
EventBus.getDefault().post(BusEvent.GoogleAAS(false))
|
||||
}
|
||||
findNavController().navigate(
|
||||
GoogleFragmentDirections.actionGoogleFragmentToSplashFragment()
|
||||
)
|
||||
} fail {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
@@ -128,9 +161,6 @@ class GoogleFragment : Fragment(R.layout.fragment_google) {
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
EventBus.getDefault().post(BusEvent.GoogleAAS(false))
|
||||
findNavController().navigate(
|
||||
GoogleFragmentDirections.actionGoogleFragmentToSplashFragment()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,25 +115,6 @@ class SplashFragment : Fragment(R.layout.fragment_splash) {
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
|
||||
@Subscribe()
|
||||
fun onEventReceived(event: BusEvent) {
|
||||
when (event) {
|
||||
is BusEvent.GoogleAAS -> {
|
||||
if (event.success) {
|
||||
updateStatus(getString(R.string.session_verifying_google))
|
||||
viewModel.buildGoogleAuthData(event.email, event.aasToken)
|
||||
} else {
|
||||
updateStatus(getString(R.string.session_login_failed_google))
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateStatus(string: String?) {
|
||||
@@ -170,7 +151,11 @@ class SplashFragment : Fragment(R.layout.fragment_splash) {
|
||||
binding.btnGoogle.addOnClickListener {
|
||||
if (viewModel.liveData.value != AuthState.Fetching) {
|
||||
binding.btnGoogle.updateProgress(true)
|
||||
findNavController().navigate(R.id.googleFragment)
|
||||
findNavController().navigate(
|
||||
SplashFragmentDirections.actionSplashFragmentToGoogleFragment(
|
||||
R.id.splashFragment
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,11 @@
|
||||
android:id="@+id/accountFragment"
|
||||
android:name="com.aurora.store.view.ui.account.AccountFragment"
|
||||
android:label="@string/title_account_manager"
|
||||
tools:layout="@layout/fragment_account" />
|
||||
tools:layout="@layout/fragment_account" >
|
||||
<action
|
||||
android:id="@+id/action_accountFragment_to_googleFragment"
|
||||
app:destination="@id/googleFragment" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/settingsFragment"
|
||||
android:name="com.aurora.store.view.ui.preferences.SettingsFragment"
|
||||
@@ -246,6 +250,9 @@
|
||||
app:launchSingleTop="true"
|
||||
app:popUpTo="@id/mobile_navigation"
|
||||
app:popUpToInclusive="true" />
|
||||
<action
|
||||
android:id="@+id/action_splashFragment_to_googleFragment"
|
||||
app:destination="@id/googleFragment" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/googleFragment"
|
||||
@@ -256,6 +263,13 @@
|
||||
app:destination="@id/splashFragment"
|
||||
app:popUpTo="@id/splashFragment"
|
||||
app:popUpToInclusive="true" />
|
||||
<action
|
||||
android:id="@+id/action_googleFragment_to_accountFragment"
|
||||
app:destination="@id/accountFragment"
|
||||
app:popUpTo="@id/accountFragment"
|
||||
app:popUpToInclusive="true" />
|
||||
<argument android:name="destination"
|
||||
app:argType="integer" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/onboardingFragment"
|
||||
|
||||
Reference in New Issue
Block a user