-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rename package
model.auth
to model.authentication
for consi…
…stency Updated references project-wide as well.
- Loading branch information
1 parent
2eea42f
commit 98a1af4
Showing
12 changed files
with
610 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
app/src/main/java/com/android/periodpals/model/auth/AuthModel.kt
This file was deleted.
Oops, something went wrong.
89 changes: 0 additions & 89 deletions
89
app/src/main/java/com/android/periodpals/model/auth/AuthViewModel.kt
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/android/periodpals/model/authentication/AuthModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.android.periodpals.model.authentication | ||
|
||
interface AuthModel { | ||
|
||
suspend fun login( | ||
userEmail: String, | ||
userPassword: String, | ||
onSuccess: () -> Unit, | ||
onFailure: (Exception) -> Unit, | ||
) | ||
|
||
suspend fun register( | ||
userEmail: String, | ||
userPassword: String, | ||
onSuccess: () -> Unit, | ||
onFailure: (Exception) -> Unit, | ||
) | ||
|
||
suspend fun logout(onSuccess: () -> Unit, onFailure: (Exception) -> Unit) | ||
|
||
suspend fun isUserLoggedIn(token: String, onSuccess: () -> Unit, onFailure: (Exception) -> Unit) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/com/android/periodpals/model/authentication/AuthViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.android.periodpals.model.authentication | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.android.periodpals.model.user.UserAuthState | ||
import kotlinx.coroutines.launch | ||
|
||
private const val TAG = "AuthViewModel" | ||
|
||
class AuthViewModel(private val authModel: AuthModel) : ViewModel() { | ||
|
||
private val _userAuthState = mutableStateOf<UserAuthState>(UserAuthState.Loading) | ||
val userAuthState: State<UserAuthState> = _userAuthState | ||
|
||
fun signUpWithEmail(context: Context, userEmail: String, userPassword: String) { | ||
_userAuthState.value = UserAuthState.Loading | ||
viewModelScope.launch { | ||
authModel.register( | ||
userEmail = userEmail, | ||
userPassword = userPassword, | ||
onSuccess = { | ||
Log.d(TAG, "signUpWithEmail: registered user successfully") | ||
_userAuthState.value = UserAuthState.Success("Registered user successfully") | ||
}, | ||
onFailure = { e: Exception -> | ||
Log.d(TAG, "signUpWithEmail: failed to register user: $e") | ||
_userAuthState.value = UserAuthState.Error("Error: $e") | ||
}, | ||
) | ||
} | ||
} | ||
|
||
fun logInWithEmail(context: Context, userEmail: String, userPassword: String) { | ||
_userAuthState.value = UserAuthState.Loading | ||
viewModelScope.launch { | ||
authModel.login( | ||
userEmail = userEmail, | ||
userPassword = userPassword, | ||
onSuccess = { | ||
Log.d(TAG, "logInWithEmail: logged in successfully") | ||
_userAuthState.value = UserAuthState.Success("Logged in successfully") | ||
}, | ||
onFailure = { e: Exception -> | ||
Log.d(TAG, "logInWithEmail: failed to log in: $e") | ||
_userAuthState.value = UserAuthState.Error("Error: $e") | ||
}, | ||
) | ||
} | ||
} | ||
|
||
fun logOut(context: Context) { | ||
// val sharedPreferenceHelper = SharedPreferenceHelper(context) | ||
_userAuthState.value = UserAuthState.Loading | ||
viewModelScope.launch { | ||
authModel.logout( | ||
onSuccess = { | ||
Log.d(TAG, "logOut: logged out successfully") | ||
// sharedPreferenceHelper.clearPreferences() | ||
_userAuthState.value = UserAuthState.Success("Logged out successfully") | ||
}, | ||
onFailure = { e: Exception -> | ||
Log.d(TAG, "logOut: failed to log out: $e") | ||
_userAuthState.value = UserAuthState.Error("Error: $e") | ||
}, | ||
) | ||
} | ||
} | ||
|
||
fun isUserLoggedIn(context: Context) { | ||
viewModelScope.launch { | ||
// call model for this ofc | ||
authModel.isUserLoggedIn( | ||
token = "", | ||
onSuccess = { | ||
Log.d(TAG, "isUserLoggedIn: user is confirmed logged in") | ||
_userAuthState.value = UserAuthState.Success("User is logged in") | ||
}, | ||
onFailure = { | ||
Log.d(TAG, "isUserLoggedIn: user is not logged in") | ||
_userAuthState.value = UserAuthState.Error("User is not logged in") | ||
}, | ||
) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...odpals/model/auth/PluginManagerWrapper.kt → ...el/authentication/PluginManagerWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.