Skip to content

Commit

Permalink
enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
OkelloSam21 committed Apr 20, 2024
1 parent 6d34add commit a5bb57f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class KaziHubRepository
}

// not complete need to work on the request
suspend fun UpdateWorkerProfileImage(id: Int, request: WorkerProfileImageRequest): Resource<WorkerProfileImageResponse> = withContext(Dispatchers.IO) {
suspend fun updateWorkerProfileImage(id: Int, request: WorkerProfileImageRequest): Resource<WorkerProfileImageResponse> = withContext(Dispatchers.IO) {
return@withContext try {
val token = getAccessToken(context)
val response =if (token != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,93 +11,51 @@ 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<AutocompletePrediction>,
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()
)
}
}
}
}
//@Composable
//fun LocationDropDown(
// value: String,
// onValueChange: (String, String) -> Unit,
// places: PlacesClient,
// label: String
//) {
// var suggestions by remember { mutableStateOf(listOf<AutocompletePrediction>())}
//
// 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()
// })
// }
// }
// }
//}

Original file line number Diff line number Diff line change
Expand Up @@ -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

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,45 +7,70 @@ 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
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()
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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)
)
Expand Down

0 comments on commit a5bb57f

Please sign in to comment.