diff --git a/app/src/main/java/com/samuelokello/kazihub/data/repository/KaziHubRepository.kt b/app/src/main/java/com/samuelokello/kazihub/data/repository/KaziHubRepository.kt index 807c67c..431d115 100644 --- a/app/src/main/java/com/samuelokello/kazihub/data/repository/KaziHubRepository.kt +++ b/app/src/main/java/com/samuelokello/kazihub/data/repository/KaziHubRepository.kt @@ -126,7 +126,7 @@ class KaziHubRepository } // not complete need to work on the request - suspend fun UpdateWorkerProfileImage(id: Int, request: WorkerProfileImageRequest): Resource = withContext(Dispatchers.IO) { + suspend fun updateWorkerProfileImage(id: Int, request: WorkerProfileImageRequest): Resource = withContext(Dispatchers.IO) { return@withContext try { val token = getAccessToken(context) val response =if (token != null) { diff --git a/app/src/main/java/com/samuelokello/kazihub/presentation/shared/components/LocationDropDown.kt b/app/src/main/java/com/samuelokello/kazihub/presentation/shared/components/LocationDropDown.kt index 7ed7823..46db518 100644 --- a/app/src/main/java/com/samuelokello/kazihub/presentation/shared/components/LocationDropDown.kt +++ b/app/src/main/java/com/samuelokello/kazihub/presentation/shared/components/LocationDropDown.kt @@ -1,7 +1,6 @@ package com.samuelokello.kazihub.presentation.shared.components -import android.os.Build -import androidx.annotation.RequiresExtension +import android.util.Log import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.shape.RoundedCornerShape @@ -12,41 +11,46 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp -import com.google.android.libraries.places.api.model.AutocompletePrediction +import com.samuelokello.kazihub.presentation.common.location.LocationViewModel -@RequiresExtension(extension = Build.VERSION_CODES.S, version = 7) @Composable fun LocationDropDown( + viewModel: LocationViewModel, // Use the interface as a parameter value: String, - onValueChange: (String, String) -> Unit, - locationSuggestions: List, - onLocationInputChanged: (String) -> Unit, + onValueChange: (String) -> Unit, label: String ) { + val locationSuggestions = viewModel.locationSuggestions // Get location suggestions from the ViewModel + Log.d("LocationDropDown", "Current number of location suggestions: ${locationSuggestions.size}") // Debug + Column { + // OutlinedTextField for location input OutlinedTextField( value = value, onValueChange = { - onValueChange(it, "") - onLocationInputChanged(it) + onValueChange(it) + viewModel.onLocationChange(it) }, label = { Text(label) }, shape = RoundedCornerShape(8.dp), modifier = Modifier.fillMaxWidth() ) + // DropdownMenu for location suggestions DropdownMenu( expanded = locationSuggestions.isNotEmpty(), - onDismissRequest = { onLocationInputChanged("") } + onDismissRequest = { + // Handle dropdown dismiss (optional) + } ) { locationSuggestions.forEach { suggestion -> DropdownMenuItem( - text = { Text(text = suggestion.getFullText(null).toString()) }, + text = { Text(text = suggestion.toString()) }, onClick = { - val newLocation = suggestion.getFullText(null).toString() - val placeId = suggestion.placeId - onValueChange(newLocation, placeId) - onLocationInputChanged("") + val newLocation = suggestion.toString() + + onValueChange(newLocation) + viewModel.onLocationChange(newLocation) }, modifier = Modifier.fillMaxWidth() ) @@ -54,51 +58,4 @@ fun LocationDropDown( } } } -//@Composable -//fun LocationDropDown( -// value: String, -// onValueChange: (String, String) -> Unit, -// places: PlacesClient, -// label: String -//) { -// var suggestions by remember { mutableStateOf(listOf())} -// -// LaunchedEffect(value) { -// if(value.isNotEmpty()) { -// val request = FindAutocompletePredictionsRequest.builder() -// .setQuery(value) -// .build() -// -// val response = places.findAutocompletePredictions(request).await() -// suggestions = response.autocompletePredictions -// } else { -// suggestions = emptyList() -// } -// } -// -// Column { -// OutlinedTextField( -// value = value, -// onValueChange = { onValueChange(it, "") }, -// label = { Text(label) }, -// shape = RoundedCornerShape(8.dp), -// modifier = Modifier.fillMaxWidth() -// ) -// -// DropdownMenu( -// expanded = suggestions.isNotEmpty(), -// onDismissRequest = {suggestions = listOf() } -// ) { -// suggestions.forEach { suggestion -> -// DropdownMenuItem( -// text = { Text(text = suggestion.getFullText(null).toString()) }, -// onClick = { -// val newLocation = suggestion.getFullText(null).toString() -// val placeId = suggestion.placeId -// onValueChange(newLocation, placeId) -// suggestions = listOf() -// }) -// } -// } -// } -//} \ No newline at end of file + diff --git a/app/src/main/java/com/samuelokello/kazihub/presentation/shared/home/presentation/HomeScreeen.kt b/app/src/main/java/com/samuelokello/kazihub/presentation/shared/home/presentation/HomeScreeen.kt index 8f70545..c6077b1 100644 --- a/app/src/main/java/com/samuelokello/kazihub/presentation/shared/home/presentation/HomeScreeen.kt +++ b/app/src/main/java/com/samuelokello/kazihub/presentation/shared/home/presentation/HomeScreeen.kt @@ -3,7 +3,7 @@ package com.samuelokello.kazihub.presentation.shared.home.presentation import androidx.compose.runtime.Composable import com.ramcosta.composedestinations.annotation.Destination import com.samuelokello.kazihub.presentation.business.BusinessHomeScreen -import com.samuelokello.kazihub.presentation.worker.WorkerHomeScreen +import com.samuelokello.kazihub.presentation.worker.ui.home.WorkerHomeScreen import com.samuelokello.kazihub.utils.UserRole /*** diff --git a/app/src/main/java/com/samuelokello/kazihub/presentation/shared/profile/CreateProfile.kt b/app/src/main/java/com/samuelokello/kazihub/presentation/shared/profile/CreateProfile.kt index 30c77f9..b0597dd 100644 --- a/app/src/main/java/com/samuelokello/kazihub/presentation/shared/profile/CreateProfile.kt +++ b/app/src/main/java/com/samuelokello/kazihub/presentation/shared/profile/CreateProfile.kt @@ -4,10 +4,9 @@ import android.os.Build import androidx.annotation.RequiresApi import androidx.compose.runtime.Composable import com.ramcosta.composedestinations.annotation.Destination -import com.ramcosta.composedestinations.annotation.RootNavGraph import com.ramcosta.composedestinations.navigation.DestinationsNavigator import com.samuelokello.kazihub.presentation.business.CreateBusinessProfile -import com.samuelokello.kazihub.presentation.worker.WorkerProfile +import com.samuelokello.kazihub.presentation.worker.ui.profile.CreateWorkerProfile import com.samuelokello.kazihub.utils.UserRole @@ -17,6 +16,6 @@ import com.samuelokello.kazihub.utils.UserRole fun CreateProfileScreen(userType: UserRole, navigator: DestinationsNavigator) { when(userType) { UserRole.BUSINESS -> CreateBusinessProfile(navigator,userType) - UserRole.WORKER -> WorkerProfile(navigator,userType) + UserRole.WORKER -> CreateWorkerProfile(navigator,userType) } } \ No newline at end of file diff --git a/app/src/main/java/com/samuelokello/kazihub/presentation/worker/CreateWorkerProfile.kt b/app/src/main/java/com/samuelokello/kazihub/presentation/worker/ui/profile/CreateWorkerProfile.kt similarity index 57% rename from app/src/main/java/com/samuelokello/kazihub/presentation/worker/CreateWorkerProfile.kt rename to app/src/main/java/com/samuelokello/kazihub/presentation/worker/ui/profile/CreateWorkerProfile.kt index fd4dc57..fe090bd 100644 --- a/app/src/main/java/com/samuelokello/kazihub/presentation/worker/CreateWorkerProfile.kt +++ b/app/src/main/java/com/samuelokello/kazihub/presentation/worker/ui/profile/CreateWorkerProfile.kt @@ -1,4 +1,4 @@ -package com.samuelokello.kazihub.presentation.worker +package com.samuelokello.kazihub.presentation.worker.ui.profile import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer @@ -7,8 +7,10 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.ui.Modifier import androidx.compose.ui.text.font.FontWeight @@ -16,36 +18,59 @@ import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel -import com.google.android.libraries.places.api.net.PlacesClient import com.ramcosta.composedestinations.navigation.DestinationsNavigator +import com.samuelokello.kazihub.presentation.common.HandleError +import com.samuelokello.kazihub.presentation.common.HandleLoading +import com.samuelokello.kazihub.presentation.common.HandleSuccess import com.samuelokello.kazihub.presentation.shared.components.CustomButton import com.samuelokello.kazihub.presentation.shared.components.EditTextField +import com.samuelokello.kazihub.presentation.shared.components.LocationDropDown +import com.samuelokello.kazihub.presentation.shared.destinations.HomeScreenDestination +import com.samuelokello.kazihub.presentation.worker.data.WorkerProfileViewModel +import com.samuelokello.kazihub.presentation.worker.state.WorkerEvent +import com.samuelokello.kazihub.presentation.worker.state.WorkerProfileState +import com.samuelokello.kazihub.ui.theme.KaziHubTheme import com.samuelokello.kazihub.utils.UserRole @Composable -fun WorkerProfile( +fun CreateWorkerProfile( navigator: DestinationsNavigator, userRole: UserRole ) { val viewModel: WorkerProfileViewModel = hiltViewModel() val state = viewModel.state.collectAsState().value val placesClient = viewModel.getPlacesClient() - - WorkerProfileForm( - state = state, - viewModel = viewModel, - placesClient = placesClient, - navigateToHome = { } - ) + Surface( + color = MaterialTheme.colorScheme.background, + modifier = Modifier.fillMaxSize() + ) { + KaziHubTheme { + WorkerProfileForm( + state = state, + viewModel = viewModel, + onEvent = viewModel::onEvent, + navigateToHome = { navigator.navigate(HomeScreenDestination(userRole)) }, + ) + } + } } @Composable fun WorkerProfileForm( state: WorkerProfileState, viewModel: WorkerProfileViewModel, - placesClient: PlacesClient, + onEvent: (WorkerEvent) -> Unit, navigateToHome: () -> Unit ) { + HandleLoading(state) + HandleError(state) + HandleSuccess(state = state, successMessage = state.error) + + LaunchedEffect(state.navigateToHome) { + if (state.navigateToHome) navigateToHome() + } + + Column( modifier = Modifier .fillMaxSize() @@ -63,7 +88,7 @@ fun WorkerProfileForm( Column { EditTextField( value = state.email, - onValueChange = { viewModel.onEmailChange(it) }, + onValueChange = { onEvent(WorkerEvent.OnEmailChanged(it)) }, label = "Email", singleLine = true, keyboardOptions = KeyboardOptions( @@ -74,7 +99,7 @@ fun WorkerProfileForm( Spacer(modifier = Modifier.height(16.dp)) EditTextField( value = state.phone, - onValueChange = { viewModel.onPhoneChange(it) }, + onValueChange = { onEvent(WorkerEvent.OnPhoneNumberChanged(it))}, label = "Phone", singleLine = true, keyboardOptions = KeyboardOptions( @@ -83,21 +108,18 @@ fun WorkerProfileForm( ) ) Spacer(modifier = Modifier.height(16.dp)) -// LocationDropDown( -// value = state.location, -// onValueChange = { newValue, input -> -// viewModel.onLocationChange(newValue) -// viewModel.onLocationInputChanged(input) -// } -// locationSuggestions = , -// onLocationInputChanged = , -// label = -// ) + + LocationDropDown( + viewModel = viewModel, + value = state.location, + onValueChange = { onEvent(WorkerEvent.OnLocationChanged(it))}, + label = "Location" + ) Spacer(modifier = Modifier.height(16.dp)) EditTextField( value = state.bio, - onValueChange = { viewModel.onBioChange(it) }, + onValueChange = { onEvent(WorkerEvent.OnBioChanged(it)) }, label = "Bio", singleLine = false, keyboardOptions = KeyboardOptions( @@ -109,7 +131,16 @@ fun WorkerProfileForm( Spacer(modifier = Modifier.weight(1.5f)) Column { CustomButton( - onClick = { navigateToHome() }, + onClick = { + onEvent( + WorkerEvent.OnCreateProfileClicked( + email = state.email, + phone = state.phone, + location = state.location, + bio = state.bio + ) + ) + }, text = "Create Profile", isEnabled = viewModel.isFormComplete(state.email, state.phone, state.location) )