Skip to content

Commit

Permalink
feat: rename package model.auth to model.authentication for consi…
Browse files Browse the repository at this point in the history
…stency

Updated references project-wide as well.
  • Loading branch information
charliemangano committed Oct 29, 2024
1 parent 2eea42f commit 98a1af4
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 590 deletions.
45 changes: 23 additions & 22 deletions app/src/main/java/com/android/periodpals/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import com.android.periodpals.model.auth.AuthModelSupabase
import com.android.periodpals.model.auth.AuthViewModel
import com.android.periodpals.model.authentication.AuthModelSupabase
import com.android.periodpals.model.authentication.AuthViewModel
import com.android.periodpals.ui.alert.AlertListScreen
import com.android.periodpals.ui.alert.AlertScreen
import com.android.periodpals.ui.authentication.SignInScreen
Expand Down Expand Up @@ -55,12 +55,12 @@ class MainActivity : ComponentActivity() {
}

private val supabaseClient =
createSupabaseClient(
supabaseUrl = BuildConfig.SUPABASE_URL,
supabaseKey = BuildConfig.SUPABASE_KEY,
) {
install(Auth)
}
createSupabaseClient(
supabaseUrl = BuildConfig.SUPABASE_URL,
supabaseKey = BuildConfig.SUPABASE_KEY,
) {
install(Auth)
}

private val authModel = AuthModelSupabase(supabaseClient)
private val authViewModel = AuthViewModel(authModel)
Expand All @@ -86,31 +86,35 @@ class MainActivity : ComponentActivity() {

// Check if location permission is granted or request it if not
private fun checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
if (
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED
) {
// **Permission is granted, update state**
locationPermissionGranted = true
} else {
// **Request permission**
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_PERMISSION_REQUEST_CODE,
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_PERMISSION_REQUEST_CODE,
)
}
}

// Handle permission result and check if permission was granted
@Deprecated("Deprecated in Java")
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE &&
if (
requestCode == LOCATION_PERMISSION_REQUEST_CODE &&
grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
// **Permission granted, update state**
locationPermissionGranted = true
} else {
Expand All @@ -127,10 +131,7 @@ fun PeriodPalsApp(locationPermissionGranted: Boolean, authViewModel: AuthViewMod

NavHost(navController = navController, startDestination = Route.AUTH) {
// Authentication
navigation(
startDestination = Screen.SIGN_IN,
route = Route.AUTH,
) {
navigation(startDestination = Screen.SIGN_IN, route = Route.AUTH) {
composable(Screen.SIGN_IN) { SignInScreen(authViewModel, navigationActions) }
composable(Screen.SIGN_UP) { SignUpScreen(authViewModel, navigationActions) }
composable(Screen.CREATE_PROFILE) { CreateProfileScreen(navigationActions) }
Expand Down
22 changes: 0 additions & 22 deletions app/src/main/java/com/android/periodpals/model/auth/AuthModel.kt

This file was deleted.

This file was deleted.

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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.android.periodpals.model.auth
package com.android.periodpals.model.authentication

import android.util.Log
import io.github.jan.supabase.SupabaseClient
Expand All @@ -9,18 +9,18 @@ import io.github.jan.supabase.auth.providers.builtin.Email
private const val TAG = "AuthModelSupabase"

class AuthModelSupabase(
private val supabase: SupabaseClient,
private val pluginManagerWrapper: PluginManagerWrapper =
PluginManagerWrapperImpl(supabase.pluginManager),
private val supabase: SupabaseClient,
private val pluginManagerWrapper: PluginManagerWrapper =
PluginManagerWrapperImpl(supabase.pluginManager),
) : AuthModel {

private val supabaseAuth: Auth = pluginManagerWrapper.getAuthPlugin()

override suspend fun register(
userEmail: String,
userPassword: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
userEmail: String,
userPassword: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
supabaseAuth.signUpWith(Email) {
Expand All @@ -36,10 +36,10 @@ class AuthModelSupabase(
}

override suspend fun login(
userEmail: String,
userPassword: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
userEmail: String,
userPassword: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
supabaseAuth.signInWith(Email) {
Expand All @@ -66,9 +66,9 @@ class AuthModelSupabase(
}

override suspend fun isUserLoggedIn(
token: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
token: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit,
) {
try {
if (null != supabaseAuth.currentUserOrNull()) {
Expand Down
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")
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.android.periodpals.model.auth
package com.android.periodpals.model.authentication

import io.github.jan.supabase.auth.Auth
import io.github.jan.supabase.plugins.PluginManager
Expand Down
Loading

0 comments on commit 98a1af4

Please sign in to comment.