Skip to content

Commit 0e75a4b

Browse files
committed
API call if username is not empty by user input
1 parent 10f5696 commit 0e75a4b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

app/src/main/kotlin/com/byteutility/dev/leetcode/plus/ui/screens/login/UserLoginViewModel.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.byteutility.dev.leetcode.plus.ui.screens.login
22

33
import android.content.Context
4+
import android.widget.Toast
45
import androidx.lifecycle.ViewModel
56
import androidx.lifecycle.viewModelScope
67
import com.byteutility.dev.leetcode.plus.data.datastore.UserDatastore
@@ -31,6 +32,10 @@ class UserLoginViewModel @Inject constructor(
3132
val loginState = _loginState.asStateFlow()
3233

3334
fun saveUserName(userName: String, context: Context) {
35+
if (userName.isEmpty()) {
36+
Toast.makeText(context, "Username cannot be empty.", Toast.LENGTH_SHORT).show()
37+
return
38+
}
3439
viewModelScope.launch {
3540
_loginState.value = LoginState.Loading
3641
try {
@@ -41,8 +46,9 @@ class UserLoginViewModel @Inject constructor(
4146
)
4247
_loginState.value = LoginState.Success(userName)
4348
UserDetailsSyncWorker.enqueuePeriodicWork(context)
44-
} catch (_: Exception) {
49+
} catch (e: Exception) {
4550
_loginState.value = LoginState.Error("Failed to retrieve user profile.")
51+
Toast.makeText(context, "Error: ${e.message}", Toast.LENGTH_SHORT).show()
4652
}
4753
}
4854
}

0 commit comments

Comments
 (0)