Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #46 from XavierDupuis/firebase
Browse files Browse the repository at this point in the history
Firebase update -> Main
  • Loading branch information
XavierDupuis committed Apr 21, 2023
2 parents 64246b1 + 73c60ba commit e0d0eaf
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 139 deletions.
7 changes: 4 additions & 3 deletions Projet/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ dependencies {
def coil_version = "2.3.0"
implementation "io.coil-kt:coil-compose:$coil_version"

// Firebase (Auth & Firestore)
implementation platform('com.google.firebase:firebase-bom:31.4.0')
implementation "com.google.firebase:firebase-auth-ktx:21.0.3"
// Firebase (Auth & Firestore & Storage)
def firebase_bom_version = "31.5.0"
implementation platform("com.google.firebase:firebase-bom:$firebase_bom_version")
implementation "com.google.firebase:firebase-auth-ktx"
implementation "com.google.firebase:firebase-firestore-ktx"
implementation "com.google.firebase:firebase-storage-ktx"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class AccountRepository @Inject constructor(
uploadImage(path, username, onComplete)
}

private fun uploadImage(path: Uri, filename: String, onComplete: ((Boolean) -> Unit)?) {
private fun uploadImage(path: Uri, username: String, onComplete: ((Boolean) -> Unit)?) {
val filename = username.lowercase()
val imageReference = firebaseStorage.reference.child("images//${filename}.jpg")
val uploadTask = imageReference.putFile(path)
uploadTask
Expand All @@ -63,7 +64,8 @@ class AccountRepository @Inject constructor(
}

fun getProfilePicture(username: String, onComplete: (Uri?) -> Unit) {
val imageReference = firebaseStorage.reference.child("images//${username}.jpg")
val filename = username.lowercase()
val imageReference = firebaseStorage.reference.child("images//${filename}.jpg")
val downloadUrl = imageReference.downloadUrl
.addOnFailureListener {
onComplete(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.net.Uri
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavHostController
import com.example.bluetoothdetector.R
import com.example.bluetoothdetector.auth.domain.AccountRedirection
import com.example.bluetoothdetector.auth.viewmodel.AuthViewModel
Expand All @@ -18,23 +17,20 @@ import java.util.*

@Composable
fun AccountScreen(
navController: NavHostController,
navigate: (Page) -> Unit,
authViewModel: AuthViewModel
) {
val currentUser = authViewModel.currentUser.collectAsState(null).value
if (currentUser == null) {
LoginScreen(navController, authViewModel)
} else {
authViewModel.currentUser.collectAsState(null).value?.let {
AuthView(
Page.ACCOUNT,
navController,
navigate,
authViewModel,
AccountRedirection
) {
WelcomeBackView(currentUser)
ProfilePictureView(authViewModel, currentUser)
LastSignInView(currentUser)
AccountCreatedView(currentUser)
WelcomeBackView(it)
ProfilePictureView(authViewModel, it)
LastSignInView(it)
AccountCreatedView(it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.bluetoothdetector.auth.domain.Redirection
import com.example.bluetoothdetector.auth.viewmodel.AuthViewModel
import com.example.bluetoothdetector.common.domain.Page
Expand All @@ -25,7 +24,7 @@ import com.example.bluetoothdetector.common.view.typography.Title
@Composable
fun AuthView(
page: Page,
navController: NavHostController,
navigate: (Page) -> Unit,
authViewModel: AuthViewModel,
redirection: Redirection,
confirm: ((Context) -> Unit)? = null,
Expand All @@ -46,7 +45,7 @@ fun AuthView(
ConfirmButton(page.description, confirm)
}
Spacer(modifier = Modifier.size(16.dp))
Redirect(navController, authViewModel, redirection)
Redirect(navigate, authViewModel, redirection)
}
}

Expand All @@ -61,7 +60,7 @@ fun ConfirmButton(value: Int, confirm: (Context) -> Unit) {

@Composable
fun Redirect(
navController: NavHostController,
navigate: (Page) -> Unit,
authViewModel: AuthViewModel,
redirection: Redirection,
) {
Expand All @@ -70,7 +69,7 @@ fun Redirect(
Spacer(modifier = Modifier.size(8.dp))
TextButton(onClick = {
redirection.action?.let { it(authViewModel) }
authViewModel.navigate(navController, redirection.page)
authViewModel.navigate(navigate, redirection.page)
}) {
Text(stringResource(redirection.label))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Fingerprint
import androidx.compose.material.icons.filled.Password
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import com.example.bluetoothdetector.R
import com.example.bluetoothdetector.auth.domain.LoginRedirection
import com.example.bluetoothdetector.auth.viewmodel.AuthViewModel
Expand All @@ -13,15 +12,15 @@ import com.example.bluetoothdetector.common.domain.Page

@Composable
fun LoginScreen(
navController: NavHostController,
navigate: (Page) -> Unit,
authViewModel: AuthViewModel
) {
AuthView(
Page.LOGIN,
navController,
navigate,
authViewModel,
LoginRedirection,
{ authViewModel.login(it, navController) }
{ authViewModel.login(it, navigate) }
) {
UsernameField(authViewModel)
PasswordField(authViewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Fingerprint
import androidx.compose.material.icons.filled.Password
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import com.example.bluetoothdetector.R
import com.example.bluetoothdetector.auth.domain.SignupRedirection
import com.example.bluetoothdetector.auth.viewmodel.AuthViewModel
Expand All @@ -13,15 +12,15 @@ import com.example.bluetoothdetector.common.view.camera.ImagePicker

@Composable
fun SignupScreen(
navController: NavHostController,
navigate: (Page) -> Unit,
authViewModel: AuthViewModel
) {
AuthView(
Page.SIGNUP,
navController,
navigate,
authViewModel,
SignupRedirection,
{ authViewModel.signup(it, navController) },
{ authViewModel.signup(it, navigate) },
) {
UsernameField(authViewModel)
PasswordField(authViewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavHostController
import com.example.bluetoothdetector.R
import com.example.bluetoothdetector.auth.model.AuthState
import com.example.bluetoothdetector.auth.repository.AccountRepository
Expand Down Expand Up @@ -88,7 +87,7 @@ class AuthViewModel @Inject constructor(

fun signup(
context: Context,
navController: NavHostController
navigate: (Page) -> Unit
) = viewModelScope.launch {
authState = authState.copy(isLoading = true)
authState = authState.copy(error = null)
Expand All @@ -108,7 +107,7 @@ class AuthViewModel @Inject constructor(
authState = authState.copy(isSuccess = isSuccess)
if (isSuccess) {
profilePictureUri.value?.let {
uploadProfilePicture(it, navController)
uploadProfilePicture(it, navigate)
}
}
}
Expand All @@ -117,18 +116,18 @@ class AuthViewModel @Inject constructor(

private fun uploadProfilePicture(
uri: Uri,
navController: NavHostController
navigate: (Page) -> Unit
) = viewModelScope.launch {
accountRepository.setProfilePicture(uri, authState.usernameSignup) { imageUploaded ->
if (imageUploaded) {
navigate(navController, Page.ACCOUNT)
navigate(navigate, Page.ACCOUNT)
}
}
}

fun login(
context: Context,
navController: NavHostController
navigate: (Page) -> Unit
) = viewModelScope.launch {
authState = authState.copy(isLoading = true)
authState = authState.copy(error = null)
Expand All @@ -147,7 +146,7 @@ class AuthViewModel @Inject constructor(
) { isSuccess ->
authState = authState.copy(isSuccess = isSuccess)
if (isSuccess) {
navigate(navController, Page.ACCOUNT)
navigate(navigate, Page.ACCOUNT)
}
}
}
Expand All @@ -157,18 +156,19 @@ class AuthViewModel @Inject constructor(
try {
authOperation()
} catch (exception: Exception) {
authState = authState.copy(error = exception.localizedMessage)
exception.printStackTrace()
authState = authState.copy(error = exception.localizedMessage)
authState = authState.copy(isLoading = false)
}
}

fun signOut() = viewModelScope.launch {
accountRepository.signOut()
}

fun navigate(navController: NavHostController, page: Page) {
fun navigate(navigate: (Page) -> Unit, page: Page) {
clearState()
navController.navigate(page.route)
navigate(page)
}

fun getProfilePictureUri(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ enum class Page(
companion object {
val Pages = Page.values()
val MenuPages = Pages.filter { it.inMenu }
val StartPage = SPLASH
val LoggedInPage = MAIN
val LoggedOutPage = LOGIN
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material.DrawerState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.bluetoothdetector.common.view.containers.CenteredHorizontalContainer
import com.example.bluetoothdetector.common.view.languages.LanguagesHeader
import com.example.bluetoothdetector.common.view.typography.Subtitle
import com.example.bluetoothdetector.menu.view.MenuHeader
import com.example.bluetoothdetector.menu.viewmodel.MenuViewModel
import kotlinx.coroutines.CoroutineScope
Expand All @@ -20,13 +17,12 @@ import kotlinx.coroutines.CoroutineScope
fun HeaderView(
menuState: DrawerState,
menuScope: CoroutineScope,
viewModel: MenuViewModel,
) {
val viewModel: MenuViewModel = hiltViewModel()
CenteredHorizontalContainer(
modifier = Modifier.padding(horizontal = 12.dp)
) {
MenuHeader(menuState, menuScope, viewModel)
Subtitle(stringResource(viewModel.selectedTab.value.denomination).uppercase())
Spacer(modifier = Modifier.weight(1f))
ThemeSelector()
Spacer(modifier = Modifier.weight(1f))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.material.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
Expand All @@ -25,24 +26,28 @@ import com.example.bluetoothdetector.main.view.MainScreen
import com.example.bluetoothdetector.main.view.MapView
import com.example.bluetoothdetector.main.view.NetworkView
import com.example.bluetoothdetector.menu.view.MenuDrawer
import com.example.bluetoothdetector.menu.viewmodel.MenuViewModel
import com.example.bluetoothdetector.splash.view.SplashScreen


@Composable
fun Navigation(
permissionsViewModel: PermissionsViewModel,
startDestination: Page = Page.SPLASH,
navController: NavHostController = rememberNavController()
) {
val menuState = rememberDrawerState(DrawerValue.Closed)
val menuScope = rememberCoroutineScope()
val authViewModel: AuthViewModel = hiltViewModel()
val menuViewModel: MenuViewModel = viewModel()
val navigate: (Page) -> Unit = {
menuViewModel.navigate(navController, menuState, menuScope, it)
}

PageWithHeader(menuState, menuScope) {
MenuDrawer(menuState, menuScope, navController) {
NavHost(navController, startDestination.route) {
PageWithHeader(menuState, menuScope, menuViewModel) {
MenuDrawer(menuState, navigate, menuViewModel) {
NavHost(navController, Page.StartPage.route) {
pageComposable(Page.SPLASH) {
SplashScreen(navController)
SplashScreen(navigate)
}

pageComposable(Page.MAIN) {
Expand All @@ -58,15 +63,15 @@ fun Navigation(
}

pageComposable(Page.LOGIN) {
LoginScreen(navController, authViewModel)
LoginScreen(navigate, authViewModel)
}

pageComposable(Page.SIGNUP) {
SignupScreen(navController, authViewModel)
SignupScreen(navigate, authViewModel)
}

pageComposable(Page.ACCOUNT) {
AccountScreen(navController, authViewModel)
AccountScreen(navigate, authViewModel)
}

pageComposable(Page.ENERGY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ fun SelectableListItem(
icon = { icon(isSelected) }
)
}

}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.example.bluetoothdetector.common.view.languages

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.bluetoothdetector.R
import com.example.bluetoothdetector.common.domain.action.Action
import com.example.bluetoothdetector.common.domain.modal.ModalActions
Expand All @@ -19,14 +22,16 @@ fun LanguagesModal(
title = R.string.choose_language,
closeModal = { viewModel.closeModal() }
) {
LanguageRepository.AvailableLanguages.forEach {
SelectableListItem(
modifier = Modifier.fillMaxWidth(0.4f),
isSelected = { viewModel.isSelectedLanguage(it) },
select = { viewModel.selectLanguage(it) },
mainText = it.denomination,
icon = it.abbreviation
)
LazyColumn {
items(LanguageRepository.AvailableLanguages) {
SelectableListItem(
modifier = Modifier.widthIn(max = 300.dp),
isSelected = { viewModel.isSelectedLanguage(it) },
select = { viewModel.selectLanguage(it) },
mainText = it.denomination,
icon = it.abbreviation
)
}
}
it(
ModalActions(
Expand Down
Loading

0 comments on commit e0d0eaf

Please sign in to comment.