Skip to content

Commit

Permalink
Add reset average speed action
Browse files Browse the repository at this point in the history
  • Loading branch information
0xRe1nk0 committed Sep 27, 2024
1 parent 55cd397 commit 29fe3d7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
19 changes: 19 additions & 0 deletions OsmAnd/res/layout/setting_action_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:minHeight="@dimen/bottom_sheet_list_item_height">

<TextView
android:id="@+id/action_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginHorizontal="@dimen/content_padding"
android:textColor="?attr/active_color_basic"
android:textSize="@dimen/default_list_text_size"
tools:text="Action" />

</FrameLayout>
1 change: 1 addition & 0 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- For wording and consistency, please note https://docs.osmand.net/docs/technical/contributions/translating-osmand
Thx - Hardy
-->
<string name="reset_average_speed">Reset average speed</string>
<string name="unit_volt">V</string>
<string name="wikimedia">Wikimedia</string>
<string name="open_in_browser">Open in browser</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected void setupContent(@NonNull LayoutInflater themedInflater, @NonNull Vie
setupSkipStopsSetting();
themedInflater.inflate(R.layout.divider, container);
super.setupContent(themedInflater, container);
setupSettingAction(themedInflater, container);
}

private void setupIntervalSliderCard() {
Expand All @@ -85,6 +86,16 @@ private void setupSkipStopsSetting() {
skipStopsContainer.setBackground(getPressedStateDrawable());
}

private void setupSettingAction(@NonNull LayoutInflater themedInflater, @NonNull ViewGroup container) {
themedInflater.inflate(R.layout.divider, container);
View actionView = themedInflater.inflate(R.layout.setting_action_button, null);
actionView.setBackground(getPressedStateDrawable());
actionView.setOnClickListener(v -> speedWidget.resetAverageSpeed());
TextView title = actionView.findViewById(R.id.action_title);
title.setText(R.string.reset_average_speed);
container.addView(actionView);
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static int getConvertedSpeedToSkip(@NonNull SpeedConstants speedSystem) {
}
}

public void resetLocations() {
locations.clear();
}

private static class SegmentsList {

private final Segment[] segments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.preferences.CommonPreference;
import net.osmand.plus.utils.ColorUtilities;
import net.osmand.plus.utils.OsmAndFormatter;
import net.osmand.plus.utils.OsmAndFormatter.FormattedValue;
import net.osmand.plus.utils.UiUtilities;
import net.osmand.plus.views.layers.base.OsmandMapLayer.DrawSettings;
import net.osmand.plus.views.mapwidgets.WidgetsPanel;
import net.osmand.plus.views.mapwidgets.utils.AverageSpeedComputer;
import net.osmand.plus.views.mapwidgets.WidgetType;
import net.osmand.plus.widgets.popup.PopUpMenuItem;
import net.osmand.util.Algorithms;

import java.util.ArrayList;
import java.util.List;

public class AverageSpeedWidget extends SimpleWidget {

private static final String MEASURED_INTERVAL_PREF_ID = "average_speed_measured_interval_millis";
Expand Down Expand Up @@ -77,6 +83,26 @@ private void updateAverageSpeed() {
}
}

@Nullable
@Override
protected List<PopUpMenuItem> getWidgetActions() {
List<PopUpMenuItem> actions = new ArrayList<>();
UiUtilities uiUtilities = app.getUIUtilities();
int iconColor = ColorUtilities.getDefaultIconColor(app, nightMode);

actions.add(new PopUpMenuItem.Builder(app)
.setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_reset_to_default_dark, iconColor))
.setTitleId(R.string.reset_average_speed)
.setOnClickListener(item -> resetAverageSpeed())
.showTopDivider(true)
.create());
return actions;
}

public void resetAverageSpeed() {
averageSpeedComputer.resetLocations();
}

@Override
public void copySettings(@NonNull ApplicationMode appMode, @Nullable String customId) {
copySettingsFromMode(appMode, appMode, customId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ public void showContextWidgetMenu(@NonNull View view) {
.setOnClickListener(item -> ConfigureWidgetsFragment.showInstance(mapActivity, widgetInfo.getWidgetPanel(), appMode, widgetId, true))
.create());

List<PopUpMenuItem> widgetActions = getWidgetActions();
if (!Algorithms.isEmpty(widgetActions)) {
items.addAll(widgetActions);
}

WidgetSettingsBaseFragment fragment = widgetType != null ? widgetType.getSettingsFragment(app, widgetInfo) : null;
if (fragment != null) {
items.add(new PopUpMenuItem.Builder(app)
Expand All @@ -229,7 +234,7 @@ public void showContextWidgetMenu(@NonNull View view) {
WidgetSettingsBaseFragment.showFragment(manager, args, null, fragment);
})
.setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_settings_outlined, iconColor))
.showTopDivider(true)
.showTopDivider(Algorithms.isEmpty(widgetActions))
.create());
}

Expand Down Expand Up @@ -261,6 +266,11 @@ public void showContextWidgetMenu(@NonNull View view) {
}
}

@Nullable
protected List<PopUpMenuItem> getWidgetActions() {
return null;
}

@Override
public final void updateInfo(@Nullable OsmandMapLayer.DrawSettings drawSettings) {
boolean shouldHideTopWidgets = (verticalWidget && mapActivity.getWidgetsVisibilityHelper().shouldHideVerticalWidgets());
Expand Down

0 comments on commit 29fe3d7

Please sign in to comment.