@@ -26,14 +26,14 @@ import com.cornellappdev.coursegrab.networking.deviceToken
2626import com.cornellappdev.coursegrab.networking.initializeSession
2727import com.cornellappdev.coursegrab.networking.setNotification
2828import com.cornellappdev.coursegrab.networking.updateSession
29- import com.google.android.gms.common.api.ApiException
3029import com.google.android.libraries.identity.googleid.GetGoogleIdOption
3130import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption
3231import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
3332import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
3433import com.google.android.material.snackbar.Snackbar
3534import com.google.firebase.messaging.FirebaseMessaging
3635import com.google.gson.reflect.TypeToken
36+ import kotlinx.coroutines.CancellationException
3737import kotlinx.coroutines.Dispatchers
3838import kotlinx.coroutines.launch
3939import kotlinx.coroutines.withContext
@@ -61,26 +61,29 @@ class LoginActivity : AppCompatActivity() {
6161 val intent = Intent (this @LoginActivity, MainActivity ::class .java)
6262 startActivity(intent)
6363 } else {
64- if (preferencesHelper.updateToken != null )
65- try {
66- val updateSession =
67- Endpoint .updateSession(preferencesHelper.updateToken.toString())
64+ val updateToken = preferencesHelper.updateToken
65+ if (! updateToken.isNullOrBlank()) {
66+ val updateSession = Endpoint .updateSession(updateToken)
6867
69- lifecycleScope.launch {
68+ lifecycleScope.launch {
69+ val userSession = try {
7070 val typeToken = object : TypeToken <ApiResponse <UserSession >>() {}.type
71- val userSession = withContext(Dispatchers .IO ) {
71+ withContext(Dispatchers .IO ) {
7272 Request .makeRequest<ApiResponse <UserSession >>(
7373 updateSession.okHttpRequest(),
7474 typeToken
7575 )
76- }!! .data
77-
78- verifySession(userSession)
76+ }?.data
77+ } catch (e: CancellationException ) {
78+ throw e
79+ } catch (e: Exception ) {
80+ Log .d(TAG , " Could not resume previous session" , e)
81+ null
7982 }
8083
81- } catch (e: ApiException ) {
82- e.printStackTrace()
84+ if (userSession != null ) verifySession(userSession)
8385 }
86+ }
8487 }
8588
8689 binding.signInButton.setOnClickListener { signIn() }
@@ -190,13 +193,25 @@ class LoginActivity : AppCompatActivity() {
190193 val initializeSession = Endpoint .initializeSession(googleCredential.idToken, null )
191194
192195 lifecycleScope.launch {
193- val typeToken = object : TypeToken <ApiResponse <UserSession >>() {}.type
194- val userSession = withContext(Dispatchers .IO ) {
195- Request .makeRequest<ApiResponse <UserSession >>(
196- initializeSession.okHttpRequest(),
197- typeToken
198- )
199- }!! .data
196+ val userSession = try {
197+ val typeToken = object : TypeToken <ApiResponse <UserSession >>() {}.type
198+ withContext(Dispatchers .IO ) {
199+ Request .makeRequest<ApiResponse <UserSession >>(
200+ initializeSession.okHttpRequest(),
201+ typeToken
202+ )
203+ }?.data
204+ } catch (e: CancellationException ) {
205+ throw e
206+ } catch (e: Exception ) {
207+ Log .e(TAG , " Failed to initialize session" , e)
208+ null
209+ }
210+
211+ if (userSession == null ) {
212+ showLoginError(" Sign-in failed. Please try again." )
213+ return @launch
214+ }
200215
201216 verifySession(userSession)
202217 }
0 commit comments