Skip to content

Commit

Permalink
Convert base map download window to dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Dec 21, 2023
1 parent 8f6f4d1 commit 7f5ca70
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 75 deletions.
10 changes: 2 additions & 8 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -963,14 +963,8 @@ public void onAnimationEnd(Animator animation) {
ft.addToBackStack("crashReport");
ft.commit();
} else if (!mBaseMapWarningShown && mNativeMapIndex != null && mNativeMapIndex.getBaseMapVersion() == 0) {
FragmentFactory factory = mFragmentManager.getFragmentFactory();
BaseMapDownload fragment = (BaseMapDownload) factory.instantiate(getClassLoader(), BaseMapDownload.class.getName());
fragment.setMapIndex(mNativeMapIndex);
fragment.setEnterTransition(new Slide());
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.contentPanel, fragment, "baseMapDownload");
ft.addToBackStack("baseMapDownload");
ft.commit();
BaseMapDownload dialogFragment = new BaseMapDownload(mNativeMapIndex);
dialogFragment.show(mFragmentManager, "baseMapDownload");
mBaseMapWarningShown = true;
} else if (WhatsNewDialog.shouldShow()) {
WhatsNewDialog dialogFragment = new WhatsNewDialog();
Expand Down
88 changes: 29 additions & 59 deletions app/src/main/java/mobi/maptrek/fragments/BaseMapDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,53 @@

package mobi.maptrek.fragments;

import android.content.Context;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.DialogFragment;

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

public class BaseMapDownload extends Fragment {
private FragmentHolder mFragmentHolder;
private Index mMapIndex;
private TextView mMessageView;
public class BaseMapDownload extends DialogFragment {
@NonNull
private final Index mMapIndex;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_basemap_download, container, false);
mMessageView = rootView.findViewById(R.id.message);
return rootView;
public BaseMapDownload(@NonNull Index mapIndex) {
super();
mMapIndex = mapIndex;
}

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

FloatingActionButton floatingButton = mFragmentHolder.enableActionButton();
floatingButton.setImageDrawable(AppCompatResources.getDrawable(view.getContext(), R.drawable.ic_file_download));
floatingButton.setOnClickListener(v -> {
mMapIndex.downloadBaseMap();
mFragmentHolder.disableActionButton();
mFragmentHolder.popCurrent();
});
}
TextView messageView = dialogView.findViewById(R.id.message);
long size = mMapIndex.getBaseMapSize();
messageView.setText(getString(R.string.msgBaseMapDownload, Formatter.formatFileSize(getContext(), size)));

@Override
public void onResume() {
super.onResume();
long size = mMapIndex != null ? mMapIndex.getBaseMapSize() : Index.BASEMAP_SIZE_STUB * 1024 * 1024;
mMessageView.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.setNegativeButton(R.string.actionSkip, (dialog, which) -> {});
dialogBuilder.setView(dialogView);

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
mFragmentHolder = (FragmentHolder) context;
} catch (ClassCastException e) {
throw new ClassCastException(context + " must implement FragmentHolder");
}
requireActivity().getOnBackPressedDispatcher().addCallback(this, mBackPressedCallback);
}
Dialog dialog = dialogBuilder.create();

@Override
public void onDetach() {
super.onDetach();
mBackPressedCallback.remove();
mFragmentHolder = null;
}
Window window = dialog.getWindow();
assert window != null;
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM;
window.setAttributes(lp);

public void setMapIndex(Index mapIndex) {
mMapIndex = mapIndex;
return dialog;
}

private final OnBackPressedCallback mBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
mFragmentHolder.disableActionButton();
this.remove();
requireActivity().getOnBackPressedDispatcher().onBackPressed();
}
};
}
10 changes: 4 additions & 6 deletions app/src/main/res/layout/fragment_basemap_download.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/fragment_width"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/panelSolidBackground"
android:paddingBottom="@dimen/fragment_padding"
android:paddingEnd="@dimen/fragment_padding"
android:paddingStart="@dimen/fragment_with_fab_start_padding"
android:paddingTop="@dimen/fragment_with_fab_top_padding"
android:paddingEnd="@dimen/dialogContentMargin"
android:paddingStart="@dimen/dialogContentMargin"
android:paddingTop="@dimen/dialogContentMargin"
tools:context=".fragments.BaseMapDownload">

<TextView
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<string name="actionResetAdvices">Сбросить подсказки</string>
<string name="actionImport">Импорт</string>
<string name="actionGrant">Дать</string>
<string name="actionDownload">Загрузить</string>
<string name="actionSkip">Пропустить</string>

<string name="notifTracking">Запись трека</string>
<string name="notifTrackingFailure">Ошибка записи трека</string>
Expand Down Expand Up @@ -230,7 +232,7 @@
<string name="complete">готово</string>
<string name="downloadBasemap">Загрузить обновлённую карту мира (%s)</string>
<string name="failed">не удалось</string>
<string name="msgBaseMapDownload">Для большего удобства загрузите детальную карту мира (%s). Вы можете сделать это сейчас или позже в любое время.</string>
<string name="msgBaseMapDownload">Для большего удобства загрузите детальную карту мира (%s). Вы можете сделать это позже в любое время.</string>
<string name="processed">%d%% обработано</string>
<string name="title_map_import">Импорт карты</string>
<string name="title_map_removal">Удаление карты</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
<string name="actionMapScale">Map scale</string>
<string name="actionImport">Import</string>
<string name="actionGrant">Grant</string>
<string name="actionDownload">Download</string>
<string name="actionSkip">Skip</string>

<string name="share_location_intent_title">Share location via:</string>
<string name="share_data_intent_title">Share data to:</string>
Expand Down Expand Up @@ -161,7 +163,7 @@
<string name="msgUnsupportedFileFormat">Unsupported file format</string>
<string name="msgPlaceLocked">Place is locked</string>
<string name="msgRestorePlacesFailed">Failed to restore places</string>
<string name="msgBaseMapDownload">For better experience, download detailed world map (%s). You can do it now or later at any time.</string>
<string name="msgBaseMapDownload">To get better experience download detailed world map (%s). You can do it later at any time.</string>
<string name="msgMapDatabaseError">An error occurred while reading detailed maps, database was reset</string>
<string name="msgHillshadeDatabaseError">An error occurred while reading hillshades, database was reset</string>
<string name="msgWaitForFtsTable">Please wait while maps database is updated for full text search. Depending on how many map areas you have downloaded so far it can take up to several minutes.</string>
Expand Down

0 comments on commit 7f5ca70

Please sign in to comment.