Skip to content

Commit

Permalink
Optimize data source selection
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Feb 2, 2024
1 parent 52efa27 commit c0ec9a8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 45 deletions.
20 changes: 7 additions & 13 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -4282,11 +4278,9 @@ public void setDataSourceAvailability(FileDataSource source, boolean available)
mMap.updateMap(true);
}

private final Observer<DataSourceViewModel.SelectedDataSourceState> selectedDataSourceObserver = new Observer<DataSourceViewModel.SelectedDataSourceState>() {
private final Observer<DataSourceViewModel.SelectedDataSourceState> dataSourceCommandObserver = new Observer<DataSourceViewModel.SelectedDataSourceState>() {
@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);
Expand Down
21 changes: 8 additions & 13 deletions app/src/main/java/mobi/maptrek/fragments/DataList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -334,10 +330,9 @@ private void deleteSelectedItems() {
}

private void populateSelectedItems(HashSet<Waypoint> waypoints, HashSet<Track> tracks, HashSet<Route> 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);
Expand Down
20 changes: 2 additions & 18 deletions app/src/main/java/mobi/maptrek/fragments/DataSourceList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -98,5 +99,11 @@ public SelectedDataSourceState(DataSource dataSource, int mode) {
}
}

public final MutableLiveData<SelectedDataSourceState> selectedDataSource = new MutableLiveData<>();
public final MutableLiveData<DataSource> selectedDataSource = new MutableLiveData<>();
public final SingleLiveEvent<SelectedDataSourceState> dataSourceCommand = new SingleLiveEvent<>();

public void selectDataSource(DataSource dataSource, int mode) {
selectedDataSource.setValue(dataSource);
dataSourceCommand.setValue(new DataSourceViewModel.SelectedDataSourceState(dataSource, mode));
}
}

0 comments on commit c0ec9a8

Please sign in to comment.