Skip to content

Commit

Permalink
Add improvements to login screen
Browse files Browse the repository at this point in the history
  • Loading branch information
paulohenriquesg committed Nov 19, 2024
1 parent 016fd3f commit 85482cc
Showing 1 changed file with 88 additions and 12 deletions.
100 changes: 88 additions & 12 deletions app/src/main/java/com/paulohenriquesg/fahrenheit/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Language
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.tv.material3.ExperimentalTvMaterial3Api
Expand All @@ -34,6 +48,7 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class LoginActivity : ComponentActivity() {
@OptIn(ExperimentalTvMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -63,49 +78,101 @@ class LoginActivity : ComponentActivity() {
var host by remember { mutableStateOf(sharedPreferences.getString("host", "") ?: "") }
var username by remember { mutableStateOf(sharedPreferences.getString("username", "") ?: "") }
var password by remember { mutableStateOf("") }
var isLoading = remember { mutableStateOf(false) }
val focusManager = LocalFocusManager.current

Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = getString(R.string.app_name),
style = MaterialTheme.typography.headlineMedium,
modifier = Modifier
.padding(bottom = 16.dp)
)
OutlinedTextField(
value = host,
onValueChange = { host = it },
label = { Text("Host") },
modifier = Modifier.fillMaxWidth()
leadingIcon = { Icon(Icons.Filled.Language, contentDescription = "Host Icon") },
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = { focusManager.moveFocus(FocusDirection.Down) }
)
)
Spacer(modifier = Modifier.height(8.dp))
OutlinedTextField(
value = username,
onValueChange = { username = it },
label = { Text("Username") },
modifier = Modifier.fillMaxWidth()
leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "Username Icon") },
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = { focusManager.moveFocus(FocusDirection.Down) }
)
)
Spacer(modifier = Modifier.height(8.dp))
OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
leadingIcon = { Icon(Icons.Filled.Lock, contentDescription = "Password Icon") },
visualTransformation = PasswordVisualTransformation(),
modifier = Modifier.fillMaxWidth()
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = { focusManager.moveFocus(FocusDirection.Down) }
)
)
Spacer(modifier = Modifier.height(16.dp))
Button(
onClick = { handleLogin(host, username, password) },
modifier = Modifier.fillMaxWidth()
) {
Text("Login")
if (isLoading.value) {
CircularProgressIndicator()
} else {
Button(
onClick = { handleLogin(host, username, password, isLoading) },
modifier = Modifier.fillMaxWidth()
) {
Text("Login")
}
}
}
}

private fun handleLogin(host: String, username: String, password: String) {
private fun handleLogin(
host: String,
username: String,
password: String,
isLoading: MutableState<Boolean>
) {
if (host.isBlank() || username.isBlank() || password.isBlank()) {
Toast.makeText(this, "All fields are required", Toast.LENGTH_SHORT).show()
return
}

if (!host.startsWith("http://") && !host.startsWith("https://")) {
Toast.makeText(this, "Host must start with http:// or https://", Toast.LENGTH_SHORT)
.show()
return
}

isLoading.value = true
val loginRequest = LoginRequest(username, password, host)
val apiService = ApiClient.getApiServiceForLogin(host)
apiService.login(loginRequest).enqueue(object : Callback<LoginResponse> {
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
isLoading.value = false
if (response.isSuccessful) {
val loginResponse = response.body()
// Handle successful login
Expand All @@ -122,14 +189,23 @@ class LoginActivity : ComponentActivity() {
}
} else {
// Handle login failure
Toast.makeText(this@LoginActivity, "Login failed", Toast.LENGTH_SHORT).show()
Toast.makeText(
this@LoginActivity,
"Login failed: ${response.message()}",
Toast.LENGTH_SHORT
).show()
}
}

override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
isLoading.value = false
// Log the error message
Log.e("LoginActivity", "Network error", t)
Toast.makeText(this@LoginActivity, "Network error: ${t.message}", Toast.LENGTH_SHORT).show()
Toast.makeText(
this@LoginActivity,
"Network error: ${t.message}",
Toast.LENGTH_SHORT
).show()
}
})
}
Expand Down

0 comments on commit 85482cc

Please sign in to comment.