Skip to content

Commit

Permalink
fix navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedshasho committed Aug 12, 2023
1 parent 9f5878f commit 2ed8b02
Show file tree
Hide file tree
Showing 26 changed files with 246 additions and 114 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies {
implementation 'de.hdodenhof:circleimageview:3.1.0'

//DataStore
implementation("androidx.datastore:datastore-preferences:1.0.0")
implementation("androidx.datastore:datastore-preferences:1.0.0")// todo try to delete this

//Splash Screen
implementation("androidx.core:core-splashscreen:1.0.1")
Expand Down
17 changes: 4 additions & 13 deletions app/src/main/java/com/scholar/center/adapter/CategoryAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.scholar.center.R
import com.scholar.center.databinding.CategoryItemBinding
import com.scholar.center.unit.getThemeColor
import com.scholar.domain.model.Category
import com.google.android.material.R.attr as theme

Expand All @@ -30,14 +31,12 @@ class CategoryAdapter(
onClick?.invoke(position)
}
if (itemSelected == position) {
val selectedColor = getThemeColor(
holder.itemView.context,
val selectedColor = holder.itemView.context.getThemeColor(
theme.colorSecondaryVariant
)
holder.binding.categoryItemCard.setCardBackgroundColor(selectedColor)
} else {
val unSelectedColor = getThemeColor(
holder.itemView.context,
val unSelectedColor = holder.itemView.context.getThemeColor(
theme.colorPrimaryContainer
)
holder.binding.categoryItemCard.setCardBackgroundColor(unSelectedColor)
Expand All @@ -62,15 +61,7 @@ class CategoryAdapter(
*/
}

private fun getThemeColor(context: Context, resId: Int): Int {
val typedValue = TypedValue()
context.theme.resolveAttribute(
resId,
typedValue,
true
)
return typedValue.data
}


fun setItemSelected(position: Int) {
val previousSelected = itemSelected
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.scholar.center.ui.auth.login

import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.view.animation.AlphaAnimation
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
Expand All @@ -13,9 +15,11 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.scholar.center.R
import com.scholar.center.databinding.FragmentLoginBinding
import com.scholar.center.unit.Constants.MUST_POP_BACK_KEY
import com.scholar.center.unit.getThemeColor
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

import com.google.android.material.R.attr as theme

@AndroidEntryPoint
class LoginFragment : Fragment(R.layout.fragment_login) {
Expand All @@ -26,6 +30,21 @@ class LoginFragment : Fragment(R.layout.fragment_login) {

binding = FragmentLoginBinding.bind(view)

val mustPopBackStack = arguments?.getBoolean(MUST_POP_BACK_KEY) ?: false

val text = getString(R.string.welcome)
val colorPrimaryContainer = requireContext().getThemeColor(
theme.colorPrimary
)
val spannable = SpannableString(text)
spannable.setSpan(
ForegroundColorSpan(colorPrimaryContainer),
0, text.split(" ").first().length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)

binding.loginFragmentWelcomeText.text = spannable


val navController = findNavController()

Expand All @@ -35,7 +54,11 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
val isLoginDestination =
navController.currentDestination?.id == R.id.loginFragment //to ensure that navigate call once
if (state.isSuccessfullyLogin && isLoginDestination) {
navController.navigate(LoginFragmentDirections.actionLoginToMain())
if (mustPopBackStack) {
navController.popBackStack()
} else {
navController.navigate(LoginFragmentDirections.actionLoginToMain())
}
}
binding.loginFragmentProgressBar.visibility =
if (state.isLoading) View.VISIBLE else View.GONE
Expand All @@ -60,10 +83,14 @@ class LoginFragment : Fragment(R.layout.fragment_login) {
viewModel.onPasswordInputChanged(it.toString())
}
binding.loginFragmentNoAccount.setOnClickListener {
navController.navigate(LoginFragmentDirections.actionLoginToRegister())
navController.navigate(LoginFragmentDirections.actionLoginToRegister(mustPopBackStack))
}
binding.loginFragmentSkipLayout.setOnClickListener {
navController.navigate(LoginFragmentDirections.actionLoginToMain())
if (mustPopBackStack) {
navController.popBackStack()
} else {
navController.navigate(LoginFragmentDirections.actionLoginToMain())
}
}

}
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/com/scholar/center/ui/auth/login/LoginVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.scholar.domain.model.LoginInputValidationType
import com.scholar.domain.model.LoginState
import com.scholar.domain.model.Resource
import com.scholar.domain.repo.AuthRepository
import com.scholar.domain.repo.DataStorePreference
import com.scholar.domain.usecase.ValidateLoginUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,6 +17,7 @@ import javax.inject.Inject

@HiltViewModel
class LoginVM @Inject constructor(
private val dataStore: DataStorePreference,
private val authRepository: AuthRepository,
private val validateLoginUseCase: ValidateLoginUseCase,
) : ViewModel() {
Expand Down Expand Up @@ -65,17 +67,21 @@ class LoginVM @Inject constructor(
)
when (result) {
is Resource.Success -> {
result.data?.let { user ->
// dataStoreRepository.saveUser(user)
result.data?.let { studentID ->
dataStore.saveValue(DataStorePreference.userId, studentID)
dataStore.saveValue(DataStorePreference.isUserLoggedIn, true)

loginState.update { currentState ->
currentState.copy(isSuccessfullyLogin = true,isLoading = false)
currentState.copy(isSuccessfullyLogin = true, isLoading = false)
}
}
}
is Resource.Error -> {
loginState.update { currentState ->
currentState.copy(errorMessageLoginProcess = result.message,isLoading = false)
currentState.copy(
errorMessageLoginProcess = result.message,
isLoading = false
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.scholar.center.ui.auth.register

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AlphaAnimation
import androidx.core.widget.addTextChangedListener
Expand All @@ -12,6 +13,7 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.scholar.center.R
import com.scholar.center.databinding.FragmentRegisterBinding
import com.scholar.center.unit.Constants
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

Expand All @@ -24,7 +26,7 @@ class RegisterFragment : Fragment(R.layout.fragment_register) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

binding = FragmentRegisterBinding.bind(view)

val mustPopBackStack = arguments?.getBoolean(Constants.MUST_POP_BACK_KEY) ?: false

val navController = findNavController()

Expand All @@ -33,7 +35,7 @@ class RegisterFragment : Fragment(R.layout.fragment_register) {
}

binding.registerFragmentLoginText.setOnClickListener {
navController.navigate(RegisterFragmentDirections.actionRegisterToLogin())
navController.navigate(RegisterFragmentDirections.actionRegisterToLogin(mustPopBackStack))
}

binding.registerFragmentFullNameEditText.addTextChangedListener {
Expand All @@ -54,8 +56,15 @@ class RegisterFragment : Fragment(R.layout.fragment_register) {
viewModel.registerState.collect { state ->
val isLoginDestination =
navController.currentDestination?.id == R.id.registerFragment //to ensure that navigate call once
Log.d("registerState", "isLoginDestination: $isLoginDestination")
Log.d("registerState", "state.isSuccessfullyRegister: ${state.isSuccessfullyRegister}")
Log.d("registerState", "mustPopBackStack: $mustPopBackStack")
if (state.isSuccessfullyRegister && isLoginDestination) {
navController.navigate(RegisterFragmentDirections.actionRegisterToMain())
if (mustPopBackStack) {
navController.popBackStack()
} else {
navController.navigate(RegisterFragmentDirections.actionRegisterToMain())
}
}
binding.registerFragmentProgressBar.visibility =
if (state.isLoading) View.VISIBLE else View.GONE
Expand All @@ -64,13 +73,9 @@ class RegisterFragment : Fragment(R.layout.fragment_register) {
state.errorMessageInput?.let { error ->
binding.registerFragmentErrorText.text = getString(error)
}

}
}
}



}

private fun showTextError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.scholar.domain.model.RegisterInputValidationType
import com.scholar.domain.model.RegisterState
import com.scholar.domain.model.Resource
import com.scholar.domain.repo.AuthRepository
import com.scholar.domain.repo.DataStorePreference
import com.scholar.domain.usecase.ValidateRegisterUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -18,6 +19,7 @@ import javax.inject.Inject
class RegisterVM @Inject constructor(
private val validateRegisterUseCase: ValidateRegisterUseCase,
private val authRepository: AuthRepository,
private val dataStore: DataStorePreference,
) : ViewModel() {
var registerState = MutableStateFlow(RegisterState())
private set
Expand All @@ -29,6 +31,7 @@ class RegisterVM @Inject constructor(
}
checkInputValidation()
}

fun onEmailInputChanged(newValue: String) {
registerState.update { currentState ->
currentState.copy(emailInput = newValue)
Expand Down Expand Up @@ -82,8 +85,9 @@ class RegisterVM @Inject constructor(
)
when (result) {
is Resource.Success -> {
result.data?.let { user ->
// dataStoreRepository.saveUser(user)
result.data?.let { studentID ->
dataStore.saveValue(DataStorePreference.userId, studentID)
dataStore.saveValue(DataStorePreference.isUserLoggedIn, true)
registerState.update { currentState ->
currentState.copy(isSuccessfullyRegister = true, isLoading = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.scholar.center.adapter.CardClassITemAdapter
import com.scholar.center.adapter.CategoryAdapter
import com.scholar.center.adapter.MaterialAdapter
import com.scholar.center.databinding.FragmentHomeBinding
import com.scholar.center.ui.MainFragmentDirections
import com.scholar.center.ui.main.MainFragmentDirections
import com.scholar.domain.model.Stage
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package com.scholar.center.ui
package com.scholar.center.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.scholar.center.R
import com.scholar.center.databinding.FragmentMainBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@AndroidEntryPoint
class MainFragment : Fragment(R.layout.fragment_main), NavController.OnDestinationChangedListener {

private lateinit var binding: FragmentMainBinding
private val viewModel: MainVM by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {
binding = FragmentMainBinding.inflate(inflater, container, false)

val nestedNavController = childFragmentManager.findFragmentById(R.id.main_nav_host_fragment)?.findNavController()
val nestedNavController =
childFragmentManager.findFragmentById(R.id.main_nav_host_fragment)?.findNavController()

nestedNavController?.let { navController ->
binding.mainNavBottom.setupWithNavController(navController)
Expand All @@ -34,22 +42,26 @@ class MainFragment : Fragment(R.layout.fragment_main), NavController.OnDestinati
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

}

override fun onDestinationChanged(
controller: NavController,
destination: NavDestination,
arguments: Bundle?,
) {

if(destination.id != R.id.homeFragment){
if (destination.id != R.id.homeFragment) {
binding.mainStudentName.visibility = View.GONE
}else{
} else {
binding.mainStudentName.visibility = View.VISIBLE
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
lifecycleScope.launch {
viewModel.studentName.collectLatest { name ->
binding.mainStudentName.text = getString(R.string.hi, name)
}
}
}


}
32 changes: 32 additions & 0 deletions app/src/main/java/com/scholar/center/ui/main/MainVM.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.scholar.center.ui.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.scholar.domain.repo.DataStorePreference
import com.scholar.domain.repo.StudentRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainVM @Inject constructor(
private val studentRepository: StudentRepository,
private val dataStore: DataStorePreference,
) : ViewModel() {

private val _studentName = MutableStateFlow<String?>(null)
val studentName = _studentName.asStateFlow()


init {
viewModelScope.launch {
val studentId = dataStore.readValue(DataStorePreference.userId).first()
studentId?.let {
_studentName.value = studentRepository.getStudentName(it)
}
}
}
}
Loading

0 comments on commit 2ed8b02

Please sign in to comment.