Skip to content

Commit

Permalink
implement adding a waypoint to building and unit map markers
Browse files Browse the repository at this point in the history
  • Loading branch information
lmj0011 committed Dec 5, 2021
1 parent bd493ba commit 1c769ea
Show file tree
Hide file tree
Showing 18 changed files with 602 additions and 77 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ android {
applicationId = "name.lmj0011.courierlocker"
minSdk = 26
targetSdk = 31
versionCode = 103
versionCode = 105
versionName = "2.3.0-beta02"

vectorDrawables {
Expand Down
10 changes: 5 additions & 5 deletions app/deps.list.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# auto-generated; this file should be checked into version control
androidx.databinding:viewbinding:7.0.2
androidx.databinding:databinding-common:7.0.2
androidx.databinding:databinding-runtime:7.0.2
androidx.databinding:databinding-adapters:7.0.2
androidx.databinding:databinding-ktx:7.0.2
androidx.databinding:viewbinding:7.0.3
androidx.databinding:databinding-common:7.0.3
androidx.databinding:databinding-runtime:7.0.3
androidx.databinding:databinding-adapters:7.0.3
androidx.databinding:databinding-ktx:7.0.3
org.jetbrains.kotlin:kotlin-parcelize-runtime:1.5.10
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.10
androidx.navigation:navigation-fragment-ktx:2.3.5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package name.lmj0011.courierlocker.database


data class Building(val number: String, val latitude: Double, val longitude: Double)
data class Building(
var number: String = "",
var latitude: Double = 0.0,
var longitude: Double = 0.0,
var hasWaypoint: Boolean = false,
var waypointLatitude: Double = 0.0,
var waypointLongitude: Double = 0.0
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package name.lmj0011.courierlocker.database


data class BuildingUnit(var number: String, var floorNumber: String, val latitude: Double, val longitude: Double)
data class BuildingUnit(
var number: String = "",
var floorNumber: String = "",
val latitude: Double = 0.0,
val longitude: Double = 0.0,
var hasWaypoint: Boolean = false,
var waypointLatitude: Double = 0.0,
var waypointLongitude: Double = 0.0
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ import name.lmj0011.courierlocker.databinding.FragmentCreateOrEditApartmentMapBi
import name.lmj0011.courierlocker.databinding.FragmentEditAptBuildingsMapBinding
import name.lmj0011.courierlocker.factories.ApartmentViewModelFactory
import name.lmj0011.courierlocker.fragments.bottomsheets.BottomSheetAptBuildingDetailsFragment
import name.lmj0011.courierlocker.helpers.AptBldgClusterItem
import name.lmj0011.courierlocker.helpers.PreferenceHelper
import name.lmj0011.courierlocker.helpers.launchUI
import name.lmj0011.courierlocker.helpers.*
import name.lmj0011.courierlocker.helpers.Util.stylePolyline
import name.lmj0011.courierlocker.viewmodels.ApartmentViewModel
import org.kodein.di.instance
import java.lang.Exception
Expand All @@ -58,8 +57,10 @@ class EditAptBuildingsMapFragment : Fragment(){
private lateinit var args: EditAptBuildingsMapFragmentArgs
private var selectedApt = MutableLiveData<Apartment>()
private var selectedBldg: Building? = null
private var activePolylinePairList: MutableList<Pair<Polyline, Circle>?> = mutableListOf()
private var fragmentJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + fragmentJob)
private var selectionMode = Const.BUILDING_SELECTION_MODE

@SuppressLint("MissingPermission")
override fun onCreateView(
Expand Down Expand Up @@ -137,28 +138,74 @@ class EditAptBuildingsMapFragment : Fragment(){
gMap.setOnMarkerClickListener(clusterManager)

gMap.setOnMapClickListener {
selectionMode = Const.BUILDING_SELECTION_MODE // default mode

this.hideAddBuildingUI()
this.hideAddWaypointUI()
}

gMap.setOnMapLongClickListener {
pinDropMarker.position = it
pinDropMarker.isVisible = true
selectedBldg = Building("", it.latitude, it.longitude)
this.showAddBuildingUI()
clearPolylines()

when(selectionMode) {
Const.BUILDING_SELECTION_MODE -> {
selectedBldg = Building("", it.latitude, it.longitude)
this.showAddBuildingUI()
}
Const.WAYPOINT_SELECTION_MODE -> {
this.showAddWaypointUI()
binding.saveWaypointButton.isEnabled = true

selectedBldg?.let { bldg ->
bldg.waypointLatitude = it.latitude
bldg.waypointLongitude = it.longitude
drawBldgPolyline(bldg)
}
}
}
}

clusterManager.setOnClusterItemClickListener {
selectedBldg = it.bldg
pinDropMarker.isVisible = false

val bottomSheet = BottomSheetAptBuildingDetailsFragment(it.bldg) {
val bottomSheet = BottomSheetAptBuildingDetailsFragment(
building = it.bldg,
addWaypointCallback = {
selectionMode = Const.WAYPOINT_SELECTION_MODE
this.showAddWaypointUI()
binding.saveWaypointButton.isEnabled = false
},
removeWaypointCallback = {
val oldBldg = selectedBldg!!

val newBldg = Building(
number = oldBldg.number,
latitude = oldBldg.latitude,
longitude = oldBldg.longitude,
hasWaypoint = false,
)

selectedApt.value?.let { apt ->
apt.buildings.remove(oldBldg)
apt.buildings.add(newBldg)

launchIO {
apartmentViewModel.updateApartment(apt)
selectedApt.postValue(apt)
}
}
},
removeBuildingCallback = {
val builder = MaterialAlertDialogBuilder(requireContext())

builder
.setTitle("Building ${it.bldg.number}")
.setMessage("Remove this building?")
.setPositiveButton("Yes") { _, _ ->
selectedBldg?.let{ selectedApt.value!!.buildings.remove(it) }
selectedBldg?.let{ bldg -> selectedApt.value!!.buildings.remove(bldg) }
launchUI {
withContext(Dispatchers.IO) {
apartmentViewModel.updateApartment(selectedApt.value)
Expand All @@ -171,7 +218,13 @@ class EditAptBuildingsMapFragment : Fragment(){
}

builder.show()
}
},
dismissCallback = {
clearPolylines()
}
)

if (it.bldg.hasWaypoint) drawBldgPolyline(it.bldg)

bottomSheet
.show(childFragmentManager, "BottomSheetAptBuildingDetailsFragment")
Expand Down Expand Up @@ -205,25 +258,47 @@ class EditAptBuildingsMapFragment : Fragment(){
}

binding.addButton.setOnClickListener {
uiScope.launch {
val b = Building(
binding.buildingEditText.text.toString(),
pinDropMarker.position.latitude,
pinDropMarker.position.longitude
)
val b = Building(
binding.buildingEditText.text.toString(),
pinDropMarker.position.latitude,
pinDropMarker.position.longitude
)

selectedBldg = b
selectedBldg = b

selectedApt.value?.let { apt ->
apt.buildings.add(b)
selectedApt.value?.let { apt ->
apt.buildings.add(b)

withContext(Dispatchers.IO) {
apartmentViewModel.updateApartment(apt)
selectedApt.postValue(apt)
}
launchIO {
apartmentViewModel.updateApartment(apt)
selectedApt.postValue(apt)
}
}

mainActivity.hideKeyBoard(binding.buildingEditText)
}

binding.saveWaypointButton.setOnClickListener {
val oldBldg = selectedBldg!!

val newBldg = Building(
number = oldBldg.number,
latitude = oldBldg.latitude,
longitude = oldBldg.longitude,
hasWaypoint = true,
waypointLatitude = pinDropMarker.position.latitude,
waypointLongitude = pinDropMarker.position.longitude

mainActivity.hideKeyBoard(binding.buildingEditText)
)

selectedApt.value?.let { apt ->
apt.buildings.remove(oldBldg)
apt.buildings.add(newBldg)

launchIO {
apartmentViewModel.updateApartment(apt)
selectedApt.postValue(apt)
}
}
}

Expand Down Expand Up @@ -328,6 +403,30 @@ class EditAptBuildingsMapFragment : Fragment(){
}
}
this.hideAddBuildingUI()
this.hideAddWaypointUI()
}

private fun drawBldgPolyline(bldg: Building) {
val polyline = gMap.addPolyline(
PolylineOptions()
.clickable(false)
.add(
LatLng(bldg.waypointLatitude, bldg.waypointLongitude),
LatLng(bldg.latitude, bldg.longitude)
)
)

val pair = stylePolyline(gMap, polyline)
activePolylinePairList.add(pair)
}

private fun clearPolylines() {
activePolylinePairList.forEach { pair ->
pair?.first?.remove()
pair?.second?.remove()
}

activePolylinePairList.clear()
}

private fun hideAddBuildingUI() {
Expand Down Expand Up @@ -362,5 +461,39 @@ class EditAptBuildingsMapFragment : Fragment(){
binding.buildingEditText.requestFocus()
mainActivity.showKeyBoard(binding.buildingEditText)
binding.buildingEditText.isEnabled = true

hideAddWaypointUI()
}

private fun hideAddWaypointUI() {
val transition = Slide(Gravity.TOP)
transition.duration = 50
transition.addTarget(binding.addWaypointInputView)
transition.addTarget(binding.saveWaypointButton)
transition.addTarget(binding.addWaypointTextView)

TransitionManager.beginDelayedTransition(binding.editAptBuildingsMapContainer, transition)
binding.addWaypointInputView.visibility = View.GONE
binding.saveWaypointButton.visibility = View.GONE
binding.addWaypointTextView.visibility = View.GONE

pinDropMarker.isVisible = false
clearPolylines()

}

private fun showAddWaypointUI() {
val transition = Slide(Gravity.TOP)
transition.duration = 50
transition.addTarget(binding.addWaypointInputView)
transition.addTarget(binding.saveWaypointButton)
transition.addTarget(binding.addWaypointTextView)

TransitionManager.beginDelayedTransition(binding.editAptBuildingsMapContainer, transition)
binding.addWaypointInputView.visibility = View.VISIBLE
binding.saveWaypointButton.visibility = View.VISIBLE
binding.addWaypointTextView.visibility = View.VISIBLE

hideAddBuildingUI()
}
}
Loading

0 comments on commit 1c769ea

Please sign in to comment.