Skip to content

Commit

Permalink
Implement view model for MapSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Jan 17, 2024
1 parent 8482d15 commit 2bd78b6
Show file tree
Hide file tree
Showing 8 changed files with 481 additions and 372 deletions.
34 changes: 16 additions & 18 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
import mobi.maptrek.util.SunriseSunset;
import mobi.maptrek.view.Gauge;
import mobi.maptrek.viewmodels.AmenityViewModel;
import mobi.maptrek.viewmodels.MapIndexViewModel;
import mobi.maptrek.viewmodels.MapViewModel;

public class MainActivity extends AppCompatActivity implements ILocationListener,
Expand Down Expand Up @@ -400,12 +401,13 @@ private enum PANEL_STATE {
private PANEL_STATE mPanelState;
private boolean secondBack;
private Toast mBackToast;

private AmenityViewModel amenityViewModel;
private MapIndexViewModel mapIndexViewModel;
private MapViewModel mapViewModel;

private SQLiteDatabase mDetailedMapDatabase;
private MapIndex mMapIndex;
private Index mNativeMapIndex;
private MapTrekTileSource mNativeTileSource;
private List<MapFile> mBitmapLayerMaps;
private WaypointDbDataSource mWaypointDbDataSource;
Expand Down Expand Up @@ -499,7 +501,6 @@ protected void onCreate(Bundle savedInstanceState) {
mFragmentManager = getSupportFragmentManager();
mFragmentManager.registerFragmentLifecycleCallbacks(mFragmentLifecycleCallback, true);

mNativeMapIndex = application.getMapIndex();
mMapIndex = application.getExtraMapIndex();

mShieldFactory = application.getShieldFactory();
Expand Down Expand Up @@ -699,6 +700,8 @@ public void onChildViewRemoved(View parent, View child) {
mMarkerLayer = new ItemizedLayer<>(mMap, new ArrayList<>(), symbol, MapTrek.density, this);
layers.add(mMarkerLayer, MAP_3D_DATA);

mapIndexViewModel = new ViewModelProvider(this).get(MapIndexViewModel.class);

mapViewModel = new ViewModelProvider(this).get(MapViewModel.class);
// Observe marker state
mapViewModel.getMarkerState().observe(this, markerState -> {
Expand Down Expand Up @@ -1018,8 +1021,8 @@ public void onAnimationEnd(Animator animation) {
ft.replace(R.id.contentPanel, fragment, "crashReport");
ft.addToBackStack("crashReport");
ft.commit();
} else if (!mBaseMapWarningShown && mNativeMapIndex != null && mNativeMapIndex.getBaseMapVersion() == 0) {
BaseMapDownload dialogFragment = new BaseMapDownload(mNativeMapIndex);
} else if (!mBaseMapWarningShown && mapIndexViewModel.nativeIndex.getBaseMapVersion() == 0) {
BaseMapDownload dialogFragment = new BaseMapDownload();
dialogFragment.show(mFragmentManager, "baseMapDownload");
mBaseMapWarningShown = true;
} else if (WhatsNewDialog.shouldShow()) {
Expand Down Expand Up @@ -1438,7 +1441,7 @@ public void onDismissed(Snackbar transientBottomBar, int event) {
return true;
} else if (action == R.id.actionSettings) {
Bundle args = new Bundle(1);
args.putBoolean(Settings.ARG_HILLSHADES_AVAILABLE, mNativeMapIndex.hasHillshades());
args.putBoolean(Settings.ARG_HILLSHADES_AVAILABLE, mapIndexViewModel.nativeIndex.hasHillshades());
FragmentFactory factory = mFragmentManager.getFragmentFactory();
Fragment fragment = factory.instantiate(getClassLoader(), Settings.class.getName());
fragment.setArguments(args);
Expand Down Expand Up @@ -2106,7 +2109,7 @@ public void setHighlightedType(int type) {

private void enableTracking() {
Intent intent = new Intent(getApplicationContext(), LocationService.class).setAction(BaseLocationService.ENABLE_TRACK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (Build.VERSION.SDK_INT >= 26)
startForegroundService(intent);
else
startService(intent);
Expand Down Expand Up @@ -3192,7 +3195,6 @@ private void startMapSelection(boolean zoom) {
}
FragmentFactory factory = mFragmentManager.getFragmentFactory();
MapSelection fragment = (MapSelection) factory.instantiate(getClassLoader(), MapSelection.class.getName());
fragment.setMapIndex(mNativeMapIndex);
fragment.setEnterTransition(new Slide());
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.contentPanel, fragment, "mapSelection");
Expand Down Expand Up @@ -3284,7 +3286,7 @@ public void onTransparencyChanged(int transparency) {

@Override
public void onBeginMapManagement() {
mMapCoverageLayer = new MapCoverageLayer(getApplicationContext(), mMap, mNativeMapIndex, MapTrek.density);
mMapCoverageLayer = new MapCoverageLayer(getApplicationContext(), mMap, mapIndexViewModel.nativeIndex, MapTrek.density);
mMap.layers().add(mMapCoverageLayer, MAP_OVERLAYS);
MapPosition mapPosition = mMap.getMapPosition();
if (mapPosition.zoomLevel > 8) {
Expand All @@ -3295,23 +3297,18 @@ public void onBeginMapManagement() {
}
int[] xy = (int[]) mViews.mapDownloadButton.getTag(R.id.mapKey);
if (xy != null)
mNativeMapIndex.selectNativeMap(xy[0], xy[1], Index.ACTION.DOWNLOAD);
mapIndexViewModel.nativeIndex.selectNativeMap(xy[0], xy[1], Index.ACTION.DOWNLOAD);
}

@Override
public void onFinishMapManagement() {
mMap.layers().remove(mMapCoverageLayer);
mMapCoverageLayer.onDetach();
mNativeMapIndex.clearSelections();
mapIndexViewModel.nativeIndex.clearSelections();
mMapCoverageLayer = null;
mMap.updateMap(true);
}

@Override
public void onManageNativeMaps(boolean hillshadesEnabled) {
mNativeMapIndex.manageNativeMaps(hillshadesEnabled);
}

private void deleteWaypoints(@NonNull Set<Waypoint> waypoints) {
HashSet<FileDataSource> sources = new HashSet<>();
for (Waypoint waypoint : waypoints) {
Expand Down Expand Up @@ -3896,8 +3893,9 @@ public void checkMissingData(MapPosition mapPosition) {
if (tileData != null) {
int mapX = tile.tileX >> (tile.zoomLevel - 7);
int mapY = tile.tileY >> (tile.zoomLevel - 7);
if (!mNativeMapIndex.isDownloading(mapX, mapY) && // Do not show button if this map is already downloading
!(mNativeMapIndex.hasDownloadSizes() && mNativeMapIndex.getNativeMap(mapX, mapY).downloadSize == 0L)) { // Do not show button if there is no map for that area
if (!mapIndexViewModel.nativeIndex.isDownloading(mapX, mapY) && // Do not show button if this map is already downloading
!(mapIndexViewModel.nativeIndex.hasDownloadSizes()
&& mapIndexViewModel.nativeIndex.getNativeMap(mapX, mapY).downloadSize == 0L)) { // Do not show button if there is no map for that area
visibility = View.VISIBLE;
map = new int[]{mapX, mapY};
}
Expand Down Expand Up @@ -4762,7 +4760,7 @@ public String getStatsString() {
Configuration.getTrackingTime() + "," +
mWaypointDbDataSource.getWaypointsCount() + "," +
mData.size() + "," +
mNativeMapIndex.getMapsCount() + "," +
mapIndexViewModel.nativeIndex.getMapsCount() + "," +
mMapIndex.getMaps().size() + "," +
Configuration.getFullScreenTimes() + "," +
Configuration.getHikingTimes() + "," +
Expand Down
19 changes: 7 additions & 12 deletions app/src/main/java/mobi/maptrek/fragments/BaseMapDownload.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Andrey Novikov
* Copyright 2024 Andrey Novikov
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
Expand Down Expand Up @@ -28,30 +28,25 @@

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProvider;

import mobi.maptrek.R;
import mobi.maptrek.maps.maptrek.Index;
import mobi.maptrek.viewmodels.MapIndexViewModel;

public class BaseMapDownload extends DialogFragment {
@NonNull
private final Index mMapIndex;

public BaseMapDownload(@NonNull Index mapIndex) {
super();
mMapIndex = mapIndex;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View dialogView = getLayoutInflater().inflate(R.layout.fragment_basemap_download, null);

MapIndexViewModel mapIndexViewModel = new ViewModelProvider(requireActivity()).get(MapIndexViewModel.class);

TextView messageView = dialogView.findViewById(R.id.message);
long size = mMapIndex.getBaseMapSize();
long size = mapIndexViewModel.nativeIndex.getBaseMapSize();
messageView.setText(getString(R.string.msgBaseMapDownload, Formatter.formatFileSize(getContext(), size)));

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
dialogBuilder.setPositiveButton(R.string.actionDownload, (dialog, which) -> mMapIndex.downloadBaseMap());
dialogBuilder.setPositiveButton(R.string.actionDownload, (dialog, which) -> mapIndexViewModel.nativeIndex.downloadBaseMap());
dialogBuilder.setNegativeButton(R.string.actionSkip, (dialog, which) -> {});
dialogBuilder.setView(dialogView);

Expand Down
Loading

0 comments on commit 2bd78b6

Please sign in to comment.