Skip to content

Commit

Permalink
Make properties change a dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Feb 2, 2024
1 parent cefca14 commit 4671620
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 114 deletions.
31 changes: 6 additions & 25 deletions app/src/main/java/mobi/maptrek/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
import mobi.maptrek.fragments.TrackInformation;
import mobi.maptrek.fragments.TrackProperties;
import mobi.maptrek.fragments.WaypointInformation;
import mobi.maptrek.fragments.WaypointProperties;
import mobi.maptrek.fragments.WaypointPropertiesDialog;
import mobi.maptrek.fragments.WhatsNewDialog;
import mobi.maptrek.fragments.preferences.BasePreferences;
import mobi.maptrek.io.Manager;
Expand Down Expand Up @@ -283,7 +283,7 @@ public class MainActivity extends AppCompatActivity implements ILocationListener
Map.UpdateListener,
GestureListener,
FragmentHolder,
WaypointProperties.OnWaypointPropertiesChangedListener,
WaypointPropertiesDialog.OnWaypointPropertiesChangedListener,
TrackProperties.OnTrackPropertiesChangedListener,
OnLocationListener,
OnWaypointActionListener,
Expand Down Expand Up @@ -1140,7 +1140,6 @@ protected void onDestroy() {
unregisterReceiver(mWaypointBroadcastReceiver);
mWaypointBroadcastReceiver = null;
}
dataSourceViewModel.waypointDbDataSource.close();

mDetailedMapDatabase = null;
mProgressHandler = null;
Expand Down Expand Up @@ -2510,17 +2509,8 @@ public void showMarkerInformation(@NonNull GeoPoint point, @Nullable String name

private void onWaypointProperties(Waypoint waypoint) {
mEditedWaypoint = waypoint;
Bundle args = new Bundle(2);
args.putString(WaypointProperties.ARG_NAME, mEditedWaypoint.name);
args.putInt(WaypointProperties.ARG_COLOR, mEditedWaypoint.style.color);
FragmentFactory factory = mFragmentManager.getFragmentFactory();
Fragment fragment = factory.instantiate(getClassLoader(), WaypointProperties.class.getName());
fragment.setArguments(args);
fragment.setEnterTransition(new Fade());
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.contentPanel, fragment, "waypointProperties");
ft.addToBackStack("waypointProperties");
ft.commit();
WaypointPropertiesDialog dialogFragment = new WaypointPropertiesDialog(waypoint);
dialogFragment.show(mFragmentManager, "waypointProperties");
}

@Override
Expand Down Expand Up @@ -2727,17 +2717,8 @@ private void onTrackProperties(String path) {
if (mEditedTrack == null)
return;

Bundle args = new Bundle(2);
args.putString(TrackProperties.ARG_NAME, mEditedTrack.name);
args.putInt(TrackProperties.ARG_COLOR, mEditedTrack.style.color);
FragmentFactory factory = mFragmentManager.getFragmentFactory();
Fragment fragment = factory.instantiate(getClassLoader(), TrackProperties.class.getName());
fragment.setArguments(args);
fragment.setEnterTransition(new Fade());
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.replace(R.id.contentPanel, fragment, "trackProperties");
ft.addToBackStack("trackProperties");
ft.commit();
TrackProperties dialogFragment = new TrackProperties(mEditedTrack);
dialogFragment.show(mFragmentManager, "trackProperties");
}

@Override
Expand Down
89 changes: 45 additions & 44 deletions app/src/main/java/mobi/maptrek/fragments/TrackProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 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 All @@ -16,83 +16,92 @@

package mobi.maptrek.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
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.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.DialogFragment;

import info.andreynovikov.androidcolorpicker.ColorPickerDialog;
import info.andreynovikov.androidcolorpicker.ColorPickerSwatch;

import mobi.maptrek.R;
import mobi.maptrek.data.Track;
import mobi.maptrek.data.style.MarkerStyle;

public class TrackProperties extends Fragment {
public static final String ARG_NAME = "name";
public static final String ARG_COLOR = "color";

public class TrackProperties extends DialogFragment {
private EditText mNameEdit;
private ColorPickerSwatch mColorSwatch;
private String mName;
private int mColor;

private OnTrackPropertiesChangedListener mListener;
private FragmentHolder mFragmentHolder;

public TrackProperties() {
super();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_track_properties, container, false);
mNameEdit = rootView.findViewById(R.id.nameEdit);
mColorSwatch = rootView.findViewById(R.id.colorSwatch);
return rootView;
public TrackProperties(Track track) {
super();
mName = track.name;
mColor = track.style.color;
}

@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.dialog_track_properties, null);

mNameEdit = dialogView.findViewById(R.id.nameEdit);
mColorSwatch = dialogView.findViewById(R.id.colorSwatch);

String name = mName;
int color = mColor;

if (savedInstanceState != null) {
mNameEdit.setText(savedInstanceState.getString(ARG_NAME));
mColorSwatch.setColor(savedInstanceState.getInt(ARG_COLOR));
} else {
Bundle arguments = getArguments();
if (arguments != null) {
mName = getArguments().getString(ARG_NAME);
mColor = getArguments().getInt(ARG_COLOR);
}
name = savedInstanceState.getString("name");
color = savedInstanceState.getInt("color");
}

mNameEdit.setText(mName);
mColorSwatch.setColor(mColor);

mNameEdit.setText(name);
mNameEdit.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
returnResult();
mFragmentHolder.popCurrent();
dismiss();
}
return false;
});
mColorSwatch.setColor(mColor);

mColorSwatch.setColor(color);
mColorSwatch.setOnClickListener(v -> {
ColorPickerDialog dialog = new ColorPickerDialog();
dialog.setColors(MarkerStyle.DEFAULT_COLORS, mColor);
dialog.setArguments(R.string.color_picker_default_title, 4, ColorPickerDialog.SIZE_SMALL);
dialog.setOnColorSelectedListener(color -> {
mColorSwatch.setColor(color);
mColor = color;
});
dialog.setOnColorSelectedListener(color1 -> mColorSwatch.setColor(color1));
dialog.show(getParentFragmentManager(), "ColorPickerDialog");
});

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
dialogBuilder.setPositiveButton(R.string.actionSave, (dialog, which) -> returnResult());
dialogBuilder.setView(dialogView);

Dialog dialog = dialogBuilder.create();

Window window = dialog.getWindow();
assert window != null;
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM;
window.setAttributes(lp);

return dialog;
}

@Override
Expand All @@ -103,27 +112,19 @@ public void onAttach(@NonNull Context context) {
} catch (ClassCastException e) {
throw new ClassCastException(context + " must implement OnTrackPropertiesChangedListener");
}
mFragmentHolder = (FragmentHolder) context;
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
mFragmentHolder = null;
}

@Override
public void onDestroyView() {
super.onDestroyView();
returnResult();
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_NAME, mNameEdit.getText().toString());
outState.putInt(ARG_COLOR, mColorSwatch.getColor());
outState.putString("name", mNameEdit.getText().toString());
outState.putInt("color", mColorSwatch.getColor());
}

private void returnResult() {
Expand Down
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 All @@ -16,69 +16,65 @@

package mobi.maptrek.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
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.view.inputmethod.EditorInfo;
import android.widget.EditText;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.DialogFragment;

import info.andreynovikov.androidcolorpicker.ColorPickerDialog;
import info.andreynovikov.androidcolorpicker.ColorPickerSwatch;
import mobi.maptrek.R;
import mobi.maptrek.data.Waypoint;
import mobi.maptrek.data.style.MarkerStyle;

public class WaypointProperties extends Fragment {
public static final String ARG_NAME = "name";
public static final String ARG_COLOR = "color";

public class WaypointPropertiesDialog extends DialogFragment {
private EditText mNameEdit;
private ColorPickerSwatch mColorSwatch;
private String mName;
private int mColor;

private OnWaypointPropertiesChangedListener mListener;
private FragmentHolder mFragmentHolder;

public WaypointProperties() {
public WaypointPropertiesDialog() {
super();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_track_properties, container, false);
mNameEdit = rootView.findViewById(R.id.nameEdit);
mColorSwatch = rootView.findViewById(R.id.colorSwatch);
return rootView;
public WaypointPropertiesDialog(Waypoint waypoint) {
super();
mName = waypoint.name;
mColor = waypoint.style.color;
}

@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.dialog_track_properties, null);

Bundle arguments = getArguments();
if (arguments != null) {
mName = arguments.getString(ARG_NAME);
mColor = arguments.getInt(ARG_COLOR);
}
mNameEdit = dialogView.findViewById(R.id.nameEdit);
mColorSwatch = dialogView.findViewById(R.id.colorSwatch);

String name = mName;
int color = mColor;

if (savedInstanceState != null) {
name = savedInstanceState.getString(ARG_NAME);
color = savedInstanceState.getInt(ARG_COLOR);
name = savedInstanceState.getString("name");
color = savedInstanceState.getInt("color");
}

mNameEdit.setText(name);
mNameEdit.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
returnResult();
mFragmentHolder.popCurrent();
dismiss();
}
return false;
});
Expand All @@ -91,6 +87,20 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
dialog.setOnColorSelectedListener(color1 -> mColorSwatch.setColor(color1));
dialog.show(getParentFragmentManager(), "ColorPickerDialog");
});

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
dialogBuilder.setPositiveButton(R.string.actionSave, (dialog, which) -> returnResult());
dialogBuilder.setView(dialogView);

Dialog dialog = dialogBuilder.create();

Window window = dialog.getWindow();
assert window != null;
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM;
window.setAttributes(lp);

return dialog;
}

@Override
Expand All @@ -101,23 +111,19 @@ public void onAttach(@NonNull Context context) {
} catch (ClassCastException e) {
throw new ClassCastException(context + " must implement OnWaypointPropertiesChangedListener");
}
mFragmentHolder = (FragmentHolder) context;
requireActivity().getOnBackPressedDispatcher().addCallback(this, mBackPressedCallback);
}

@Override
public void onDetach() {
super.onDetach();
mBackPressedCallback.remove();
mFragmentHolder = null;
mListener = null;
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ARG_NAME, mNameEdit.getText().toString());
outState.putInt(ARG_COLOR, mColorSwatch.getColor());
outState.putString("name", mNameEdit.getText().toString());
outState.putInt("color", mColorSwatch.getColor());
}

private void returnResult() {
Expand All @@ -130,15 +136,6 @@ private void returnResult() {
}
}

OnBackPressedCallback mBackPressedCallback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
returnResult();
this.remove();
requireActivity().getOnBackPressedDispatcher().onBackPressed();
}
};

public interface OnWaypointPropertiesChangedListener {
void onWaypointPropertiesChanged(String name, int color);
}
Expand Down
Loading

0 comments on commit 4671620

Please sign in to comment.