Skip to content

Commit 1a0e416

Browse files
authored
Merge pull request #3 from isoguzay/task/add-service-location-circular-progress
task/add-state-control-and-circular-progress
2 parents f0a5e2e + 2ce2a34 commit 1a0e416

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed

app/src/main/java/com/adyen/android/assignment/network/service/PlacesService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import retrofit2.http.GET
1010
import retrofit2.http.Headers
1111
import retrofit2.http.QueryMap
1212

13-
1413
interface PlacesService {
1514
/**
1615
* Get venue recommendations.

app/src/main/java/com/adyen/android/assignment/screens/home/view/HomeScreen.kt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
2626
import com.adyen.android.assignment.R
2727
import com.adyen.android.assignment.model.response.Category
2828
import com.adyen.android.assignment.model.response.Result
29-
import com.adyen.android.assignment.network.util.NetworkResult
3029
import com.adyen.android.assignment.screens.home.viewmodel.HomeViewModel
3130
import com.adyen.android.assignment.screens.permission.screen.LocationPermissionScreen
3231
import com.adyen.android.assignment.screens.permission.viewmodel.PermissionViewModel
@@ -36,7 +35,6 @@ import com.google.android.gms.maps.model.LatLng
3635
import com.google.maps.android.compose.GoogleMap
3736
import com.google.maps.android.compose.Marker
3837
import com.google.maps.android.compose.rememberCameraPositionState
39-
import timber.log.Timber
4038

4139
@RequiresApi(Build.VERSION_CODES.M)
4240
@Composable
@@ -45,11 +43,10 @@ fun HomeScreen(
4543
homeViewModel: HomeViewModel = hiltViewModel(),
4644
permissionViewModel: PermissionViewModel = hiltViewModel()
4745
) {
46+
val venuesListState = homeViewModel.venuesListState
4847

49-
val placesStatesList = mutableListOf<Result>()
48+
val showProgress = remember { mutableStateOf(false) }
5049
val showInfo = remember { mutableStateOf(false) }
51-
52-
val placesState = homeViewModel.places.observeAsState()
5350
val selectedPlaceState = homeViewModel.selectedPlace.observeAsState()
5451
val permissionsState = permissionViewModel.locationPermission.observeAsState()
5552
val currentLocation = homeViewModel.currentLocation.observeAsState()
@@ -67,26 +64,12 @@ fun HomeScreen(
6764
LocationPermissionScreen()
6865
}
6966

70-
when (val placesResponse = placesState.value) {
71-
is NetworkResult.Success -> {
72-
placesResponse.data?.results?.forEach { result ->
73-
placesStatesList.add(result)
74-
}
75-
}
76-
is NetworkResult.Failure -> {
77-
placesResponse.apply {
78-
Timber.e("$statusCode")
79-
}
80-
}
81-
else -> {}
82-
}
83-
84-
if (placesStatesList.isNotEmpty()) {
67+
if (venuesListState.venueList?.isNotEmpty() == true) {
8568
GoogleMap(
8669
modifier = Modifier.fillMaxSize(),
8770
cameraPositionState = cameraPositionState
8871
) {
89-
placesStatesList.forEach { place ->
72+
venuesListState.venueList.forEach { place ->
9073
place.geocodes?.main?.let { main ->
9174
Marker(
9275
position = LatLng(
@@ -103,7 +86,6 @@ fun HomeScreen(
10386
}
10487
}
10588
}
106-
10789
Box(
10890
modifier = Modifier
10991
.fillMaxWidth()
@@ -113,6 +95,7 @@ fun HomeScreen(
11395
Button(modifier = Modifier
11496
.wrapContentSize(),
11597
onClick = {
98+
showProgress.value = true
11699
locationRequestOnClick()
117100
}) {
118101
Icon(
@@ -123,13 +106,37 @@ fun HomeScreen(
123106
}
124107
}
125108
}
109+
Box(
110+
modifier = Modifier.fillMaxSize(),
111+
contentAlignment = Alignment.Center
112+
) {
113+
if (venuesListState.isLoading) {
114+
CircularProgressIndicator()
115+
} else if (venuesListState.error != null) {
116+
Text(
117+
text = venuesListState.error,
118+
color = MaterialTheme.colors.error
119+
)
120+
}
121+
}
126122

127123
if (currentLocation.value != null) {
128124
homeViewModel.getPlaces(currentLocation.value!!)
129125
cameraPositionState.position = CameraPosition.fromLatLngZoom(
130126
homeViewModel.getUserCurrentLocation(),
131127
GOOGLE_MAPS_CAMERA_ZOOM
132128
)
129+
showProgress.value = false
130+
homeViewModel.setCurrentLocationEmpty()
131+
}
132+
133+
if (showProgress.value) {
134+
Box(
135+
modifier = Modifier.fillMaxSize(),
136+
contentAlignment = Alignment.Center
137+
) {
138+
CircularProgressIndicator()
139+
}
133140
}
134141

135142
if (selectedPlaceState.value != null) {

app/src/main/java/com/adyen/android/assignment/screens/home/viewmodel/HomeViewModel.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.adyen.android.assignment.screens.home.viewmodel
22

3+
import androidx.compose.runtime.getValue
4+
import androidx.compose.runtime.mutableStateOf
5+
import androidx.compose.runtime.setValue
36
import androidx.lifecycle.LiveData
47
import androidx.lifecycle.MutableLiveData
58
import androidx.lifecycle.ViewModel
@@ -19,20 +22,37 @@ import javax.inject.Inject
1922
class HomeViewModel @Inject constructor(private val placesRepository: PlacesRepository) :
2023
ViewModel() {
2124

22-
private val mGetPlaces = MutableLiveData<NetworkResult<PlacesReponse>>()
23-
val places: LiveData<NetworkResult<PlacesReponse>>
24-
get() = mGetPlaces
25+
var venuesListState by mutableStateOf(VenuesListState())
2526

2627
private val mSelectedPlace = MutableLiveData<Result>()
2728
val selectedPlace: LiveData<Result>
2829
get() = mSelectedPlace
2930

30-
private val mCurrentLocation = MutableLiveData<LocationRequestModel>()
31-
val currentLocation: LiveData<LocationRequestModel>
31+
private val mCurrentLocation = MutableLiveData<LocationRequestModel?>()
32+
val currentLocation: LiveData<LocationRequestModel?>
3233
get() = mCurrentLocation
3334

3435
fun getPlaces(requestModel: LocationRequestModel) = viewModelScope.launch {
35-
mGetPlaces.value = placesRepository.getPlaces(requestModel = requestModel)
36+
venuesListState = venuesListState.copy(isLoading = true)
37+
when (val placesResponse = placesRepository.getPlaces(requestModel = requestModel)) {
38+
is NetworkResult.Success -> {
39+
placesResponse.data?.results?.let { venueList ->
40+
venuesListState = venuesListState.copy(
41+
venueList = venueList,
42+
isLoading = false,
43+
error = null
44+
)
45+
}
46+
}
47+
is NetworkResult.Failure -> {
48+
venuesListState = venuesListState.copy(
49+
venueList = null,
50+
isLoading = false,
51+
error = placesResponse.statusCode.toString()
52+
)
53+
}
54+
else -> {}
55+
}
3656
}
3757

3858
fun setSelectedPlace(place: Result) {
@@ -47,6 +67,10 @@ class HomeViewModel @Inject constructor(private val placesRepository: PlacesRepo
4767
mCurrentLocation.value = locationRequestModel
4868
}
4969

70+
fun setCurrentLocationEmpty() {
71+
mCurrentLocation.value = null
72+
}
73+
5074
fun getDummyAmsterdamLocation(): LatLng {
5175
return LatLng(Constant.DUMMY_LOCATION_LAT, Constant.DUMMY_LOCATION_LON)
5276
}
@@ -58,4 +82,5 @@ class HomeViewModel @Inject constructor(private val placesRepository: PlacesRepo
5882
)
5983
}
6084

85+
6186
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.adyen.android.assignment.screens.home.viewmodel
2+
3+
import com.adyen.android.assignment.model.response.Result
4+
5+
data class VenuesListState(
6+
val venueList: List<Result>? = emptyList(),
7+
val isLoading: Boolean = false,
8+
val error: String? = null
9+
)

app/src/main/java/com/adyen/android/assignment/utils/Constant.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.adyen.android.assignment.utils
22

33
object Constant {
44

5-
const val GOOGLE_MAPS_CAMERA_ZOOM = 16f // float
5+
const val GOOGLE_MAPS_CAMERA_ZOOM = 18f // float
66
const val DUMMY_LOCATION_LAT = 52.37651 // double dummy location for Amsterdam
77
const val DUMMY_LOCATION_LON = 4.90589 // double dummy location for Amsterdam
88
const val REQUEST_FINE_LOCATION_PERMISSIONS_REQUEST_CODE = 34

0 commit comments

Comments
 (0)