-
Notifications
You must be signed in to change notification settings - Fork 553
feat: Change authentication process and remove navigation (#1749) #1763
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
app/src/main/java/org/fossasia/openevent/general/auth/AuthFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.fossasia.openevent.general.auth | ||
|
||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.lifecycle.Observer | ||
import androidx.navigation.Navigation | ||
import androidx.navigation.fragment.navArgs | ||
import kotlinx.android.synthetic.main.fragment_auth.view.getStartedButton | ||
import kotlinx.android.synthetic.main.fragment_auth.view.email | ||
import kotlinx.android.synthetic.main.fragment_auth.view.rootLayout | ||
import org.fossasia.openevent.general.BuildConfig | ||
import org.fossasia.openevent.general.PLAY_STORE_BUILD_FLAVOR | ||
import org.fossasia.openevent.general.R | ||
import org.fossasia.openevent.general.utils.Utils | ||
import org.fossasia.openevent.general.utils.Utils.hideSoftKeyboard | ||
import org.fossasia.openevent.general.utils.Utils.show | ||
import org.fossasia.openevent.general.utils.Utils.progressDialog | ||
import org.fossasia.openevent.general.utils.extensions.nonNull | ||
import org.jetbrains.anko.design.longSnackbar | ||
import org.jetbrains.anko.design.snackbar | ||
import org.koin.androidx.viewmodel.ext.android.sharedViewModel | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
|
||
class AuthFragment : Fragment() { | ||
private lateinit var rootView: View | ||
private val authViewModel by viewModel<AuthViewModel>() | ||
private val safeArgs: AuthFragmentArgs by navArgs() | ||
private val smartAuthViewModel by sharedViewModel<SmartAuthViewModel>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
if (BuildConfig.FLAVOR == PLAY_STORE_BUILD_FLAVOR) { | ||
smartAuthViewModel.requestCredentials(SmartAuthUtil.getCredentialsClient(requireActivity())) | ||
smartAuthViewModel.isCredentialStored | ||
.nonNull() | ||
.observe(this, Observer { | ||
if (it) redirectToLogin() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikit19 Here no email address is passing, so set its default value to an empty string. |
||
}) | ||
} | ||
} | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
rootView = inflater.inflate(R.layout.fragment_auth, container, false) | ||
|
||
Utils.setToolbar(activity, "", true) | ||
setHasOptionsMenu(true) | ||
|
||
val progressDialog = progressDialog(context) | ||
|
||
val snackbarMessage = safeArgs.snackbarMessage | ||
if (!snackbarMessage.isNullOrEmpty()) rootView.snackbar(snackbarMessage) | ||
|
||
rootView.getStartedButton.setOnClickListener { | ||
hideSoftKeyboard(context, rootView) | ||
authViewModel.checkUser(rootView.email.text.toString()) | ||
} | ||
|
||
authViewModel.isUserExists | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
if (it) | ||
redirectToLogin(rootView.email.text.toString()) | ||
else | ||
redirectToSignUp() | ||
authViewModel.mutableStatus.postValue(null) | ||
}) | ||
|
||
authViewModel.progress | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
progressDialog.show(it) | ||
}) | ||
|
||
smartAuthViewModel.progress | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
progressDialog.show(it) | ||
}) | ||
|
||
authViewModel.error | ||
.nonNull() | ||
.observe(viewLifecycleOwner, Observer { | ||
rootView.rootLayout.longSnackbar(it) | ||
}) | ||
|
||
return rootView | ||
} | ||
|
||
private fun redirectToLogin(email: String = "") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this default value? |
||
Navigation.findNavController(rootView) | ||
.navigate(AuthFragmentDirections | ||
.actionAuthToLogIn(email, safeArgs.redirectedFrom) | ||
) | ||
} | ||
|
||
private fun redirectToSignUp() { | ||
Navigation.findNavController(rootView) | ||
.navigate(AuthFragmentDirections | ||
.actionAuthToSignUp(rootView.email.text.toString(), safeArgs.redirectedFrom) | ||
) | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
return when (item.itemId) { | ||
android.R.id.home -> { | ||
activity?.onBackPressed() | ||
true | ||
} | ||
else -> super.onOptionsItemSelected(item) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
app/src/main/java/org/fossasia/openevent/general/auth/AuthViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.fossasia.openevent.general.auth | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import org.fossasia.openevent.general.R | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.rxkotlin.plusAssign | ||
import org.fossasia.openevent.general.data.Network | ||
import org.fossasia.openevent.general.data.Resource | ||
import org.fossasia.openevent.general.utils.extensions.withDefaultSchedulers | ||
import timber.log.Timber | ||
|
||
class AuthViewModel( | ||
private val authService: AuthService, | ||
private val network: Network, | ||
private val resource: Resource | ||
) : ViewModel() { | ||
|
||
private val compositeDisposable = CompositeDisposable() | ||
private val mutableProgress = MutableLiveData<Boolean>() | ||
val progress: LiveData<Boolean> = mutableProgress | ||
val mutableStatus = MutableLiveData<Boolean>() | ||
val isUserExists: LiveData<Boolean> = mutableStatus | ||
private val mutableError = MutableLiveData<String>() | ||
val error: LiveData<String> = mutableError | ||
|
||
fun checkUser(email: String) { | ||
if (!network.isNetworkConnected()) { | ||
mutableError.value = resource.getString(R.string.no_internet_message) | ||
return | ||
} | ||
compositeDisposable += authService.checkEmail(email) | ||
.withDefaultSchedulers() | ||
.doOnSubscribe { | ||
mutableProgress.value = true | ||
}.doFinally { | ||
mutableProgress.value = false | ||
}.subscribe({ | ||
mutableStatus.value = !it.result | ||
Timber.d("Success!") | ||
}, { | ||
mutableError.value = resource.getString(R.string.error) | ||
Timber.d(it, "Failed") | ||
}) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/org/fossasia/openevent/general/auth/CheckEmailResponse.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.fossasia.openevent.general.auth | ||
|
||
class CheckEmailResponse( | ||
val result: Boolean | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small problem: this will always pop back to EventsFragment -> So if the user navigate from other fragments like Order or Ticket to log in and somehow they press back button, they will not return to the correct one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I have not removed it because imo it make scene. User is redirected to auth fragment where authentication requires, if user cancel the authentication then it should redirect to home screen.
If you want I will remove the condition from auth fragment and navigation flow will work in it's natural way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think that popping back to EventsFragment is fine in most case, except when a user is booking tickets in AttendeeFragment, canceling logging in will not return them to the current event/ticket detail that they're searching for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so It will make complex to apply different conditions for different fragments. I will remove it. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any other changes you want to suggest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't. I think the rest is good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got a problem in that case. If the user goes to tickets, redirected to auth fragment. After pressing the back button, the user must redirect to events fragment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iamareebjamal Can we use fragment's
navArgs()
in Main activity?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it be a problem?