Skip to content

Commit 290d609

Browse files
committed
Marker editor to DialogFragment
1 parent 61666d4 commit 290d609

4 files changed

Lines changed: 174 additions & 100 deletions

File tree

‎app/src/main/java/org/nitri/opentopo/MapFragment.kt‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.nitri.opentopo.util.MapOrientation
5757
import org.nitri.opentopo.util.OrientationSensor
5858
import org.nitri.opentopo.util.Utils
5959
import org.nitri.opentopo.view.AboutDialog
60+
import org.nitri.opentopo.view.MarkerEditorDialog
6061
import org.nitri.opentopo.viewmodel.LocationViewModel
6162
import org.nitri.opentopo.viewmodel.MarkerViewModel
6263
import org.nitri.ors.OrsClient
@@ -319,7 +320,7 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
319320
locationOverlay?.isOptionsMenuEnabled = true
320321
compassOverlay?.enableCompass()
321322
mapView.visibility = View.VISIBLE
322-
overlayHelper = OverlayHelper(hostActivity, mapView)
323+
overlayHelper = OverlayHelper(hostActivity, mapView, parentFragmentManager)
323324
setTilesOverlay()
324325
}
325326

@@ -328,6 +329,30 @@ class MapFragment : Fragment(), LocationListener, PopupMenu.OnMenuItemClickListe
328329
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
329330
super.onViewCreated(view, savedInstanceState)
330331

332+
parentFragmentManager.setFragmentResultListener(
333+
MarkerEditorDialog.RESULT_REQUEST_KEY,
334+
viewLifecycleOwner
335+
) { _, result ->
336+
val markerId = result.getInt(MarkerEditorDialog.RESULT_MARKER_ID)
337+
when (result.getString(MarkerEditorDialog.RESULT_ACTION)) {
338+
MarkerEditorDialog.RESULT_ACTION_UPDATE -> {
339+
val currentMarker = markerViewModel.markers.value
340+
?.firstOrNull { it.id == markerId }
341+
?: return@setFragmentResultListener
342+
343+
val updated = currentMarker.copy(
344+
name = result.getString(MarkerEditorDialog.RESULT_NAME).orEmpty(),
345+
description = result.getString(MarkerEditorDialog.RESULT_DESCRIPTION).orEmpty()
346+
)
347+
markerViewModel.updateMarker(updated)
348+
}
349+
350+
MarkerEditorDialog.RESULT_ACTION_DELETE -> {
351+
markerViewModel.removeMarker(markerId)
352+
}
353+
}
354+
}
355+
331356
viewLifecycleOwner.lifecycleScope.launch {
332357
if (!mapViewInitialized) {
333358
initializeMapView()

‎app/src/main/java/org/nitri/opentopo/MarkerListActivity.kt‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import android.net.Uri
44
import android.os.Bundle
55
import android.view.Window
66
import android.widget.Toast
7-
import androidx.activity.ComponentActivity
87
import androidx.activity.compose.BackHandler
98
import androidx.activity.compose.setContent
109
import androidx.activity.result.contract.ActivityResultContracts
1110
import androidx.activity.viewModels
11+
import androidx.appcompat.app.AppCompatActivity
1212
import androidx.compose.foundation.ExperimentalFoundationApi
1313
import androidx.compose.foundation.combinedClickable
1414
import androidx.compose.foundation.layout.Box
@@ -62,7 +62,7 @@ import org.nitri.opentopo.view.MarkerEditorDialog
6262
import org.nitri.opentopo.viewmodel.MarkerViewModel
6363
import java.util.Locale
6464

65-
class MarkerListActivity : ComponentActivity() {
65+
class MarkerListActivity : AppCompatActivity() {
6666
private val markerViewModel: MarkerViewModel by viewModels()
6767

6868
private var idsToExport: Set<Int> = emptySet()
@@ -86,6 +86,30 @@ class MarkerListActivity : ComponentActivity() {
8686
MarkerListActivity::class.java.simpleName
8787
)
8888

89+
supportFragmentManager.setFragmentResultListener(
90+
MarkerEditorDialog.RESULT_REQUEST_KEY,
91+
this
92+
) { _, result ->
93+
val markerId = result.getInt(MarkerEditorDialog.RESULT_MARKER_ID)
94+
when (result.getString(MarkerEditorDialog.RESULT_ACTION)) {
95+
MarkerEditorDialog.RESULT_ACTION_UPDATE -> {
96+
val currentMarker = markerViewModel.markers.value
97+
?.firstOrNull { it.id == markerId }
98+
?: return@setFragmentResultListener
99+
100+
val updated = currentMarker.copy(
101+
name = result.getString(MarkerEditorDialog.RESULT_NAME).orEmpty(),
102+
description = result.getString(MarkerEditorDialog.RESULT_DESCRIPTION).orEmpty()
103+
)
104+
markerViewModel.updateMarker(updated)
105+
}
106+
MarkerEditorDialog.RESULT_ACTION_DELETE -> {
107+
markerViewModel.removeMarker(markerId)
108+
AnalyticsProvider.get(this).trackMarkersDeleted(1)
109+
}
110+
}
111+
}
112+
89113
setContent {
90114
OpenTopoTheme(dynamicColor = false) {
91115
MarkerListScreen(
@@ -106,15 +130,11 @@ class MarkerListActivity : ComponentActivity() {
106130
}
107131

108132
private fun openMarkerEditor(marker: MarkerModel) {
109-
MarkerEditorDialog.show(
110-
context = this,
111-
markerModel = marker.copy(),
112-
onUpdate = { updated -> markerViewModel.updateMarker(updated) },
113-
onDelete = { toDelete ->
114-
markerViewModel.removeMarker(toDelete.id)
115-
AnalyticsProvider.get(this).trackMarkersDeleted(1)
116-
}
117-
)
133+
if (supportFragmentManager.findFragmentByTag(MarkerEditorDialog.TAG) != null) {
134+
return
135+
}
136+
MarkerEditorDialog.newInstance(marker.id, marker.name, marker.description)
137+
.show(supportFragmentManager, MarkerEditorDialog.TAG)
118138
}
119139

120140
private fun showDeleteSelectedConfirmation(selectedIds: Set<Int>) {

‎app/src/main/java/org/nitri/opentopo/overlay/OverlayHelper.kt‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import android.graphics.ColorMatrix
66
import android.graphics.ColorMatrixColorFilter
77
import androidx.core.content.ContextCompat
88
import androidx.preference.PreferenceManager
9+
import androidx.fragment.app.FragmentManager
910
import io.ticofab.androidgpxparser.parser.domain.Gpx
1011
import org.nitri.opentopo.R
1112
import org.nitri.opentopo.SettingsActivity.Companion.PREF_ORS_API_KEY
1213
import org.nitri.opentopo.viewmodel.GpxViewModel
1314
import org.nitri.opentopo.model.MarkerModel
1415
import org.nitri.opentopo.nearby.entity.NearbyItem
1516
import org.osmdroid.tileprovider.MapTileProviderBasic
16-
import org.nitri.opentopo.util.Utils
1717
import org.nitri.opentopo.view.MarkerEditorDialog
1818
import org.osmdroid.tileprovider.tilesource.ITileSource
1919
import org.osmdroid.tileprovider.tilesource.XYTileSource
@@ -28,13 +28,16 @@ import org.osmdroid.views.overlay.TilesOverlay
2828
import org.osmdroid.views.overlay.FolderOverlay
2929
import org.osmdroid.bonuspack.kml.KmlDocument
3030

31-
class OverlayHelper(private val mContext: Context, private val mMapView: MapView?) {
31+
class OverlayHelper(
32+
private val mContext: Context,
33+
private val mMapView: MapView?,
34+
private val fragmentManager: FragmentManager? = null
35+
) {
3236
private var wayPointOverlay: ItemizedIconInfoOverlay? = null
3337
private var nearbyItemOverlay: ItemizedIconInfoOverlay? = null
3438
private var overlayType = OVERLAY_NONE
3539
private var overlayTileProvider: MapTileProviderBasic? = null
3640
private var tilesOverlay: TilesOverlay? = null
37-
private var gpxViewModel: GpxViewModel? = null
3841
private val tileOverlayAlphaMatrix = ColorMatrix(
3942
floatArrayOf(
4043
1f, 0f, 0f, 0f, 0f,
@@ -113,17 +116,16 @@ class OverlayHelper(private val mContext: Context, private val mMapView: MapView
113116
private val onMarkerInfoEditClickListener : MarkerInfoWindow.OnMarkerInfoEditClickListener =
114117
object : MarkerInfoWindow.OnMarkerInfoEditClickListener {
115118
override fun onMarkerInfoEditClick(markerModel: MarkerModel) {
116-
MarkerEditorDialog.show(
117-
context = mContext,
118-
markerModel = markerModel,
119-
onUpdate = { markerInteractionListener.onMarkerUpdate(it) },
120-
onDelete = {
121-
if (it.routeWaypoint) {
122-
removeWaypoint(it)
123-
}
124-
markerInteractionListener.onMarkerDelete(it)
119+
fragmentManager?.let { fm ->
120+
if (fm.findFragmentByTag(MarkerEditorDialog.TAG) != null) {
121+
return
125122
}
126-
)
123+
MarkerEditorDialog.newInstance(
124+
markerModel.id,
125+
markerModel.name,
126+
markerModel.description
127+
).show(fm, MarkerEditorDialog.TAG)
128+
}
127129
}
128130
}
129131

0 commit comments

Comments
 (0)