Skip to content

Commit

Permalink
Clean up view model
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Feb 24, 2023
1 parent d2e3ef2 commit 3202ac2
Showing 1 changed file with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ class SelectionMapFragment(
@Inject
lateinit var permissionsChecker: PermissionsChecker

private val selectedFeatureViewModel by viewModels<SelectedFeatureViewModel>()
private val selectedItemViewModel by viewModels<SelectedItemViewModel>()

private lateinit var map: MapFragment
private lateinit var summarySheetBehavior: BottomSheetBehavior<*>
private lateinit var summarySheet: SelectionSummarySheet
private lateinit var bottomSheetCallback: BottomSheetCallback

private val itemsByFeatureId: MutableMap<Int, MappableSelectItem> = mutableMapOf()
private val featureIdsByItemId: MutableMap<Long, Int> = mutableMapOf()

/**
* Points to be mapped. Note: kept separately from [.itemsByFeatureId] so we can
Expand Down Expand Up @@ -234,10 +235,10 @@ class SelectionMapFragment(

bottomSheetCallback = object : BottomSheetCallback() {
override fun onStateChanged(onStateChangedbottomSheet: View, newState: Int) {
val selectedItemId = selectedFeatureViewModel.getSelectedFeatureId()
if (newState == STATE_HIDDEN && selectedItemId != null) {
selectedFeatureViewModel.setSelectedFeatureId(null)
resetIcon(selectedItemId)
val selectedItem = selectedItemViewModel.getSelectedItem()
if (newState == STATE_HIDDEN && selectedItem != null) {
selectedItemViewModel.setSelectedItem(null)
resetIcon(selectedItem)

closeSummarySheet.isEnabled = false
} else {
Expand Down Expand Up @@ -265,13 +266,14 @@ class SelectionMapFragment(
}

private fun onFeatureClicked(featureId: Int, maintainZoom: Boolean = true) {
val selectedItemId = selectedFeatureViewModel.getSelectedFeatureId()
if (selectedItemId != null && selectedItemId != itemsByFeatureId[featureId]!!.id) {
resetIcon(selectedItemId)
}

val item = itemsByFeatureId[featureId]
val selectedItem = selectedItemViewModel.getSelectedItem()

if (item != null) {
if (selectedItem != null && selectedItem.id != item.id) {
resetIcon(selectedItem)
}

if (!skipSummary) {
if (item.points.size > 1) {
map.zoomToBoundingBox(item.points, 0.8, true)
Expand Down Expand Up @@ -302,7 +304,7 @@ class SelectionMapFragment(
}
)

selectedFeatureViewModel.setSelectedFeatureId(item.id)
selectedItemViewModel.setSelectedItem(item)
} else {
parentFragmentManager.setFragmentResult(
REQUEST_SELECT_ITEM,
Expand All @@ -327,14 +329,13 @@ class SelectionMapFragment(

val previouslySelectedItem =
itemsByFeatureId.filter { it.value.selected }.map { it.key }.firstOrNull()
val selectedItemId = selectedFeatureViewModel.getSelectedFeatureId()

if (selectedItemId != null) {
val (featureId, _) = itemsByFeatureId.entries.find {
it.value.id == selectedItemId
}!!
val selectedItem = selectedItemViewModel.getSelectedItem()

onFeatureClicked(featureId)
if (selectedItem != null) {
val featureId = featureIdsByItemId[selectedItem.id]
if (featureId != null) {
onFeatureClicked(featureId)
}
} else if (previouslySelectedItem != null) {
onFeatureClicked(previouslySelectedItem, maintainZoom = false)
} else if (!map.hasCenter()) {
Expand All @@ -349,15 +350,14 @@ class SelectionMapFragment(
}
}

private fun resetIcon(selectedItemId: Long) {
val (featureId, item) = itemsByFeatureId.entries.find {
it.value.id == selectedItemId
}!!

map.setMarkerIcon(
featureId,
MarkerIconDescription(item.smallIcon, item.color, item.symbol)
)
private fun resetIcon(selectedItem: MappableSelectItem) {
val featureId = featureIdsByItemId[selectedItem.id]
if (featureId != null) {
map.setMarkerIcon(
featureId,
MarkerIconDescription(selectedItem.smallIcon, selectedItem.color, selectedItem.symbol)
)
}
}

/**
Expand Down Expand Up @@ -389,6 +389,7 @@ class SelectionMapFragment(

(singlePoints + traces).zip(pointIds + traceIds).forEach { (item, featureId) ->
itemsByFeatureId[featureId] = item
featureIdsByItemId[item.id] = featureId
points.addAll(item.points)
}

Expand All @@ -402,16 +403,16 @@ class SelectionMapFragment(
}
}

internal class SelectedFeatureViewModel : ViewModel() {
internal class SelectedItemViewModel : ViewModel() {

private var selectedItemId: Long? = null
private var selectedItem: MappableSelectItem? = null

fun getSelectedFeatureId(): Long? {
return selectedItemId
fun getSelectedItem(): MappableSelectItem? {
return selectedItem
}

fun setSelectedFeatureId(itemId: Long?) {
selectedItemId = itemId
fun setSelectedItem(item: MappableSelectItem?) {
selectedItem = item
}
}

Expand Down

0 comments on commit 3202ac2

Please sign in to comment.