Skip to content

Commit

Permalink
Cleanup: inline TrackIconUtils methods.
Browse files Browse the repository at this point in the history
Part of #1608.
  • Loading branch information
dennisguse committed Jun 27, 2023
1 parent b0bbece commit 01c5c54
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import java.util.List;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Speed;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.stats.TrackStatistics;
import de.dennisguse.opentracks.util.TrackIconUtils;

@RunWith(JUnit4.class)
public class AggregatedStatisticsTest {
Expand All @@ -40,7 +40,8 @@ private static Track createTrack(Context context, Distance totalDistance, Durati
statistics.setMinAltitude(1200.0); // Resulting min altitude

Track track = new Track();
track.setIcon(TrackIconUtils.getActivityTypeId(context, category));
track.setIcon(ActivityType.findByLocalizedString(context, category)
.getId());
track.setActivityType(category);
track.setTrackStatistics(statistics);
return track;
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/de/dennisguse/opentracks/TrackEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import de.dennisguse.opentracks.databinding.TrackEditBinding;
import de.dennisguse.opentracks.fragments.ChooseActivityTypeDialogFragment;
import de.dennisguse.opentracks.services.TrackRecordingServiceConnection;
import de.dennisguse.opentracks.util.TrackIconUtils;
import de.dennisguse.opentracks.util.TrackUtils;

/**
Expand Down Expand Up @@ -78,11 +77,16 @@ protected void onCreate(Bundle bundle) {

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, ActivityType.getLocalizedStrings(this));
viewBinding.fields.trackEditActivityType.setAdapter(adapter);
viewBinding.fields.trackEditActivityType.setOnItemClickListener((parent, view, position, id) -> setActivityTypeIcon(TrackIconUtils.getActivityTypeId(this, (String) viewBinding.fields.trackEditActivityType.getAdapter().getItem(position))));
viewBinding.fields.trackEditActivityType.setOnItemClickListener((parent, view, position, id) -> {
String localizedActivityType = (String) viewBinding.fields.trackEditActivityType.getAdapter().getItem(position);
setActivityTypeIcon(ActivityType.findByLocalizedString(this, localizedActivityType)
.getId());
});
viewBinding.fields.trackEditActivityType.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
setActivityTypeIcon(TrackIconUtils.getActivityTypeId(
TrackEditActivity.this, viewBinding.fields.trackEditActivityType.getText().toString()));
String localizedActivityType = viewBinding.fields.trackEditActivityType.getText().toString();
setActivityTypeIcon(ActivityType.findByLocalizedString(this, localizedActivityType)
.getId());
}
});

Expand Down Expand Up @@ -138,12 +142,14 @@ protected View getRootView() {

private void setActivityTypeIcon(String iconValue) {
this.iconValue = iconValue;
viewBinding.fields.trackEditActivityTypeIcon.setImageResource(TrackIconUtils.getIconDrawableId(iconValue));
viewBinding.fields.trackEditActivityTypeIcon.setImageResource(ActivityType.findByActivityTypeId(iconValue)
.getIconDrawableId());
}

@Override
public void onChooseActivityTypeDone(String value) {
setActivityTypeIcon(value);
viewBinding.fields.trackEditActivityType.setText(getString(TrackIconUtils.getIconActivityType(value)));
viewBinding.fields.trackEditActivityType.setText(getString(ActivityType.findByActivityTypeId(value)
.getFirstLocalizedStringId()));
}
}
5 changes: 3 additions & 2 deletions src/main/java/de/dennisguse/opentracks/TrackListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.DistanceFormatter;
import de.dennisguse.opentracks.data.models.Track;
Expand All @@ -72,7 +73,6 @@
import de.dennisguse.opentracks.util.IntentUtils;
import de.dennisguse.opentracks.util.PermissionRequester;
import de.dennisguse.opentracks.util.StringUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;

/**
* An activity displaying a list of tracks.
Expand Down Expand Up @@ -219,7 +219,8 @@ public void bindView(View view, Context context, Cursor cursor) {
Track.Id trackId = new Track.Id(cursor.getLong(idIndex));
boolean isRecording = trackId.equals(recordingStatus.getTrackId());
String icon = cursor.getString(iconIndex);
int iconId = TrackIconUtils.getIconDrawableId(icon);
int iconId = ActivityType.findByActivityTypeId(icon)
.getIconDrawableId();
String name = cursor.getString(nameIndex);
String totalTime = StringUtils.formatElapsedTime(Duration.ofMillis(cursor.getLong(totalTimeIndex)));
String totalDistance = DistanceFormatter.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import de.dennisguse.opentracks.chart.TrackDataHubInterface;
import de.dennisguse.opentracks.data.ContentProviderUtils;
import de.dennisguse.opentracks.data.TrackDataHub;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.databinding.TrackRecordingBinding;
import de.dennisguse.opentracks.fragments.ChooseActivityTypeDialogFragment;
Expand All @@ -46,7 +47,6 @@
import de.dennisguse.opentracks.ui.util.ActivityUtils;
import de.dennisguse.opentracks.util.IntentDashboardUtils;
import de.dennisguse.opentracks.util.IntentUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;
import de.dennisguse.opentracks.util.TrackUtils;

/**
Expand Down Expand Up @@ -319,7 +319,8 @@ public TrackDataHub getTrackDataHub() {
@Override
public void onChooseActivityTypeDone(String iconValue) {
Track track = contentProviderUtils.getTrack(trackId);
String activityType = getString(TrackIconUtils.getIconActivityType(iconValue));
String activityType = getString(ActivityType.findByActivityTypeId(iconValue)
.getFirstLocalizedStringId());
TrackUtils.updateTrack(this, track, null, activityType, null, contentProviderUtils);
}

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/de/dennisguse/opentracks/TrackStoppedActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import de.dennisguse.opentracks.util.ExportUtils;
import de.dennisguse.opentracks.util.IntentUtils;
import de.dennisguse.opentracks.util.StringUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;
import de.dennisguse.opentracks.util.TrackUtils;

public class TrackStoppedActivity extends AbstractTrackDeleteActivity implements ChooseActivityTypeDialogFragment.ChooseActivityTypeCaller {
Expand Down Expand Up @@ -54,11 +53,16 @@ protected void onCreate(Bundle savedInstanceState) {

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, ActivityType.getLocalizedStrings(this));
viewBinding.trackEditActivityType.setAdapter(adapter);
viewBinding.trackEditActivityType.setOnItemClickListener((parent, view, position, id) -> setActivityTypeIcon(TrackIconUtils.getActivityTypeId(this, (String) viewBinding.trackEditActivityType.getAdapter().getItem(position))));
viewBinding.trackEditActivityType.setOnItemClickListener((parent, view, position, id) -> {
String localizedActivityType = (String) viewBinding.trackEditActivityType.getAdapter().getItem(position);
setActivityTypeIcon(ActivityType.findByLocalizedString(this, localizedActivityType)
.getId());
});
viewBinding.trackEditActivityType.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
setActivityTypeIcon(TrackIconUtils.getActivityTypeId(
TrackStoppedActivity.this, viewBinding.trackEditActivityType.getText().toString()));
String localizedActivityType = viewBinding.trackEditActivityType.getText().toString();
setActivityTypeIcon(ActivityType.findByLocalizedString(this, localizedActivityType)
.getId());
}
});

Expand Down Expand Up @@ -126,13 +130,15 @@ protected View getRootView() {
}

private void setActivityTypeIcon(String iconValue) {
viewBinding.trackEditActivityTypeIcon.setImageResource(TrackIconUtils.getIconDrawableId(iconValue));
viewBinding.trackEditActivityTypeIcon.setImageResource(ActivityType.findByActivityTypeId(iconValue)
.getIconDrawableId());
}

@Override
public void onChooseActivityTypeDone(String iconValue) {
setActivityTypeIcon(iconValue);
viewBinding.trackEditActivityType.setText(getString(TrackIconUtils.getIconActivityType(iconValue)));
viewBinding.trackEditActivityType.setText(getString(ActivityType.findByActivityTypeId(iconValue)
.getFirstLocalizedStringId()));
}

private void resumeTrackAndFinish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.databinding.ChooseActivityTypeBinding;
import de.dennisguse.opentracks.util.TrackIconUtils;

/**
* A DialogFragment to choose an activity type.
Expand All @@ -37,7 +36,8 @@ private static int getPosition(Context context, String category) {
if (category == null) {
return -1;
}
String iconValue = TrackIconUtils.getActivityTypeId(context, category);
String iconValue = ActivityType.findByLocalizedString(context, category)
.getId();

return getAllActivityTypeIds().indexOf(iconValue);
}
Expand Down Expand Up @@ -66,7 +66,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,

List<Integer> iconDrawableIds = new ArrayList<>();
for (String iconValue : getAllActivityTypeIds()) {
iconDrawableIds.add(TrackIconUtils.getIconDrawableId(iconValue));
iconDrawableIds.add(ActivityType.findByActivityTypeId(iconValue)
.getIconDrawableId());
}

final ChooseActivityTypeImageAdapter imageAdapter = new ChooseActivityTypeImageAdapter(iconDrawableIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package de.dennisguse.opentracks.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -34,6 +35,7 @@
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.TrackRecordedActivity;
import de.dennisguse.opentracks.data.ContentProviderUtils;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.DistanceFormatter;
import de.dennisguse.opentracks.data.models.SpeedFormatter;
import de.dennisguse.opentracks.data.models.Track;
Expand All @@ -43,7 +45,6 @@
import de.dennisguse.opentracks.stats.SensorStatistics;
import de.dennisguse.opentracks.stats.TrackStatistics;
import de.dennisguse.opentracks.util.StringUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;

/**
* A fragment to display track statistics to the user for a recorded {@link Track}.
Expand Down Expand Up @@ -191,8 +192,12 @@ private void updateUI() {

// Set activity type
{
String trackIconValue = TrackIconUtils.getActivityTypeId(getContext(), track.getActivityType());
viewBinding.statsActivityTypeIcon.setImageDrawable(ContextCompat.getDrawable(getContext(), TrackIconUtils.getIconDrawableId(trackIconValue)));
Context context = getContext();
String localizedActivityType = track.getActivityType();
String trackIconValue = ActivityType.findByLocalizedString(context, localizedActivityType)
.getId();
viewBinding.statsActivityTypeIcon.setImageDrawable(ContextCompat.getDrawable(getContext(), ActivityType.findByActivityTypeId(trackIconValue)
.getIconDrawableId()));
}

// Set time and start datetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.ContentProviderUtils;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Speed;
Expand All @@ -30,7 +31,6 @@
import de.dennisguse.opentracks.ui.markers.MarkerUtils;
import de.dennisguse.opentracks.util.FileUtils;
import de.dennisguse.opentracks.util.LocationUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;

/**
* Handles logic to import:
Expand Down Expand Up @@ -107,7 +107,8 @@ void setTrack(Context context, String name, String uuid, String description, Str
track.setActivityType(activityType);

if (icon == null) {
icon = TrackIconUtils.getActivityTypeId(context, activityType);
icon = ActivityType.findByLocalizedString(context, activityType)
.getId();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.ContentProviderUtils;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Track;
Expand All @@ -24,7 +25,6 @@
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.stats.TrackStatistics;
import de.dennisguse.opentracks.stats.TrackStatisticsUpdater;
import de.dennisguse.opentracks.util.TrackIconUtils;
import de.dennisguse.opentracks.util.TrackNameUtils;

class TrackRecordingManager implements SharedPreferences.OnSharedPreferenceChangeListener {
Expand Down Expand Up @@ -74,7 +74,8 @@ Track.Id startNewTrack(TrackPointCreator trackPointCreator) {

String category = PreferencesUtils.getDefaultActivity();
track.setActivityType(category);
track.setIcon(TrackIconUtils.getActivityTypeId(context, category));
track.setIcon(ActivityType.findByLocalizedString(context, category)
.getId());
track.setTrackStatistics(trackStatisticsUpdater.getTrackStatistics());
//TODO Pass TrackPoint
track.setName(TrackNameUtils.getTrackName(context, trackId, track.getStartTime()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.fragments.ChooseActivityTypeDialogFragment;
import de.dennisguse.opentracks.util.TrackIconUtils;

/**
* For entering the default activity type.
Expand Down Expand Up @@ -83,20 +82,25 @@ protected void onBindDialogView(View view) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, ActivityType.getLocalizedStrings(context));
textView.setAdapter(adapter);
textView.setOnItemClickListener((parent, v, position, id) -> {
String iconValue = TrackIconUtils.getActivityTypeId(context, (String) textView.getAdapter().getItem(position));
String localizedActivityType = (String) textView.getAdapter().getItem(position);
String iconValue = ActivityType.findByLocalizedString(context, localizedActivityType)
.getId();
updateIcon(iconValue);
});
textView.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
String iconValue = TrackIconUtils.getActivityTypeId(context, textView.getText().toString());
String localizedActivityType = textView.getText().toString();
String iconValue = ActivityType.findByLocalizedString(context, localizedActivityType)
.getId();
updateIcon(iconValue);
}
});

iconView = view.findViewById(R.id.activity_type_preference_spinner);
iconView.setOnClickListener((it) -> showIconSelectDialog());

updateIcon(TrackIconUtils.getActivityTypeId(context, category));
updateIcon(ActivityType.findByLocalizedString(context, category)
.getId());
}

private void showIconSelectDialog() {
Expand All @@ -117,12 +121,14 @@ public void onDialogClosed(boolean positiveResult) {

public void updateUI(String iconValue) {
updateIcon(iconValue);
textView.setText(getActivity().getString(TrackIconUtils.getIconActivityType(iconValue)));
textView.setText(getActivity().getString(ActivityType.findByActivityTypeId(iconValue)
.getFirstLocalizedStringId()));
textView.clearFocus();
}

private void updateIcon(String iconValue) {
iconView.setImageResource(TrackIconUtils.getIconDrawableId(iconValue));
iconView.setImageResource(ActivityType.findByActivityTypeId(iconValue)
.getIconDrawableId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.stream.Stream;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.ActivityType;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.DistanceFormatter;
import de.dennisguse.opentracks.data.models.HeartRate;
Expand All @@ -51,7 +52,6 @@
import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayout;
import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayoutIO;
import de.dennisguse.opentracks.util.IntentDashboardUtils;
import de.dennisguse.opentracks.util.TrackIconUtils;

/**
* Utilities to access preferences stored in {@link SharedPreferences}.
Expand Down Expand Up @@ -237,7 +237,8 @@ public static boolean isReportSpeed(String category) {
final String STATS_RATE_DEFAULT = resources.getString(R.string.stats_rate_default);
String currentStatsRate = getString(R.string.stats_rate_key, STATS_RATE_DEFAULT);
if (currentStatsRate.equals(getString(R.string.stats_rate_speed_or_pace_default, STATS_RATE_DEFAULT))) {
return TrackIconUtils.isSpeedIcon(resources, category);
return ActivityType.findByLocalizedString(resources, category)
.isShowSpeedPreferred();
}

return currentStatsRate.equals(resources.getString(R.string.stats_rate_speed));
Expand Down
Loading

0 comments on commit 01c5c54

Please sign in to comment.