Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
anrouxel committed Feb 28, 2024
2 parents 7016d2f + 88759b6 commit e288308
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ interface ApiServiceGuewen {

@GET("get_all_med")
fun getTotalMedications(): Call<String>

@GET("get_doc/id/{id}")
fun getDocById(
@Path("id") id: Long
): Call<String>

@GET("get_doc/onlyName/{name}")
fun getLittleDocByNom(
@Path("name") name: String
): Call<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,54 @@ import java.time.LocalDate

class DoctorsSearch {

fun searchDoctor(doctor: String, callback: (List<Doctor>) -> Unit) {
fun searchLittleDoctor(doctor: String, callback: (List<Doctor>) -> Unit) {
val apiService = APIAddressClient().apiServiceGuewen
val call = apiService.getLittleDocByNom(doctor)

call.enqueue(object : Callback<String> {
override fun onResponse(call: Call<String>, response: Response<String>) {
if (response.isSuccessful) {
val allDocsJsonArray = response.body()!!
Log.d("Docteurs", "Les docteurs sont trouvés !")

val gson = GsonBuilder()
.registerTypeAdapter(LocalDate::class.java, LocalDateTypeAdapter())
.create()

val doctorListType: Type = object : TypeToken<List<Doctor>>() {}.type
val doctors: List<Doctor> = gson.fromJson(allDocsJsonArray, doctorListType)

Log.d("Docteurs", "Les docteurs sont trouvés ! : ${doctors.size}")
// Callback on the main thread
return callback(doctors)
} else {
Log.d("Docteurs", "Les docteurs ne sont pas trouvés !")
// Callback on the main thread
return callback(emptyList())
}
}

override fun onFailure(call: Call<String>, t: Throwable) {
Log.e("GuegueApi", "Error during API call: ${t.message}")
if (t.message == "timeout") {
Log.d("Gueguemagouille", "je relance la requête")
Handler(Looper.getMainLooper()).post {
// Relancer la requête avec les mêmes paramètres
searchLittleDoctor(doctor, callback)
}
} else {
return callback(emptyList())
}
}
})
}

fun searchDoctor(doctor: Long, callback: (List<Doctor>) -> Unit) {

cancelSearch()

val apiService = APIAddressClient().apiServiceGuewen
val call = apiService.getDocByNom(doctor)
val call = apiService.getDocById(doctor)

currentCall = call

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ fun NavGraphBuilder.doctorNavGraph(
DoctorHome(
onDoctorClick = {
navController.navigate(
/*
PrescriptionRoute.PrescriptionDetailRoute.route.replace(
DoctorRoute.DoctorDetailRoute.route.replace(
"{id}",
it.toString()
)
*/
DoctorRoute.DoctorDetailRoute.route
)
}
)
}

composable(route = DoctorRoute.DoctorDetailRoute.route) {
val doctor = it.arguments?.getString("id")?.toLongOrNull()

val viewModel =
it.sharedViewModel<SharedDoctorDetailViewModel>(navController = navController)

if (doctor != null) {
viewModel.loadDoctor(doctor)
}

DoctorDetail(
viewModel = viewModel
)
Expand All @@ -66,7 +69,7 @@ sealed class DoctorRoute(val route: String) {
/**
* Route pour afficher une prescription spécifique.
*/
object DoctorDetailRoute : DoctorRoute(route = "doctor_detail")
object DoctorDetailRoute : DoctorRoute(route = "doctor_detail/{id}")

/**
* Route pour ajouter une nouvelle prescription.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import fr.medicapp.medicapp.viewModel.DoctorViewModel
@Composable
fun DoctorHome(
viewModel: DoctorViewModel = viewModel(),
onDoctorClick: (Doctor) -> Unit
onDoctorClick: (Long) -> Unit
) {
val doctor = remember { mutableStateOf("") }
val doctors by viewModel.doctors.observeAsState(emptyList())
Expand All @@ -47,7 +47,7 @@ fun DoctorHome(
doctor.value = it
if (it.length > 3) {
isLoading.value = true
viewModel.searchDoctor(it) {
viewModel.searchLittleDoctor(it) {
isLoading.value = false
}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ fun DoctorHome(
@Composable
fun DoctorList(
doctors: List<Doctor>,
onDoctorClick: (Doctor) -> Unit
onDoctorClick: (Long) -> Unit
) {
LazyColumn {
items(doctors) { doctor ->
Expand All @@ -94,10 +94,10 @@ fun DoctorList(
@Composable
fun DoctorItem(
doctor: Doctor,
onDoctorClick: (Doctor) -> Unit
onDoctorClick: (Long) -> Unit
) {
ReusableElevatedCardButton(
onClick = { onDoctorClick(doctor) }
onClick = {onDoctorClick(doctor.nationalId) }
) {
CardContent(
title = "${doctor.civilCodeEx} ${doctor.firstName} ${doctor.lastName}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ class DoctorViewModel : ViewModel() {
private val _doctors = MutableLiveData<List<Doctor>>()
val doctors: LiveData<List<Doctor>> get() = _doctors

fun searchDoctor(query: String, callback: (List<Doctor>) -> Unit) {
fun searchDoctor(query: Long, callback: (List<Doctor>) -> Unit){
viewModelScope.launch {
val searchResults = DoctorsSearch().searchDoctor(query) { doctors ->
_doctors.postValue(doctors)
callback(doctors)
}
}
}

fun searchLittleDoctor(query: String, callback: (List<Doctor>) -> Unit){
viewModelScope.launch {
val searchResults = DoctorsSearch().searchLittleDoctor(query) { doctors ->
_doctors.postValue(doctors)
callback(doctors)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.mapbox.mapboxsdk.geometry.LatLng
import fr.medicapp.medicapp.api.address.APIAddressClient
import fr.medicapp.medicapp.api.address.apiInteractions.DoctorsSearch
import fr.medicapp.medicapp.model.prescription.Doctor
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -27,6 +28,12 @@ class SharedDoctorDetailViewModel(
)
val sharedState: StateFlow<Doctor> = _sharedState

fun loadDoctor(id : Long) {
DoctorsSearch().searchDoctor(id) {
_sharedState.value = it.first()
}
}

fun fetch(): LatLng {
return runBlocking {
APIAddressClient().apiService.getPosition(
Expand Down

0 comments on commit e288308

Please sign in to comment.