diff --git a/app/src/main/java/mobi/maptrek/MainActivity.java b/app/src/main/java/mobi/maptrek/MainActivity.java index b2bd54e2..248f3a77 100644 --- a/app/src/main/java/mobi/maptrek/MainActivity.java +++ b/app/src/main/java/mobi/maptrek/MainActivity.java @@ -85,7 +85,6 @@ import androidx.fragment.app.FragmentFactory; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; -import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import androidx.loader.app.LoaderManager; @@ -690,7 +689,10 @@ public void onChildViewRemoved(View parent, View child) { mapIndexViewModel = new ViewModelProvider(this).get(MapIndexViewModel.class); dataSourceViewModel = new ViewModelProvider(this).get(DataSourceViewModel.class); - dataSourceViewModel.selectedDataSource.observe(this, selectedDataSourceObserver); + if (savedInstanceState != null) { + dataSourceViewModel.showNativeTracks(savedInstanceState.getBoolean("nativeTracks")); + } + dataSourceViewModel.dataSourceCommand.observe(this, dataSourceCommandObserver); // Observe marker state mapViewModel.getMarkerState().observe(this, markerState -> { @@ -1183,8 +1185,6 @@ protected void onDestroy() { @Override public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { - logger.debug("onSaveInstanceState()"); - MapTrek application = MapTrek.getApplication(); application.setEditedWaypoint(mEditedWaypoint); application.setBitmapLayerMaps(mBitmapLayerMaps); @@ -1202,6 +1202,7 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { savedInstanceState.putBoolean("savedNavigationState", mNavigationService != null); if (mViews.progressBar.getVisibility() == View.VISIBLE) savedInstanceState.putInt("progressBar", mViews.progressBar.getMax()); + savedInstanceState.putBoolean("nativeTracks", Boolean.TRUE.equals(dataSourceViewModel.getNativeTracksState().getValue())); savedInstanceState.putSerializable("panelState", mPanelState); savedInstanceState.putBoolean("autoTiltShouldSet", mAutoTiltShouldSet); savedInstanceState.putBoolean("baseMapWarningShown", mBaseMapWarningShown); @@ -1692,12 +1693,7 @@ private void onPlacesClicked() { Fragment fragment = factory.instantiate(getClassLoader(), DataSourceList.class.getName()); showExtendPanel(PANEL_STATE.PLACES, "dataSourceList", fragment); } else { - dataSourceViewModel.selectedDataSource.setValue( - new DataSourceViewModel.SelectedDataSourceState( - dataSourceViewModel.waypointDbDataSource, - DataSourceViewModel.MODE_PANEL - ) - ); + dataSourceViewModel.selectDataSource(dataSourceViewModel.waypointDbDataSource, DataSourceViewModel.MODE_PANEL); } } @@ -4282,11 +4278,9 @@ public void setDataSourceAvailability(FileDataSource source, boolean available) mMap.updateMap(true); } - private final Observer selectedDataSourceObserver = new Observer() { + private final Observer dataSourceCommandObserver = new Observer() { @Override public void onChanged(DataSourceViewModel.SelectedDataSourceState dataSourceState) { - if (getLifecycle().getCurrentState() != Lifecycle.State.RESUMED) - return; if (dataSourceState.dataSource.isNativeTrack()) { Track track = ((TrackDataSource) dataSourceState.dataSource).getTracks().get(0); onTrackDetails(track); diff --git a/app/src/main/java/mobi/maptrek/fragments/DataList.java b/app/src/main/java/mobi/maptrek/fragments/DataList.java index 73e94a59..c7adf93e 100644 --- a/app/src/main/java/mobi/maptrek/fragments/DataList.java +++ b/app/src/main/java/mobi/maptrek/fragments/DataList.java @@ -111,10 +111,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat super.onViewCreated(view, savedInstanceState); dataSourceViewModel = new ViewModelProvider(requireActivity()).get(DataSourceViewModel.class); - dataSourceViewModel.selectedDataSource.observe(getViewLifecycleOwner(), dataSourceState -> { + dataSourceViewModel.selectedDataSource.observe(getViewLifecycleOwner(), dataSource -> { logger.debug("dataSource changed"); - setDataSource(dataSourceState.dataSource, savedInstanceState); - if (dataSourceState.dataSource instanceof WaypointDbDataSource) { + setDataSource(dataSource, savedInstanceState); + if (dataSource instanceof WaypointDbDataSource) { mFloatingButton = mFragmentHolder.enableListActionButton(R.drawable.ic_add_location, v -> { CoordinatesInput.Builder builder = new CoordinatesInput.Builder(); CoordinatesInput coordinatesInput = builder.setCallbacks(DataList.this) @@ -131,10 +131,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat MapViewModel mapViewModel = new ViewModelProvider(requireActivity()).get(MapViewModel.class); mapViewModel.currentLocation.observe(getViewLifecycleOwner(), location -> { logger.debug("location changed"); - DataSourceViewModel.SelectedDataSourceState state = dataSourceViewModel.selectedDataSource.getValue(); - if (state == null) - return; - DataSource dataSource = state.dataSource; + DataSource dataSource = dataSourceViewModel.selectedDataSource.getValue(); if (dataSource == null) return; if ("unknown".equals(location.getProvider())) { @@ -176,10 +173,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); - DataSourceViewModel.SelectedDataSourceState state = dataSourceViewModel.selectedDataSource.getValue(); - if (state == null) + DataSource dataSource = dataSourceViewModel.selectedDataSource.getValue(); + if (dataSource == null) return; - DataSource dataSource = state.dataSource; Cursor cursor = dataSource.getCursor(); if (cursor.getCount() > 0) { View view = viewBinding.list.getChildAt(0).findViewById(R.id.view); @@ -334,10 +330,9 @@ private void deleteSelectedItems() { } private void populateSelectedItems(HashSet waypoints, HashSet tracks, HashSet routes) { - DataSourceViewModel.SelectedDataSourceState state = dataSourceViewModel.selectedDataSource.getValue(); - if (state == null) + DataSource dataSource = dataSourceViewModel.selectedDataSource.getValue(); + if (dataSource == null) return; - DataSource dataSource = state.dataSource; Cursor cursor = dataSource.getCursor(); for (int position = 0; position < cursor.getCount(); position++) { cursor.moveToPosition(position); diff --git a/app/src/main/java/mobi/maptrek/fragments/DataSourceList.java b/app/src/main/java/mobi/maptrek/fragments/DataSourceList.java index 74272d7a..d91658e5 100644 --- a/app/src/main/java/mobi/maptrek/fragments/DataSourceList.java +++ b/app/src/main/java/mobi/maptrek/fragments/DataSourceList.java @@ -265,15 +265,7 @@ void bindView(DataSource dataSource, int position) { icon.setImageResource(R.drawable.ic_points); action.setVisibility(View.GONE); action.setOnClickListener(null); - itemView.setOnClickListener(v -> { - // mFragmentHolder.disableListActionButton(); - dataSourceViewModel.selectedDataSource.setValue( - new DataSourceViewModel.SelectedDataSourceState( - dataSource, - DataSourceViewModel.MODE_SELECTOR - ) - ); - }); + itemView.setOnClickListener(v -> dataSourceViewModel.selectDataSource(dataSource, DataSourceViewModel.MODE_SELECTOR)); } else { File file = new File(((FileDataSource) dataSource).path); if (dataSource.isLoaded()) { @@ -311,15 +303,7 @@ else if (routesCount > 0 && waypointsCount == 0 && tracksCount == 0) else icon.setImageResource(R.drawable.ic_dataset); } - itemView.setOnClickListener(v -> { - // mFragmentHolder.disableListActionButton(); - dataSourceViewModel.selectedDataSource.setValue( - new DataSourceViewModel.SelectedDataSourceState( - dataSource, - DataSourceViewModel.MODE_SELECTOR - ) - ); - }); + itemView.setOnClickListener(v -> dataSourceViewModel.selectDataSource(dataSource, DataSourceViewModel.MODE_SELECTOR)); } else { String size = Formatter.formatShortFileSize(getContext(), file.length()); description.setText(String.format(Locale.ENGLISH, "%s – %s", size, file.getName())); diff --git a/app/src/main/java/mobi/maptrek/viewmodels/DataSourceViewModel.java b/app/src/main/java/mobi/maptrek/viewmodels/DataSourceViewModel.java index fe16baa6..9f3c853f 100644 --- a/app/src/main/java/mobi/maptrek/viewmodels/DataSourceViewModel.java +++ b/app/src/main/java/mobi/maptrek/viewmodels/DataSourceViewModel.java @@ -29,6 +29,7 @@ import mobi.maptrek.data.source.DataSource; import mobi.maptrek.data.source.FileDataSource; import mobi.maptrek.data.source.WaypointDbDataSource; +import mobi.maptrek.util.SingleLiveEvent; public class DataSourceViewModel extends ViewModel { public final WaypointDbDataSource waypointDbDataSource = MapTrek.getApplication().getWaypointDbDataSource(); @@ -98,5 +99,11 @@ public SelectedDataSourceState(DataSource dataSource, int mode) { } } - public final MutableLiveData selectedDataSource = new MutableLiveData<>(); + public final MutableLiveData selectedDataSource = new MutableLiveData<>(); + public final SingleLiveEvent dataSourceCommand = new SingleLiveEvent<>(); + + public void selectDataSource(DataSource dataSource, int mode) { + selectedDataSource.setValue(dataSource); + dataSourceCommand.setValue(new DataSourceViewModel.SelectedDataSourceState(dataSource, mode)); + } }