Skip to content

Commit

Permalink
Refactor failed signin flow (#65)
Browse files Browse the repository at this point in the history
* Added generic message with optional dialog for more information.
  • Loading branch information
newmanw authored Apr 11, 2024
1 parent 8726645 commit 566290e
Show file tree
Hide file tree
Showing 21 changed files with 953 additions and 638 deletions.
13 changes: 9 additions & 4 deletions mage/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="LockedOrientationActivity">

<permission
android:name="${applicationId}.permission.MAPS_RECEIVE"
Expand Down Expand Up @@ -73,8 +74,10 @@
<activity
android:name=".login.LoginActivity"
android:exported="true"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/AppTheme3.NoActionBar">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down Expand Up @@ -133,7 +136,7 @@
</activity>
<activity
android:name=".login.SignupActivity"
android:theme="@style/AppTheme.NoActionBar"
android:theme="@style/AppTheme3.NoActionBar"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name="mil.nga.giat.mage.compat.server5.login.SignupActivityServer5"
Expand All @@ -144,7 +147,9 @@
</activity>
<activity
android:name=".login.ServerUrlActivity"
android:theme="@style/AppTheme.NoActionBar" />
android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
<activity
android:name=".event.EventsActivity"
android:theme="@style/AppTheme.NoActionBar" />
Expand Down
2 changes: 1 addition & 1 deletion mage/src/main/java/mil/nga/giat/mage/MageApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import mil.nga.giat.mage.location.LocationFetchService
import mil.nga.giat.mage.location.LocationReportingService
import mil.nga.giat.mage.login.AccountStateActivity
import mil.nga.giat.mage.login.LoginActivity
import mil.nga.giat.mage.login.ServerUrlActivity
import mil.nga.giat.mage.login.SignupActivity
import mil.nga.giat.mage.login.idp.IdpLoginActivity
import mil.nga.giat.mage.network.Server
Expand All @@ -45,6 +44,7 @@ import mil.nga.giat.mage.observation.sync.ObservationSyncWorker
import mil.nga.giat.mage.data.datasource.observation.ObservationLocalDataSource
import mil.nga.giat.mage.data.datasource.user.UserLocalDataSource
import mil.nga.giat.mage.di.TokenStatus
import mil.nga.giat.mage.login.ServerUrlActivity
import javax.inject.Inject

@HiltAndroidApp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import org.json.JSONObject
import javax.inject.Inject

sealed class ApiResponse {
object Valid: ApiResponse()
object Invalid: ApiResponse()
data class Error(val message: String): ApiResponse()
data object Success: ApiResponse()
data class Incompatible(val version: String): ApiResponse()
data class Error(val statusCode: Int? = null, val message: String? = null): ApiResponse()
}

class ApiRepository @Inject constructor(
Expand All @@ -30,28 +30,23 @@ class ApiRepository @Inject constructor(
removeValues()
populateValues(SERVER_API_PREFERENCE_PREFIX, apiJson)
parseAuthenticationStrategies(apiJson)
if (isApiValid()) ApiResponse.Valid else ApiResponse.Invalid

val majorVersion = preferences.getInt(application.getString(R.string.serverVersionMajorKey), 0)
val minorVersion = preferences.getInt(application.getString(R.string.serverVersionMinorKey), 0)
val patchVersion = preferences.getInt(application.getString(R.string.serverVersionPatchKey), 0)
val isApiCompatible = Compatibility.isCompatibleWith(majorVersion, minorVersion)
if (isApiCompatible) {
ApiResponse.Success
} else {
ApiResponse.Incompatible("$majorVersion.$minorVersion.$patchVersion")
}
} else {
val message = response.errorBody()?.string() ?: "Application is not compatible with server"
ApiResponse.Error(message)
ApiResponse.Error(response.code(), response.errorBody()?.string())
}
} catch (e: Exception) {
Log.e(LOG_NAME, "Error fetching API", e)
ApiResponse.Error(e.cause?.localizedMessage ?: "Cannot connect to server.")
}
}

private fun isApiValid(): Boolean {
// check versions
var majorVersion: Int? = null
if (preferences.contains(application.getString(R.string.serverVersionMajorKey))) {
majorVersion = preferences.getInt(application.getString(R.string.serverVersionMajorKey), 0)
}
var minorVersion: Int? = null
if (preferences.contains(application.getString(R.string.serverVersionMinorKey))) {
minorVersion = preferences.getInt(application.getString(R.string.serverVersionMinorKey), 0)
ApiResponse.Error(message = e.message)
}
return Compatibility.isCompatibleWith(majorVersion!!, minorVersion!!)
}

private fun populateValues(sharedPreferenceName: String, json: JSONObject) {
Expand Down
15 changes: 5 additions & 10 deletions mage/src/main/java/mil/nga/giat/mage/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mil.nga.giat.mage.login
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.util.Log
Expand Down Expand Up @@ -147,8 +146,6 @@ class LoginActivity : AppCompatActivity() {
// no title bar
setContentView(R.layout.activity_login)
hideKeyboardOnClick(findViewById(R.id.login))
val appName = findViewById<TextView>(R.id.mage)
appName.setTypeface(Typeface.createFromAsset(assets, "fonts/GondolaMage-Regular.otf"))
(findViewById<View>(R.id.login_version) as TextView).text = "App Version: " + preferences.getString(getString(R.string.buildVersionKey), "NA")
serverUrlText = findViewById(R.id.server_url)
val serverUrl = preferences.getString(getString(R.string.serverURLKey), getString(R.string.serverURLDefaultValue))!!
Expand Down Expand Up @@ -187,12 +184,10 @@ class LoginActivity : AppCompatActivity() {
}

private fun observeAuthenticationState(state: AuthenticationState) {
if (state === AuthenticationState.LOADING) {
findViewById<View>(R.id.login_status).visibility = View.VISIBLE
findViewById<View>(R.id.login_form).visibility = View.GONE
} else if (state === AuthenticationState.ERROR) {
findViewById<View>(R.id.login_status).visibility = View.GONE
findViewById<View>(R.id.login_form).visibility = View.VISIBLE
findViewById<View>(R.id.progress).visibility = if (state === AuthenticationState.LOADING) {
View.VISIBLE
} else {
View.VISIBLE
}
}

Expand Down Expand Up @@ -356,7 +351,7 @@ class LoginActivity : AppCompatActivity() {
}
}

fun changeServerURL() {
private fun changeServerURL() {
val intent = Intent(this, ServerUrlActivity::class.java)
startActivity(intent)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class LoginViewModel @Inject constructor(
viewModelScope.launch {
val response = apiRepository.getApi(url)
if (authenticationState.value != AuthenticationState.LOADING) {
_apiStatus.value = response is ApiResponse.Valid
_apiStatus.value = response is ApiResponse.Success
}
}
}
Expand Down
168 changes: 0 additions & 168 deletions mage/src/main/java/mil/nga/giat/mage/login/ServerUrlActivity.java

This file was deleted.

33 changes: 33 additions & 0 deletions mage/src/main/java/mil/nga/giat/mage/login/ServerUrlActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mil.nga.giat.mage.login

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import mil.nga.giat.mage.ui.theme.MageTheme3
import mil.nga.giat.mage.ui.url.ServerUrlScreen

@AndroidEntryPoint
class ServerUrlActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
MageTheme3 {
ServerUrlScreen(
onDone = {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
},
onCancel = {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
}
)
}
}
}
}
Loading

0 comments on commit 566290e

Please sign in to comment.