#52 Add logout functionality
This commit is contained in:
		
							parent
							
								
									22b5a27102
								
							
						
					
					
						commit
						85e1b55507
					
				
					 4 changed files with 31 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -0,0 +1,120 @@
 | 
			
		|||
package be.ugent.sel.studeez.common.composable
 | 
			
		||||
 | 
			
		||||
import androidx.compose.foundation.Image
 | 
			
		||||
import androidx.compose.foundation.clickable
 | 
			
		||||
import androidx.compose.foundation.layout.*
 | 
			
		||||
import androidx.compose.material.Divider
 | 
			
		||||
import androidx.compose.material.Icon
 | 
			
		||||
import androidx.compose.material.Text
 | 
			
		||||
import androidx.compose.material.icons.Icons
 | 
			
		||||
import androidx.compose.material.icons.filled.*
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.graphics.vector.ImageVector
 | 
			
		||||
import androidx.compose.ui.res.painterResource
 | 
			
		||||
import androidx.compose.ui.res.stringResource
 | 
			
		||||
import androidx.compose.ui.tooling.preview.Preview
 | 
			
		||||
import androidx.hilt.navigation.compose.hiltViewModel
 | 
			
		||||
import be.ugent.sel.studeez.R
 | 
			
		||||
import be.ugent.sel.studeez.resources
 | 
			
		||||
import be.ugent.sel.studeez.screens.drawer.DrawerViewModel
 | 
			
		||||
import be.ugent.sel.studeez.ui.theme.StudeezTheme
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun Drawer(
 | 
			
		||||
    openAndPopup: (String, String) -> Unit,
 | 
			
		||||
    viewModel: DrawerViewModel = hiltViewModel()
 | 
			
		||||
) {
 | 
			
		||||
    Column(modifier = Modifier.fillMaxSize()) {
 | 
			
		||||
        LoggedInUserCard()
 | 
			
		||||
 | 
			
		||||
        Divider()
 | 
			
		||||
 | 
			
		||||
        DrawerEntry(
 | 
			
		||||
            icon = Icons.Default.Home,
 | 
			
		||||
            text = resources().getString(R.string.home)
 | 
			
		||||
        ) {
 | 
			
		||||
            // TODO Go to home
 | 
			
		||||
        }
 | 
			
		||||
        DrawerEntry(
 | 
			
		||||
            icon = Icons.Default.LocationOn, // TODO Fix icon
 | 
			
		||||
            text = resources().getString(R.string.timers)
 | 
			
		||||
        ) {
 | 
			
		||||
            // TODO Go to timers
 | 
			
		||||
        }
 | 
			
		||||
        DrawerEntry(
 | 
			
		||||
            icon = Icons.Default.Settings,
 | 
			
		||||
            text = resources().getString(R.string.settings)
 | 
			
		||||
        ) {
 | 
			
		||||
            // TODO Go to settings
 | 
			
		||||
        }
 | 
			
		||||
        DrawerEntry(
 | 
			
		||||
            icon = Icons.Default.AccountBox, // TODO Fix icon
 | 
			
		||||
            text = resources().getString(R.string.log_out)
 | 
			
		||||
        ) {
 | 
			
		||||
            viewModel.onLogoutClick(openAndPopup)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        DrawerEntry(
 | 
			
		||||
            icon = Icons.Default.Info,
 | 
			
		||||
            text = resources().getString(R.string.about)
 | 
			
		||||
        ) {
 | 
			
		||||
            // TODO Go to about
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun DrawerEntry(
 | 
			
		||||
    icon: ImageVector,
 | 
			
		||||
    text: String,
 | 
			
		||||
    onClick: () -> Unit
 | 
			
		||||
) {
 | 
			
		||||
    Row(
 | 
			
		||||
        horizontalArrangement = Arrangement.Center,
 | 
			
		||||
        modifier = Modifier
 | 
			
		||||
            .clickable(onClick = { onClick() })
 | 
			
		||||
            .fillMaxWidth()
 | 
			
		||||
    ) {
 | 
			
		||||
        Box(modifier = Modifier.fillMaxWidth(0.25f)) {
 | 
			
		||||
            Icon(imageVector = icon, contentDescription = text)
 | 
			
		||||
        }
 | 
			
		||||
        Box(modifier = Modifier.fillMaxWidth(0.75f)) {
 | 
			
		||||
            Text(text = text)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun LoggedInUserCard() {
 | 
			
		||||
    Column() {
 | 
			
		||||
        // TODO Profile picture of current user
 | 
			
		||||
        Image(
 | 
			
		||||
            painter = painterResource(id = R.drawable.ic_launcher_background),
 | 
			
		||||
            contentDescription = stringResource(R.string.profile_picture_description)
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        // TODO Username
 | 
			
		||||
        Text(text = "Username todo")
 | 
			
		||||
 | 
			
		||||
        // TODO Description of user (normal user or something else?)
 | 
			
		||||
        Text(text = stringResource(id = R.string.user_description))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun DrawerPreview() {
 | 
			
		||||
    StudeezTheme {
 | 
			
		||||
        Drawer({ a, b -> {}})
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Preview
 | 
			
		||||
@Composable
 | 
			
		||||
fun LoggedInUserCardPreview() {
 | 
			
		||||
    StudeezTheme {
 | 
			
		||||
        LoggedInUserCard()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
package be.ugent.sel.studeez.screens.drawer
 | 
			
		||||
 | 
			
		||||
import be.ugent.sel.studeez.domain.AccountDAO
 | 
			
		||||
import be.ugent.sel.studeez.domain.LogService
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations.HOME_SCREEN
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations.LOGIN_SCREEN
 | 
			
		||||
import be.ugent.sel.studeez.screens.StudeezViewModel
 | 
			
		||||
import dagger.hilt.android.lifecycle.HiltViewModel
 | 
			
		||||
import javax.inject.Inject
 | 
			
		||||
 | 
			
		||||
@HiltViewModel
 | 
			
		||||
class DrawerViewModel @Inject constructor(
 | 
			
		||||
    private val accountDAO: AccountDAO,
 | 
			
		||||
    logService: LogService
 | 
			
		||||
): StudeezViewModel(logService) {
 | 
			
		||||
    fun onLogoutClick(openAndPopup: (String, String) -> Unit) {
 | 
			
		||||
        launchCatching {
 | 
			
		||||
            accountDAO.signOut()
 | 
			
		||||
            openAndPopup(LOGIN_SCREEN, HOME_SCREEN)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -5,7 +5,6 @@ import be.ugent.sel.studeez.common.ext.isValidEmail
 | 
			
		|||
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
 | 
			
		||||
import be.ugent.sel.studeez.domain.AccountDAO
 | 
			
		||||
import be.ugent.sel.studeez.domain.LogService
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations.HOME_SCREEN
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations.LOGIN_SCREEN
 | 
			
		||||
import be.ugent.sel.studeez.navigation.StudeezDestinations.SIGN_UP_SCREEN
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue