Skip to content

Commit

Permalink
Implementing delete user view
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzemehdi committed Jan 26, 2024
1 parent 688340d commit 1b559e1
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
import util.logging.AppLogger
import kotlin.coroutines.CoroutineContext

class UserRepository(private val backgroundScope: CoroutineContext = Dispatchers.IO) {
Expand All @@ -26,7 +27,12 @@ class UserRepository(private val backgroundScope: CoroutineContext = Dispatchers
}

suspend fun deleteAccount() = withContext(backgroundScope) {
Firebase.auth.currentUser?.delete()
val currentUser=Firebase.auth.currentUser
currentUser?.providerData?.forEach {
AppLogger.e("ProviderId: ${it.providerId}")

}
currentUser?.delete()
}

}
5 changes: 5 additions & 0 deletions shared/src/commonMain/kotlin/domain/model/AuthProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package domain.model

enum class AuthProvider {
GOOGLE, APPLE, GITHUB
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package presentation.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.mmk.kmpauth.core.KMPAuthInternalApi
import com.mmk.kmpauth.core.di.isAndroidPlatform
import com.mmk.kmpauth.firebase.apple.AppleButtonUiContainer
import com.mmk.kmpauth.firebase.github.GithubButtonUiContainer
import com.mmk.kmpauth.firebase.google.GoogleButtonUiContainerFirebase
import com.mmk.kmpauth.uihelper.apple.AppleSignInButton
import com.mmk.kmpauth.uihelper.google.GoogleButtonMode
import com.mmk.kmpauth.uihelper.google.GoogleSignInButton
import dev.gitlive.firebase.auth.FirebaseUser
import domain.model.AuthProvider
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
import presentation.theme.strings.Strings

@Composable
fun AuthUiHelperButtonsAndFirebaseAuth(
modifier: Modifier = Modifier,
authProviders: List<AuthProvider> = AuthProvider.values().asList(),
onFirebaseResult: (Result<FirebaseUser?>) -> Unit,
) {
Column(
modifier = modifier,
verticalArrangement = Arrangement.spacedBy(14.dp)
) {
val shape = RoundedCornerShape(8.dp)
val height = 56.dp
val textFontSize = 24.sp

if (authProviders.contains(AuthProvider.GOOGLE)) {
//Google Sign-In Button and authentication with Firebase
GoogleButtonUiContainerFirebase(onResult = onFirebaseResult) {
GoogleSignInButton(
modifier = Modifier.fillMaxWidth().height(height),
text = Strings.btn_sign_in_with_google,
mode = GoogleButtonMode.Light,
fontSize = textFontSize,
shape = shape
) { this.onClick() }
}
}

if (authProviders.contains(AuthProvider.APPLE)) {
//Apple Sign-In Button and authentication with Firebase
AppleButtonUiContainer(onResult = onFirebaseResult) {
AppleSignInButton(
modifier = Modifier.fillMaxWidth().height(height),
text = Strings.btn_sign_in_with_apple,
shape = shape
) { this.onClick() }
}
}

if (authProviders.contains(AuthProvider.GITHUB)) {
//Github Sign-In Button and authentication with Firebase
GithubButtonUiContainer(onResult = onFirebaseResult) {
GithubSignInButton(
modifier = Modifier.fillMaxWidth().height(height),
text = Strings.btn_sign_in_with_github,
fontSize = textFontSize,
shape = shape
) { this.onClick() }
}
}

}
}

@OptIn(ExperimentalResourceApi::class, KMPAuthInternalApi::class)
@Composable
private fun GithubSignInButton(
modifier: Modifier = Modifier,
text: String = "Sign in with Github",
fontSize: TextUnit = 14.sp,
shape: Shape = ButtonDefaults.shape,
onClick: () -> Unit,
) {

val horizontalPadding = if (isAndroidPlatform()) 12.dp else 16.dp
val iconTextPadding = if (isAndroidPlatform()) 10.dp else 12.dp
val containerColor = Color.White
val contentColor = Color(0xFF1F1F1F)
val buttonColor =
ButtonDefaults.buttonColors(containerColor = containerColor, contentColor = contentColor)
Button(
modifier = modifier,
contentPadding = PaddingValues(horizontal = horizontalPadding),
onClick = onClick,
shape = shape,
colors = buttonColor,
border = BorderStroke(
width = 1.dp,
color = Color(0xFF747775),
),
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Image(
modifier = Modifier.size(20.dp),
painter = painterResource("drawable/ic_github.xml"),
contentDescription = "githubicon"
)
Spacer(modifier = Modifier.width(iconTextPadding))
Text(
text = text,
maxLines = 1,
fontSize = fontSize,
)
}

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package presentation.components

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import presentation.theme.Black_22
import presentation.theme.strings.Strings

@Composable
fun DeleteUserConfirmationDialog(
modifier: Modifier = Modifier,
onConfirm: () -> Unit,
onDismiss: () -> Unit,
) {
AlertDialog(
modifier = modifier,
onDismissRequest = {onDismiss() },
icon = { Icon(imageVector = Icons.Filled.Delete, contentDescription = "Delete") },
title = {
Text(text = Strings.title_delete_user_dialog, color = Black_22)
},
text = {
Text(
text = Strings.description_delete_user_dialog,
color = Black_22
)
},
confirmButton = {
TextButton(
onClick = { onConfirm() },
) {
Text(
text = Strings.btn_delete,
color = MaterialTheme.colorScheme.secondary,
style = MaterialTheme.typography.titleSmall
)
}

},
dismissButton = {
Button(
colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.secondary),
onClick = { onDismiss() },
) {
Text(
text = Strings.btn_cancel,
color = Color.White,
style = MaterialTheme.typography.titleSmall
)
}
}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand All @@ -19,8 +20,10 @@ import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -35,9 +38,12 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePainter
import dev.gitlive.firebase.auth.FirebaseUser
import domain.model.User
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.painterResource
import presentation.components.AuthUiHelperButtonsAndFirebaseAuth
import presentation.components.DeleteUserConfirmationDialog
import presentation.components.ExpandableBoxItem
import presentation.components.GradientButton
import presentation.components.MyAppCircularProgressIndicator
Expand All @@ -51,11 +57,24 @@ import util.asState
@Composable
fun ProfileScreen(modifier: Modifier = Modifier, uiStateHolder: ProfileUiStateHolder) {
val uiState by uiStateHolder.profileScreenUiState.asState()
if (uiState.reAuthenticateUserViewShown) {
SocialLoginsBottomSheet(
onDismiss = uiStateHolder::onDismissReAuthenticateView,
onResult = uiStateHolder::onUserReAuthenticatedResult
)
}

if (uiState.isLoading) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
MyAppCircularProgressIndicator()
}
}
if (uiState.deleteUserDialogShown) {
DeleteUserConfirmationDialog(
onConfirm = uiStateHolder::onConfirmDeleteAccount,
onDismiss = uiStateHolder::onDismissDeleteUserConfirmationDialog
)
}
uiState.currentUser?.let { currentUser ->
ProfileScreen(
modifier = modifier.fillMaxSize(),
Expand Down Expand Up @@ -199,3 +218,21 @@ private fun BasicInfo(modifier: Modifier = Modifier, currentUser: User) {
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SocialLoginsBottomSheet(onDismiss: () -> Unit, onResult: (Result<FirebaseUser?>) -> Unit) {
ModalBottomSheet(

windowInsets = WindowInsets(0),
sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true),
dragHandle = {},
onDismissRequest = { onDismiss() }
) {
Box(modifier = Modifier.padding(40.dp)) {
AuthUiHelperButtonsAndFirebaseAuth(onFirebaseResult = onResult)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ package presentation.screens.account.profile

import domain.model.User

data class ProfileScreenUiState(val currentUser: User? = null, val isLoading: Boolean = true)
data class ProfileScreenUiState(
val currentUser: User? = null,
val isLoading: Boolean = true,
val deleteUserDialogShown: Boolean = false,
val reAuthenticateUserViewShown: Boolean = false,
)
Loading

0 comments on commit 1b559e1

Please sign in to comment.