Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to invalidate the quest cache #706

Merged
merged 8 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/java/de/westnordost/streetcomplete/Prefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class Prefs

// not shown anywhere directly
public static final String
QUEST_ORDER = "quests.order";
QUEST_ORDER = "quests.order",
QUEST_INVALIDATION = "quests.invalidation";

public enum Autosync
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public int remove(Point tile)
DownloadedTilesTable.Columns.Y + " = ?", whereArgs);
}

public void removeAll()
{
SQLiteDatabase db = dbHelper.getReadableDatabase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getWritableDatabase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

db.execSQL("DELETE FROM " + DownloadedTilesTable.NAME);
}

/** @return a list of quest type names which have already been downloaded in every tile in the
* given tile range */
public List<String> get(Rect tiles, long ignoreOlderThan)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
package de.westnordost.streetcomplete.settings;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
import android.widget.Toast;

import com.mapzen.tangram.LngLat;

import javax.inject.Inject;
import javax.inject.Provider;

import de.westnordost.streetcomplete.ApplicationConstants;
import de.westnordost.streetcomplete.FragmentContainerActivity;
import de.westnordost.streetcomplete.Injector;
import de.westnordost.streetcomplete.IntentListener;
import de.westnordost.streetcomplete.Prefs;
import de.westnordost.streetcomplete.data.tiles.DownloadedTilesDao;
import de.westnordost.streetcomplete.oauth.OAuthPrefs;
import de.westnordost.streetcomplete.R;
import de.westnordost.streetcomplete.oauth.OsmOAuthDialogFragment;
import de.westnordost.streetcomplete.util.SlippyMapMath;
import de.westnordost.streetcomplete.view.dialogs.AlertDialogBuilder;

import static de.westnordost.streetcomplete.tangram.MapFragment.PREF_LAT;
import static de.westnordost.streetcomplete.tangram.MapFragment.PREF_LON;
import static de.westnordost.streetcomplete.tangram.MapFragment.PREF_NAME;
import static de.westnordost.streetcomplete.tangram.TangramConst.toLatLon;

public class SettingsFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener, IntentListener
Expand All @@ -27,6 +41,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
@Inject SharedPreferences prefs;
@Inject OAuthPrefs oAuth;
@Inject Provider<ApplyNoteVisibilityChangedTask> applyNoteVisibilityChangedTask;
@Inject DownloadedTilesDao downloadedTilesDao;

@Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
Expand All @@ -47,6 +62,30 @@ public class SettingsFragment extends PreferenceFragmentCompat
getFragmentActivity().setCurrentFragment(new QuestSelectionFragment());
return true;
});

Preference questsInvalidation = getPreferenceScreen().findPreference("quests.invalidation");
questsInvalidation.setOnPreferenceClickListener(preference ->
{
new AlertDialogBuilder(getContext())
.setMessage(R.string.invalidation_dialog_message)
.setPositiveButton(R.string.invalidate_shown_tile, (dialog, which) -> {
if (getShownTile() != null)
{
downloadedTilesDao.remove(getShownTile());
Toast.makeText(getContext(), R.string.invalidation_success, Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(getContext(), R.string.invalidation_error, Toast.LENGTH_SHORT).show();
}
Copy link
Member

@westnordost westnordost Nov 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave out the success and error messages. This will only confuse the users. Instead, just check if getShownTile() is null before building the dialog and only add that answer option ("Only this tile") if the current shown tile can be determined.

Or, optionally, if possible, show that option but disable it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if the invalidation succeeded, there should really be no success message?

})
.setNeutralButton(R.string.invalidate_whole_cache, (dialog, which) -> {
downloadedTilesDao.removeAll();
Toast.makeText(getContext(), R.string.invalidation_success, Toast.LENGTH_SHORT).show();
})
.setNegativeButton(android.R.string.cancel, (dialog, which) -> {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(dialog, which) -> {} can be null, by the way.

.show();
return true;
});
}

@Override
Expand All @@ -57,6 +96,21 @@ public void onStart()
getActivity().setTitle(R.string.action_settings);
}

private Point getShownTile()
{
SharedPreferences preferences = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
Copy link
Member

@westnordost westnordost Nov 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have the static const here fully qualified, i.e. MapFragment.PREF_NAME etc. because this is the SettingsFragment, everything is settings and prefernces here. It is necessary to know the context of any such preference/settings.

if(preferences.contains(PREF_LAT) && preferences.contains(PREF_LON))
{
LngLat pos = new LngLat(
Double.longBitsToDouble(preferences.getLong(PREF_LON,0)),
Double.longBitsToDouble(preferences.getLong(PREF_LAT,0))
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You create a LngLat here, and then convert the LngLat to a LatLon. Why not simply create a LatLon in the first place? (The default implementation of that interface is OsmLatLon)

return SlippyMapMath.enclosingTile(toLatLon(pos), ApplicationConstants.QUEST_TILE_ZOOM);

}
return null;
}

private void updateOsmAuthSummary()
{
Preference oauth = getPreferenceScreen().findPreference("oauth");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,19 @@ private float meters2Pixels(LngLat at, float meters) {
);
}

private static final String
public static final String
PREF_ROTATION = "map_rotation",
PREF_TILT = "map_tilt",
PREF_ZOOM = "map_zoom",
PREF_LAT = "map_lat",
PREF_LON = "map_lon",
PREF_FOLLOWING = "map_following",
PREF_COMPASS_MODE = "map_compass_mode";
PREF_COMPASS_MODE = "map_compass_mode",
PREF_NAME = "map_settings";

private void restoreMapState()
{
SharedPreferences prefs = getActivity().getPreferences(Activity.MODE_PRIVATE);
SharedPreferences prefs = getActivity().getSharedPreferences(PREF_NAME, Activity.MODE_PRIVATE);

if(prefs.contains(PREF_ROTATION)) controller.setRotation(prefs.getFloat(PREF_ROTATION,0));
if(prefs.contains(PREF_TILT)) controller.setTilt(prefs.getFloat(PREF_TILT,0));
Expand All @@ -559,7 +560,7 @@ private void saveMapState()
{
if(controller == null) return;

SharedPreferences.Editor editor = getActivity().getPreferences(Activity.MODE_PRIVATE).edit();
SharedPreferences.Editor editor = getActivity().getSharedPreferences(PREF_NAME, Activity.MODE_PRIVATE).edit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok,... that's the caveat. Hmm, it's a but ugly that other activities access the preferences of the map this way. But I don't have an idea right now to have a cleaner interface there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh... me too...

editor.putFloat(PREF_ROTATION, controller.getRotation());
editor.putFloat(PREF_TILT, controller.getTilt());
editor.putFloat(PREF_ZOOM, controller.getZoom());
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,11 @@ Otherwise, you can download another keyboard in the app store. Popular keyboards
<string name="quest_housenumber_conscription_number_required">A conscription number must be specified</string>
<string name="quest_cycleway_answer_contraflow_cycleway">Also cycleway on other side</string>
<string name="need_specify_both_sides">You need to specify both sides</string>
<string name="pref_title_quests_invalidation">Invalidate Quest Cache</string>
<string name="pref_title_quests_invalidation_summary">Useful if you think that some quests are outdated</string>
<string name="invalidate_shown_tile">Invalidate shown tile</string>
<string name="invalidate_whole_cache">Invalidate whole cache</string>
<string name="invalidation_success">Invalidation successful</string>
<string name="invalidation_error">Invalidation went wrong</string>
<string name="invalidation_dialog_message">Invalidating the cache causes the quests to be updated next time they are downloaded each. The quest cache is invalidated automatically after one week and immediately when a quest you solved turns out to be already answered by someone else.</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
android:widgetLayout="@layout/widget_image_next"
/>

<Preference
android:key="quests.invalidation"
android:title="@string/pref_title_quests_invalidation"
android:summary="@string/pref_title_quests_invalidation_summary"
/>

</PreferenceCategory>

</PreferenceScreen>