Skip to content

Commit

Permalink
Map View UI
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaailashsharma committed Dec 10, 2023
1 parent 14620e5 commit a87c31f
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 110 deletions.
72 changes: 63 additions & 9 deletions app/src/main/java/ai/travel/app/mapsSearch/MapsSearchViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import ai.travel.app.home.ApiState
import ai.travel.app.home.TourDetails
import ai.travel.app.repository.ApiService
import android.app.Application
import android.widget.SearchView
import androidx.compose.runtime.mutableDoubleStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.input.TextFieldValue
Expand All @@ -20,12 +22,23 @@ import com.example.mapbox_map.Address
import com.example.mapbox_map.mapsSearch
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
Expand Down Expand Up @@ -60,27 +73,58 @@ class MapsSearchViewModel @Inject constructor(
val searchResponse: StateFlow<MapsSearchResponse?> = _searchResponse.asStateFlow()

var isClicked = mutableStateOf(false)
var latitude = mutableDoubleStateOf(20.5937)
var longitude =mutableDoubleStateOf(78.9629)


fun setImageState(state: ApiState) {
_imageState.value = state
}

@OptIn(FlowPreview::class)
@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class)
fun setQuery(query: TextFieldValue) {
_query.value = query
viewModelScope.launch {
_query.debounce(800).collectLatest {
if (_imageState.value !is ApiState.ReceivedPhoto) {
getAutoComplete(it.text)
_query.debounce(800)
.filter { query ->
if (query.text.isEmpty()) {
_query.value = TextFieldValue("")
return@filter false
} else {
return@filter true
}
}
}
.filter {
return@filter _imageState.value !is ApiState.ReceivedPhoto
}
.distinctUntilChanged()
.flatMapLatest { query ->
dataFromNetwork(query.text)
.catch {
emitAll(flowOf(""))
}
}
.flowOn(Dispatchers.Default)
.collect { result ->
if (result.isNotEmpty()) {
getAutoComplete(result)
}
}


// _query.debounce(800).collectLatest {
// if (_imageState.value !is ApiState.ReceivedPhoto) {
// getAutoComplete(it.text)
// }
// }
}
}

fun searchPlace(index: Int) {
try {
_address.value = _addresses.value[index]
latitude.value = _addresses.value[index].latitude
longitude.value = _addresses.value[index].longitude
_addresses.value = listOf()
getApiData()
} catch (e: Exception) {
Expand All @@ -98,6 +142,13 @@ class MapsSearchViewModel @Inject constructor(
}
}

private fun dataFromNetwork(query: String): Flow<String> {
return flow {
delay(2000)
emit(query)
}
}

private fun getApiData() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
Expand All @@ -108,7 +159,7 @@ class MapsSearchViewModel @Inject constructor(
)
)
_imageState.value = ApiState.ReceivedPlaceId
val photoIdData =
var photoIdData =
repository.getPhotoId(
photoId = apiData.places?.get(0)?.id ?: ""
)
Expand All @@ -119,10 +170,11 @@ class MapsSearchViewModel @Inject constructor(
repository.getPhoto(
photoReference = photoIdData.result?.photos?.get(i)?.photo_reference
?: "",
maxWidth = 1200,
maxWidth = 600,
)
_photoId.value.add(photoId)
}
_searchResponse.value?.photos?.clear()
_searchResponse.value = MapsSearchResponse(
places = photoIdData.result?.copy(
formattedAddress = address.value?.formattedAddress ?: "",
Expand All @@ -135,22 +187,24 @@ class MapsSearchViewModel @Inject constructor(
),
name = address.value?.name ?: "",
),
photos = photoId.value
photos = _photoId.value.toMutableList()
)
_imageState.value = ApiState.ReceivedPhoto
_query.value = TextFieldValue(searchResponse.value?.places?.name ?: "")
_addresses.value = listOf()
isClicked.value = true
_photoId.value.clear()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}

}

data class MapsSearchResponse(
val places: Result?,
val photos: List<ByteArray?>,
val photos: MutableList<ByteArray?>,
)

117 changes: 112 additions & 5 deletions app/src/main/java/ai/travel/app/mapsSearch/ui/MapsSearchBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
Expand All @@ -21,14 +22,22 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddReaction
import androidx.compose.material.icons.filled.ArrowBackIos
import androidx.compose.material.icons.filled.AutoAwesomeMotion
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Navigation
import androidx.compose.material.icons.filled.NorthEast
import androidx.compose.material.icons.filled.PhotoLibrary
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
Expand All @@ -43,13 +52,15 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand All @@ -62,10 +73,13 @@ fun MapsSearchBar(
onValueChange: (TextFieldValue) -> Unit,
onTrailingClick: () -> Unit = {},
viewModel: MapsSearchViewModel,
navController: NavController,
) {
var isChecking by remember { mutableStateOf(false) }
var isCheckingJob: Job? = null // Initialize isCheckingJob
val addresses = viewModel.addresses.collectAsState()
val imageState = viewModel.imageState.collectAsState()
val searchResponse = viewModel.searchResponse.collectAsState()
Column(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -74,13 +88,32 @@ fun MapsSearchBar(
alpha = 0.5f
)
),
horizontalAlignment = Alignment.CenterHorizontally
horizontalAlignment = Alignment.Start
) {
Spacer(modifier = Modifier.height(20.dp))
Row(
modifier = Modifier
.fillMaxWidth()
.padding(5.dp),
.padding(15.dp)
.clickable(
interactionSource = MutableInteractionSource(),
indication = null
) {
navController.popBackStack()
},
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.End
) {
Icon(
imageVector = Icons.Filled.ArrowBackIos,
contentDescription = "topText",
tint = textColor,
modifier = Modifier.size(30.dp)
)
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 5.dp),
verticalAlignment = Alignment.CenterVertically
) {
TextFieldWithIcons(
Expand Down Expand Up @@ -132,7 +165,7 @@ fun MapsSearchBar(
modifier = Modifier.fillMaxSize()
) {
LazyColumn(contentPadding = PaddingValues(5.dp)) {
itemsIndexed(addresses.value) {index, address ->
itemsIndexed(addresses.value) { index, address ->
Card(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -150,7 +183,9 @@ fun MapsSearchBar(
elevation = CardDefaults.cardElevation(0.dp)
) {
Row(
modifier = Modifier.fillMaxWidth().padding(10.dp),
modifier = Modifier
.fillMaxWidth()
.padding(10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Expand All @@ -172,5 +207,77 @@ fun MapsSearchBar(
}
}
}
AnimatedVisibility(
visible = imageState.value is ApiState.ReceivedPhoto,
enter = slideInVertically(initialOffsetY = {
it
}),
exit = slideOutVertically(targetOffsetY = {
it
})
) {
searchResponse.value?.let { response ->
Card(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.offset(y = (-10).dp),
shape = RoundedCornerShape(topStart = 10.dp, topEnd = 10.dp),
elevation = CardDefaults.cardElevation(7.dp),
colors = CardDefaults.cardColors(
containerColor = Color.Black.copy(0.8f),
),
border = BorderStroke(
width = 0.5.dp,
color = textColor
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 10.dp, vertical = 10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier,
) {
Icon(
imageVector = Icons.Filled.AddReaction,
contentDescription = "topText",
tint = lightText,
modifier = Modifier.size(30.dp)
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = "Generate Trip",
color = textColor,
fontSize = 15.sp,
)
}
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier,
) {
Icon(
imageVector = Icons.Filled.PhotoLibrary,
contentDescription = "topText",
tint = lightText,
modifier = Modifier
.size(30.dp)
.rotate(45f)
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = "Add Collection",
color = textColor,
fontSize = 15.sp,
)
}
}
}
}
}
}
}
Loading

0 comments on commit a87c31f

Please sign in to comment.