Skip to content

Instantly share code, notes, and snippets.

@NurseyitTursunkulov
Created November 12, 2020 07:24
Show Gist options
  • Save NurseyitTursunkulov/ff46c8e311806a5993d9db8a8a2b9cbe to your computer and use it in GitHub Desktop.
Save NurseyitTursunkulov/ff46c8e311806a5993d9db8a8a2b9cbe to your computer and use it in GitHub Desktop.
class MainViewModel(
val lonLatOfBishkek: LonLat,
private val app: Application,
val mainUseCase: MainUseCase,
private val loginUseCase: LoginUseCase,
presenterState: MainPresenterState,
globalNavigationState: GlobalNavigationState
) : AndroidViewModel(app), SuggestSession.SuggestListener,
MainPresenterState by presenterState,
GlobalNavigationState by globalNavigationState {
private val _currentPickedLocation = MutableLiveData<String>()
private val _pickupLocation = MutableLiveData<String>()
private val _destinationLocation = MutableLiveData<String>()
private val _bottomSheetExpanded = MutableLiveData<Boolean>().also { it.value = false }
private val _displayFragmentHidden = MutableLiveData<Boolean>().also { it.value = true }
private val _locationsPicked = MutableLiveData<Boolean>().also { it.value = false }
// private val _foundLocations = MutableLiveData<List<Item>>()
private val _moveMapToLonLat = MutableLiveData<LonLat>()
private val _estimation = MutableLiveData<Estimation>()
private val _inInitialState = MutableLiveData<Boolean>().also { it.value = true }
var currentIsPickupLocation: Boolean = true
var lastLonLat: LonLat = lonLatOfBishkek
var pickupLocationLonLat: LonLat? = LonLat(42.831698, 74.621554)
var destinationLocationLonLat: LonLat? = LonLat(42.8655, 74.5943)
private var mapIsPickable: Boolean = true
val phoneNumber: String get() = "0703188111"
val currentPickedLocation: LiveData<String> get() = _currentPickedLocation
val pickupLocation: LiveData<String> get() = _pickupLocation
val destinationLocation: LiveData<String> get() = _destinationLocation
val bottomSheetExpanded: LiveData<Boolean> get() = _bottomSheetExpanded
val locationDisplayFragmentHidden: LiveData<Boolean> get() = _displayFragmentHidden
val locationsPicked: LiveData<Boolean> get() = _locationsPicked
// val foundLocations: LiveData<List<Item>> get() = _foundLocations
// val order: LiveData<Order> get() = orderRepository.orderLiveData
val moveMapToLonLat: LiveData<LonLat> get() = _moveMapToLonLat
val estimation: LiveData<Estimation> get() = _estimation
val inInitialState: LiveData<Boolean> get() = _inInitialState
private fun setInInitialState(initialState: Boolean) {
if (_inInitialState.value != initialState) {
_inInitialState.value = initialState
}
}
fun setLocationsPicked(picked: Boolean) {
// if (_locationsPicked.value != picked) {
_locationsPicked.value = picked
// }
}
fun pickLocationOnMap() {
setInInitialState(false)
if (currentIsPickupLocation) {
pickupLocationLonLat?.let {
_moveMapToLonLat.value = it
lastLonLat = it
}
} else {
destinationLocationLonLat?.let {
_moveMapToLonLat.value = it
lastLonLat = it
}
}
closeBottomSheetFragment()
showDisplayFragment()
requestLocationNameByLonLat(lastLonLat)
}
init {
pickupLocationLonLat = LonLat(42.831694, 74.6214)
_pickupLocation.value = "Юнусалиева 41"
}
fun pickLocationOnList(item: SelectedPoint) {
if (currentIsPickupLocation) {
pickupLocationLonLat = LonLat(item.latitute, item.longtitute)
_pickupLocation.value = item.adressName
} else {
destinationLocationLonLat = LonLat(item.latitute, item.longtitute)
_destinationLocation.value = item.adressName
}
mapIsPickable = false
_moveMapToLonLat.value = LonLat(item.latitute, item.longtitute)
_currentPickedLocation.value = item.adressName
checkIfBothLocationsSelected()
closeBottomSheetFragment()
showDisplayFragment()
}
private fun checkIfBothLocationsSelected() {
if (pickupLocation.value != null && destinationLocation.value != null) {
_locationsPicked.value = true
}
}
fun openBottomSheetFragment() {
if (_bottomSheetExpanded.value != true) {
_bottomSheetExpanded.value = true
if (inInitialState.value != true) {
setInInitialState(true)
}
}
}
fun closeBottomSheetFragment() {
if (_bottomSheetExpanded.value != false) {
_bottomSheetExpanded.value = false
if (inInitialState.value == true && locationsPicked.value != true) {
currentIsPickupLocation = true
_currentPickedLocation.run {
value = value
}
destinationLocationLonLat = null
_destinationLocation.value = null
}
}
}
fun closeLocationFragment(){
_displayFragmentHidden.value = true
}
private val _hideOrderDetails = MutableLiveData<Boolean>()
val hideOrderDetails = _hideOrderDetails
fun hideOrderDetails() {
_hideOrderDetails.value = false
}
fun hideDisplayFragment() {
_displayFragmentHidden.value = true
}
fun showDisplayFragment() {
if (inInitialState.value != true) {
_displayFragmentHidden.value = false
}
}
fun requestLocationNameByLonLat(lonLat: LonLat = lastLonLat) {
if (!mapIsPickable) {
mapIsPickable = true
return
}
}
fun updateCurrentPickedLocation(name: String, lonLat: LonLat) {
_currentPickedLocation.value = name
val isPickUpLocation = currentIsPickupLocation
if (isPickUpLocation) {
_pickupLocation.value = "Юнусалиева 41"
pickupLocationLonLat = LonLat(42.831698, 74.621554)
} else {
_destinationLocation.value = name
destinationLocationLonLat = lonLat
}
}
fun searchPlacesByName(name: String) {
if (bottomSheetExpanded.value != true) return
requestSuggest(name)
}
fun requestEstimations() {
val pointA = pickupLocationLonLat
val pointB = destinationLocationLonLat
if (pointA == null || pointB == null) return
viewModelScope.launch {
withContext(Dispatchers.IO) {
mainUseCase.requestEstimations(pointA, pointB)
}
}
}
fun orderCourier(
senderPhone: String,
receiverPhone: String,
comment: String,
items: List<OrderItem>?
) {
val pointA = pickupLocationLonLat
val pointB = destinationLocationLonLat
if (pointA == null || pointB == null) return
val estimation = showEstimationsEvent.value?.peekContent() ?: Estimation()
viewModelScope.launch {
withContext(Dispatchers.IO) {
mainUseCase.createOrder(
NewOrder(
senderPhone = senderPhone,
receiverPhone = receiverPhone,
pointA = pickupLocation.value ?: "",
pickupLat = pointA.latitute,
pickupLong = pointA.longtitute,
pointB = destinationLocation.value ?: "",
destinationLat = pointB.latitute,
destinationLong = pointB.longtitute,
comment = comment,
estimatedDeliveryTime = estimation.time,
estimatedDeliveryPrice = estimation.cost,
estimatedDeliveryDistance = estimation.distance,
items = items ?: emptyList()
)
)
}
}
}
private val MAPKIT_API_KEY = "5e4b8c11-3756-4222-ae2d-062f9e631c4f"
private val RESULT_NUMBER_LIMIT = 5
private var searchManager: SearchManager? = null
private var suggestSession: SuggestSession? = null
private var suggestResult: MutableList<SuggestItem> = mutableListOf()
var resLive = MutableLiveData<MutableList<SuggestItem>>()
private val CENTER = Point(42.870049, 74.592873)
private val BOX_SIZE = 0.2
private val BOUNDING_BOX = BoundingBox(
Point(CENTER.latitude - BOX_SIZE, CENTER.longitude - BOX_SIZE),
Point(CENTER.latitude + BOX_SIZE, CENTER.longitude + BOX_SIZE)
)
private val SEARCH_OPTIONS = SuggestOptions().setSuggestTypes(
SuggestType.GEO.value or
SuggestType.BIZ.value or
SuggestType.TRANSIT.value
)
init {
MapKitFactory.setApiKey(MAPKIT_API_KEY)
SearchFactory.initialize(app)
SearchFactory.initialize(app)
searchManager = SearchFactory.getInstance().createSearchManager(SearchManagerType.COMBINED)
suggestSession = searchManager!!.createSuggestSession()
MapKitFactory.getInstance().onStart()
}
override fun onResponse(suggest: MutableList<SuggestItem>) {
suggestResult?.clear()
for (i in 0 until Math.min(RESULT_NUMBER_LIMIT, suggest.size)) {
suggest.get(i).let { suggestResult?.add(it) }
Log.d("Nurs", "" + suggest)
}
// _foundLocations.value = it.result.items.onlyWithLonLat
resLive.postValue(suggestResult)
}
override fun onError(p0: Error) {
Toast.makeText(app, p0.toString(), Toast.LENGTH_SHORT).show()
}
fun requestSuggest(query: String) {
suggestSession!!.suggest(query, BOUNDING_BOX, SEARCH_OPTIONS, this)
}
override fun onCleared() {
super.onCleared()
MapKitFactory.getInstance().onStop()
}
fun logout() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
loginUseCase.logout()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment