Skip to content

Commit

Permalink
toolbar scrollbehaviour added
Browse files Browse the repository at this point in the history
  • Loading branch information
Na Ran authored and Na Ran committed Feb 29, 2024
1 parent 47fdb02 commit fefdd4a
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 141 deletions.
19 changes: 12 additions & 7 deletions app/src/main/java/com/iamnaran/firefly/di/SharedPrefModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,28 @@ object SharedPrefModule {
@ApplicationContext context: Context,
@SharedPrefInfoQualifier fileName: String
): SharedPreferences {
val sharedPreferences: SharedPreferences = try {
val masterKey: MasterKey = MasterKey.Builder(context)
try {
val masterKeyAlias = MasterKey.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
EncryptedSharedPreferences.create(

return EncryptedSharedPreferences.create(
context,
fileName,
masterKey,
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
} catch (e: GeneralSecurityException) {
context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
// Handle cryptographic exception
e.printStackTrace()
} catch (e: IOException) {
context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
// Handle IO exception
e.printStackTrace()
}
return sharedPreferences

// If an exception occurs, fallback to using regular SharedPreferences
return context.getSharedPreferences(fileName, Context.MODE_PRIVATE)
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import com.iamnaran.firefly.ui.appcomponent.AppIcons
import com.iamnaran.firefly.ui.theme.AppIcons
import com.iamnaran.firefly.ui.theme.Shapes

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.iamnaran.firefly.R
import com.iamnaran.firefly.ui.appcomponent.AppIcons
import com.iamnaran.firefly.ui.theme.AppIcons
import com.iamnaran.firefly.ui.appcomponent.common.EmailInput
import com.iamnaran.firefly.ui.appcomponent.common.PasswordInput
import com.iamnaran.firefly.ui.theme.FireflyComposeTheme
Expand Down
119 changes: 48 additions & 71 deletions app/src/main/java/com/iamnaran/firefly/ui/auth/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,91 +63,68 @@ class LoginViewModel @Inject constructor(
)
)
)
viewModelScope.launch {
postServerLoginUseCase(
_loginState.value.email,
_loginState.value.password
)
.onStart {
doLoginWork()

}
.onCompletion {
}

}
.collectLatest { resource ->
when (resource) {
is Resource.Success -> {
if (resource.data != null) {
AppLog.showLog("----------LOGIN SUCCESS----------")
preferenceHelper.saveLoggedInUserDetails(
resource.data.id.toString(),
resource.data.token,
true,
)
setLoggedInUserUseCase(resource.data)
_loginState.value = _loginState.value.copy(
isLoginSuccessful = true,
isLoading = false
)
}
}

}
}
private fun doLoginWork() {

is Resource.Loading -> {
_loginState.value = _loginState.value.copy(
isLoading = true
)
}
viewModelScope.launch {
postServerLoginUseCase(
_loginState.value.email,
_loginState.value.password
)
.onStart {

else -> {
}
.onCompletion {

}
.collectLatest { resource ->
when (resource) {
is Resource.Success -> {
if (resource.data != null) {
AppLog.showLog("----------LOGIN SUCCESS----------")
preferenceHelper.saveLoggedInUserDetails(
resource.data.id.toString(),
resource.data.token,
true,
)
setLoggedInUserUseCase(resource.data)
_loginState.value = _loginState.value.copy(
isLoginSuccessful = false,
isLoginSuccessful = true,
isLoading = false
)

_loginState.value = _loginState.value.copy(
loginErrorState = LoginErrorState(
serverErrorState = ErrorState(
true,
serverErrorMsg = resource.message.toString()
)
}
}

is Resource.Loading -> {
_loginState.value = _loginState.value.copy(
isLoading = true
)
}

else -> {
_loginState.value = _loginState.value.copy(
isLoginSuccessful = false,
isLoading = false
)

_loginState.value = _loginState.value.copy(
loginErrorState = LoginErrorState(
serverErrorState = ErrorState(
true,
serverErrorMsg = resource.message.toString()
)
)
}
)
}
}
}

}

}

}


// private fun validateInputs(): Boolean {
// val emailOrMobileString = _emailState.value.trim()
// val passwordString = _passwordState.value.trim()
// return when {
//
// // Email/Mobile empty
// emailOrMobileString.isEmpty() -> {
//
// false
// }
//
// //Password Empty
// passwordString.isEmpty() -> {
//
// false
// }
//
// // No errors
// else -> {
// // Set default error state
// true
// }
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.iamnaran.firefly.R
import com.iamnaran.firefly.ui.appcomponent.AppIcons
import com.iamnaran.firefly.ui.theme.AppIcons
import com.iamnaran.firefly.ui.appcomponent.common.EmailInput
import com.iamnaran.firefly.ui.appcomponent.common.PasswordInput
import com.iamnaran.firefly.ui.theme.FireflyComposeTheme
Expand Down
108 changes: 57 additions & 51 deletions app/src/main/java/com/iamnaran/firefly/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.iamnaran.firefly.ui.auth.login.LoginScreen
import com.iamnaran.firefly.ui.navigation.AppScreen
import com.iamnaran.firefly.ui.navigation.bottomappbar.BottomBar
import com.iamnaran.firefly.ui.navigation.multiplebackstack.RootMultipleBackStackNavHost
import com.iamnaran.firefly.ui.navigation.topappbar.AppTopBar
import com.iamnaran.firefly.ui.theme.FireflyComposeTheme
import com.iamnaran.firefly.utils.AppLog
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -34,9 +38,7 @@ import dagger.hilt.android.AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val TAG: String = AppLog.tagFor(this.javaClass)
private lateinit var navController: NavHostController
private val mainViewModel: MainViewModel by viewModels()

private var isAuthenticated = false

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -45,61 +47,71 @@ class MainActivity : ComponentActivity() {
isAuthenticated = mainViewModel.isUserAuthenticated()
setContent {
FireflyComposeTheme {
navController = rememberNavController()

val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route

val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }

val bottomBarState = rememberSaveable { (mutableStateOf(true)) }
val topBarState = rememberSaveable { (mutableStateOf(true)) }

// Control TopBar and BottomBar
when (navBackStackEntry?.destination?.route) {
AppScreen.Main.Home.route -> {
bottomBarState.value = true
topBarState.value = true
}

AppScreen.Main.Profile.route -> {
bottomBarState.value = true
topBarState.value = true
}

AppScreen.Main.Notification.route -> {
bottomBarState.value = true
topBarState.value = true
}

else -> {
bottomBarState.value = false
topBarState.value = false
}
}

MainScreenContent(snackbarHostState,bottomBarState,navController,isAuthenticated)

MainScreenContent(isAuthenticated)
}
}
}


}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MainScreenContent(
snackbarHostState: SnackbarHostState,
bottomBarState: MutableState<Boolean>,
navController: NavHostController,
isAuthenticated: Boolean
) {
val topAppbarTitle = remember { mutableStateOf("") }
val topAppBarState = rememberTopAppBarState()
val barScrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(state = topAppBarState)
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
val bottomBarState = rememberSaveable { (mutableStateOf(true)) }
val topBarState = rememberSaveable { (mutableStateOf(true)) }

// Control TopBar and BottomBar
when (navBackStackEntry?.destination?.route) {
AppScreen.Main.Home.route -> {
bottomBarState.value = true
topBarState.value = true
topAppbarTitle.value = stringResource(AppScreen.Main.Home.title!!)

}

AppScreen.Main.Profile.route -> {
bottomBarState.value = true
topBarState.value = true
topAppbarTitle.value = stringResource(AppScreen.Main.Profile.title!!)

}

AppScreen.Main.Notification.route -> {
bottomBarState.value = true
topBarState.value = true
topAppbarTitle.value = stringResource(AppScreen.Main.Notification.title!!)

}

else -> {
bottomBarState.value = false
topBarState.value = false
}
}

Scaffold(
modifier = Modifier
.fillMaxSize()
.nestedScroll(barScrollBehavior.nestedScrollConnection),
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
topBar = {
if (topBarState.value) {
AppTopBar(topAppbarTitle.value, barScrollBehavior)
}
},
bottomBar = {
if (bottomBarState.value) {
BottomBar(navController = navController)
Expand Down Expand Up @@ -129,19 +141,13 @@ private fun MainScreenContent(
}




@Preview(showBackground = true)
@Composable
fun MainActivityPreview() {
FireflyComposeTheme {
LoginScreen(
navigateToHome = {},
navigateToSignUp = {})
MainScreenContent(true)
}
}




//https://gist.github.com/stevdza-san/5743a32dc1ec2d8eaf62c29d9bab1c43
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.iamnaran.firefly.ui.main.home

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.iamnaran.firefly.ui.main.home.components.ProductLazyList
import com.iamnaran.firefly.utils.AppLog
Expand Down
Loading

0 comments on commit fefdd4a

Please sign in to comment.