Save and load the state of the map (map center and zoom level) for a recording (was broken before)

This commit is contained in:
y20k 2021-09-16 22:44:34 +02:00
parent d22638da92
commit b8c3da2619
No known key found for this signature in database
GPG key ID: 824D4259F41FAFF6
2 changed files with 23 additions and 33 deletions

View file

@ -53,26 +53,26 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
/* Main class variables */ /* Main class variables */
private lateinit var layout: TrackFragmentLayoutHolder private lateinit var layout: TrackFragmentLayoutHolder
private lateinit var track: Track private lateinit var trackFileUriString: String
/* Overrides onCreate from Fragment */ /* Overrides onCreate from Fragment */
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// get track trackFileUriString = arguments?.getString(Keys.ARG_TRACK_FILE_URI, String()) ?: String()
val fileUriString: String = arguments?.getString(Keys.ARG_TRACK_FILE_URI, String()) ?: String()
if (fileUriString.isNotBlank()) {
track = FileHelper.readTrack(activity as Context, Uri.parse(fileUriString))
} else {
track = Track()
}
} }
/* Overrides onCreateView from Fragment */ /* Overrides onCreateView from Fragment */
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
// initialize layout // initialize layout
val statusBarHeight: Int = UiHelper.getStatusBarHeight(activity as Context) val statusBarHeight: Int = UiHelper.getStatusBarHeight(activity as Context)
val track: Track
if (this::trackFileUriString.isInitialized && trackFileUriString.isNotBlank()) {
track = FileHelper.readTrack(activity as Context, Uri.parse(trackFileUriString))
} else {
track = Track()
}
layout = TrackFragmentLayoutHolder(activity as Context, this as MapOverlayHelper.MarkerListener, inflater, statusBarHeight, container, track) layout = TrackFragmentLayoutHolder(activity as Context, this as MapOverlayHelper.MarkerListener, inflater, statusBarHeight, container, track)
// set up share button // set up share button
@ -102,8 +102,6 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
/* Overrides onResume from Fragment */ /* Overrides onResume from Fragment */
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
// update zoom level and map center
layout.updateMapView()
} }
@ -114,15 +112,16 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
layout.saveViewStateToTrack() layout.saveViewStateToTrack()
} }
/* Register the ActivityResultLauncher for saving GPX */ /* Register the ActivityResultLauncher for saving GPX */
private val requestSaveGpxLauncher = private val requestSaveGpxLauncher = registerForActivityResult(StartActivityForResult(), this::requestSaveGpxResult)
registerForActivityResult(StartActivityForResult(), this::requestSaveGpxResult)
/* Pass the activity result */ /* Pass the activity result */
private fun requestSaveGpxResult(result: ActivityResult) { private fun requestSaveGpxResult(result: ActivityResult) {
// save GPX file to result file location // save GPX file to result file location
if (result.resultCode == Activity.RESULT_OK && result.data != null) { if (result.resultCode == Activity.RESULT_OK && result.data != null) {
val sourceUri: Uri = Uri.parse(track.gpxUriString) val sourceUri: Uri = Uri.parse(layout.track.gpxUriString)
val targetUri: Uri? = result.data?.data val targetUri: Uri? = result.data?.data
if (targetUri != null) { if (targetUri != null) {
// copy file async (= fire & forget - no return value needed) // copy file async (= fire & forget - no return value needed)
@ -153,7 +152,7 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
// user tapped remove track // user tapped remove track
true -> { true -> {
// switch to TracklistFragment and remove track there // switch to TracklistFragment and remove track there
val bundle: Bundle = bundleOf(Keys.ARG_TRACK_ID to track.getTrackId()) val bundle: Bundle = bundleOf(Keys.ARG_TRACK_ID to layout.track.getTrackId())
findNavController().navigate(R.id.tracklist_fragment, bundle) findNavController().navigate(R.id.tracklist_fragment, bundle)
} }
} }
@ -166,10 +165,8 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
override fun onMarkerTapped(latitude: Double, longitude: Double) { override fun onMarkerTapped(latitude: Double, longitude: Double) {
super.onMarkerTapped(latitude, longitude) super.onMarkerTapped(latitude, longitude)
// update track display // update track display
track = TrackHelper.toggleStarred(activity as Context, track, latitude, longitude) layout.track = TrackHelper.toggleStarred(activity as Context, layout.track, latitude, longitude)
layout.updateTrackOverlay(track) layout.updateTrackOverlay()
// save track
CoroutineScope(Dispatchers.IO).launch { FileHelper.saveTrackSuspended(track, true) }
} }
@ -178,7 +175,7 @@ class TrackFragment : Fragment(), RenameTrackDialog.RenameTrackListener, YesNoDi
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
type = Keys.MIME_TYPE_GPX type = Keys.MIME_TYPE_GPX
putExtra(Intent.EXTRA_TITLE, FileHelper.getGpxFileName(track)) putExtra(Intent.EXTRA_TITLE, FileHelper.getGpxFileName(layout.track))
} }
// file gets saved in the ActivityResult // file gets saved in the ActivityResult
try { try {

View file

@ -73,7 +73,7 @@ data class TrackFragmentLayoutHolder(private var context: Context, private var m
private var trackSpecialMarkersOverlay: ItemizedIconOverlay<OverlayItem>? private var trackSpecialMarkersOverlay: ItemizedIconOverlay<OverlayItem>?
private var trackOverlay: SimpleFastPointOverlay? private var trackOverlay: SimpleFastPointOverlay?
private var controller: IMapController private var controller: IMapController
private var zoomLevel: Double //private var zoomLevel: Double
private val statisticsSheetBehavior: BottomSheetBehavior<View> private val statisticsSheetBehavior: BottomSheetBehavior<View>
private val statisticsSheet: NestedScrollView private val statisticsSheet: NestedScrollView
private val statisticsView: View private val statisticsView: View
@ -113,8 +113,8 @@ data class TrackFragmentLayoutHolder(private var context: Context, private var m
mapView.setTileSource(TileSourceFactory.MAPNIK) mapView.setTileSource(TileSourceFactory.MAPNIK)
mapView.setMultiTouchControls(true) mapView.setMultiTouchControls(true)
mapView.zoomController.setVisibility(org.osmdroid.views.CustomZoomButtonsController.Visibility.NEVER) mapView.zoomController.setVisibility(org.osmdroid.views.CustomZoomButtonsController.Visibility.NEVER)
zoomLevel = Keys.DEFAULT_ZOOM_LEVEL controller.setCenter(GeoPoint(track.latitude, track.longitude))
controller.setZoom(zoomLevel) controller.setZoom(track.zoomLevel)
// get views for statistics sheet // get views for statistics sheet
statisticsSheet = rootView.findViewById(R.id.statistics_sheet) statisticsSheet = rootView.findViewById(R.id.statistics_sheet)
@ -167,17 +167,8 @@ data class TrackFragmentLayoutHolder(private var context: Context, private var m
} }
/* Updates zoom level and center of this map */
fun updateMapView() {
val position = GeoPoint(track.latitude, track.longitude)
controller.setCenter(position)
controller.setZoom(track.zoomLevel)
}
/* Updates map overlay */ /* Updates map overlay */
fun updateTrackOverlay(newTrack: Track) { fun updateTrackOverlay() {
track = newTrack
if (trackOverlay != null) { if (trackOverlay != null) {
mapView.overlays.remove(trackOverlay) mapView.overlays.remove(trackOverlay)
} }
@ -191,6 +182,8 @@ data class TrackFragmentLayoutHolder(private var context: Context, private var m
mapView.overlays.add(trackOverlay) mapView.overlays.add(trackOverlay)
mapView.overlays.add(trackSpecialMarkersOverlay) mapView.overlays.add(trackSpecialMarkersOverlay)
} }
// save track
CoroutineScope(Dispatchers.IO).launch { FileHelper.saveTrackSuspended(track, true) }
} }