Advertisement
Guest User

Untitled

a guest
Apr 14th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. /*
  2. * Copyright 2018 Ahmed, Umar Bello.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package com.khattabu.med_manager.presentation.login;
  18.  
  19. import android.content.Intent;
  20. import android.os.Bundle;
  21. import android.support.annotation.Nullable;
  22. import android.util.Patterns;
  23. import android.widget.EditText;
  24.  
  25. import com.google.android.gms.auth.api.signin.GoogleSignIn;
  26. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  27. import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  28. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  29. import com.google.android.gms.common.SignInButton;
  30. import com.google.android.gms.common.api.ApiException;
  31. import com.google.android.gms.tasks.Task;
  32. import com.khattabu.med_manager.R;
  33. import com.khattabu.med_manager.data.model.User;
  34. import com.khattabu.med_manager.presentation.base.BaseActivity;
  35. import com.khattabu.med_manager.presentation.list.MedicationList;
  36. import com.khattabu.med_manager.utils.AppLogger;
  37.  
  38. import javax.inject.Inject;
  39.  
  40. import butterknife.BindView;
  41. import butterknife.ButterKnife;
  42. import butterknife.OnClick;
  43.  
  44. /**
  45. * Created by ahmed on 4/8/18.
  46. */
  47.  
  48. public class LoginActivity extends BaseActivity {
  49. @Inject LoginRepository repository;
  50.  
  51. @BindView(R.id.edit_email_address) EditText emailAddress;
  52.  
  53. @BindView(R.id.edit_password) EditText userPassword;
  54.  
  55. @BindView(R.id.button_google_login) SignInButton googleSignIn;
  56.  
  57. private GoogleSignInClient client;
  58. private static final int GOOGLE_SIGN_IN = 41;
  59.  
  60. @Override
  61. protected void onCreate(@Nullable Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63.  
  64. setContentView(R.layout.activity_login);
  65. ButterKnife.bind(this);
  66. getAppComponent().inject(this);
  67.  
  68. GoogleSignInOptions options = new GoogleSignInOptions.Builder
  69. (GoogleSignInOptions.DEFAULT_SIGN_IN)
  70. .requestEmail()
  71. .build();
  72.  
  73. client = GoogleSignIn.getClient(this, options);
  74.  
  75. GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
  76. if (account != null){
  77. startApp(accountToUser(account));
  78. return;
  79. }
  80.  
  81. googleSignIn.setSize(SignInButton.SIZE_STANDARD);
  82. googleSignIn.setOnClickListener(view ->
  83. startNextActivityForResult(client.getSignInIntent(), GOOGLE_SIGN_IN));
  84. }
  85.  
  86. @Override
  87. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  88. super.onActivityResult(requestCode, resultCode, data);
  89.  
  90. if (requestCode == GOOGLE_SIGN_IN){
  91. Task<GoogleSignInAccount> task = GoogleSignIn
  92. .getSignedInAccountFromIntent(data);
  93. handleGoogleIn(task);
  94. }
  95. }
  96.  
  97. @OnClick(R.id.button_login)
  98. public void logUserIn(){
  99. String email = emailAddress.getText().toString();
  100. String password = userPassword.getText().toString();
  101. if (email.trim().isEmpty() ||
  102. !Patterns.EMAIL_ADDRESS.matcher(email).matches() ||
  103. password.isEmpty()){
  104. showMessage("Invalid input");
  105. return;
  106. }
  107.  
  108.  
  109. User user = new User();
  110. user.setEmailAddress(emailAddress.getText().toString());
  111.  
  112. startApp(user);
  113. }
  114.  
  115. private void handleGoogleIn(Task<GoogleSignInAccount> task) {
  116. try {
  117. GoogleSignInAccount account = task.getResult(ApiException.class);
  118. startApp(accountToUser(account));
  119. }catch (ApiException ex){
  120. showMessage("Unable to sign in with Google, kindly use alternative login.");
  121. AppLogger.d("Sign in failed with code " + ex.getStatusCode());
  122. }
  123. }
  124.  
  125. private void startApp(User user){
  126. repository.saveUser(user);
  127.  
  128. Intent intent = new Intent(this, MedicationList.class);
  129. startNextActivity(intent);
  130. finish();
  131. }
  132.  
  133. private User accountToUser(GoogleSignInAccount account) {
  134. User user = new User();
  135. user.setEmailAddress(account.getEmail());
  136. user.setFirstName(account.getGivenName());
  137. user.setLastName(account.getFamilyName());
  138. return user;
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement