Skip to content

Commit

Permalink
Fixes #15: Display username when logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
Teester committed Apr 10, 2018
2 parents 1bd24a3 + f3b871f commit 6d2ab1e
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public class PreferenceList {
public final static String LAST_QUERY_LOCATION_LONGITUDE = "last_query_location_longitude";
public final static String LOCATION_PROVIDER = "location_provider";
public final static String NOT_QUERY_REASON = "reason_not_to_query";
public final static String OSM_USER_NAME = "osm_user_name";
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public void processOAuth() {
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer, verifier);

new UploadToOSM(preferences);

preferences.setStringPreference(PreferenceList.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
preferences.setStringPreference(PreferenceList.OAUTH_TOKEN, consumer.getToken());
preferences.setBooleanPreference(PreferenceList.LOGGED_IN_TO_OSM, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public String getOverpassUri(double latitude, double longitude, float accuracy)
return String.format("https://www.overpass-api.de/api/interpreter?data=[out:json][timeout:25];(%s%s%s);out%%20center%%20meta%%20qt;", node, way, relation);
}

/**
* Querys overpass with a given string and gets json back
*
* @param urlString the url to query
* @return json retrieved from overpass
*/
@Override
public String queryOverpassApi(String urlString) {

Expand All @@ -94,6 +100,11 @@ public String queryOverpassApi(String urlString) {
return resultToDisplay;
}

/**
* Converts the json returned from overpass into usable objects
*
* @param result The json from overpass
*/
@Override
public void processResult(String result) {
if (result != null) {
Expand Down Expand Up @@ -150,6 +161,13 @@ public void processResult(String result) {
}
}

/**
* Entry point to query Overpass
*
* @param latitude Location latitude
* @param longitude Location longitude
* @param accuracy Location accuracy
*/
@Override
public void queryOverpass(double latitude, double longitude, float accuracy) {
SourceContract.Preferences preferences = new Preferences(context);
Expand Down Expand Up @@ -207,6 +225,12 @@ private void prepareNotification() {
}
}

/**
* Checks the room database to see if we've been here before recently
*
* @param osmId the id of the object we're interested in
* @return true if we've been there in the last week
*/
private boolean checkDatabaseForLocation(long osmId) {
SourceContract.Preferences preferences = new Preferences(context);
if (BuildConfig.DEBUG && preferences.getBooleanPreference(PreferenceList.DEBUG_MODE)) {
Expand All @@ -226,6 +250,11 @@ private boolean checkDatabaseForLocation(long osmId) {
return location != null && location.getTimeVisited() > time;
}

/**
* Adds an object to the room database
*
* @param osmObject the object to be added
*/
private void updateDatabase(OsmObject osmObject) {
AppDatabase db = AppDatabase.getAppDatabase(context);
VisitedLocation location = db.visitedLocationDao().findByOsmId(osmObject.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface Overpass {
interface upload {

void uploadToOsm();

void setUsername();
}

interface oAuth {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.westnordost.osmapi.common.errors.OsmAuthorizationException;
import de.westnordost.osmapi.map.MapDataDao;
import de.westnordost.osmapi.map.data.Element;
import de.westnordost.osmapi.user.UserDao;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

Expand Down Expand Up @@ -97,4 +98,13 @@ private Element modifyCurrentElement(Element modifiedElement) {

return modifiedElement;
}

@Override
public void setUsername() {
OsmConnection connection = getConnection();

UserDao userDao = new UserDao(connection);
String name = userDao.getMine().displayName;
preferences.setStringPreference(PreferenceList.OSM_USER_NAME, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ protected void onCreate(Bundle savedInstanceState) {
this.textView = this.findViewById(R.id.textView);
this.button = this.findViewById(R.id.button);
Toolbar toolbar = findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

this.button.setOnClickListener(this);
Expand Down Expand Up @@ -200,8 +199,8 @@ public void toggleDebugMode(boolean state) {
}

@Override
public void showIfLoggedIn(int messageStringId, int buttonStringId) {
textView.setText(getString(messageStringId));
public void showIfLoggedIn(int messageStringId, int buttonStringId, String user) {
textView.setText(String.format(getString(messageStringId), user));
button.setText(getString(buttonStringId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Presenter {

interface View {

void showIfLoggedIn(int message, int button);
void showIfLoggedIn(int message, int button, String user);

void startOAuth();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.teester.whatsnearby.Utilities;
import com.teester.whatsnearby.data.PreferenceList;
import com.teester.whatsnearby.data.source.SourceContract;
import com.teester.whatsnearby.data.source.UploadToOSM;

import java.io.UnsupportedEncodingException;
import java.net.URI;
Expand Down Expand Up @@ -32,14 +33,15 @@ public void showIfLoggedIn() {
boolean loggedIn = preferences.getBooleanPreference(PreferenceList.LOGGED_IN_TO_OSM);
int message;
int button;
String userName = getUserName();
if (loggedIn) {
message = R.string.logged_in_as;
button = R.string.log_out;
} else {
message = R.string.not_logged_in;
button = R.string.authorise_openstreetmap;
}
view.showIfLoggedIn(message, button);
view.showIfLoggedIn(message, button, userName);
}

/**
Expand Down Expand Up @@ -91,4 +93,19 @@ public void toggleDebugMode() {
preferences.setBooleanPreference(preference, true);
}
}

private String getUserName() {
String preference = preferences.getStringPreference(PreferenceList.OSM_USER_NAME);
if ("".equals(preference)) {
new Thread(new Runnable() {
@Override
public void run() {
SourceContract.upload upload = new UploadToOSM(preferences);
upload.setUsername();
}
}).start();
preference = preferences.getStringPreference(PreferenceList.OSM_USER_NAME);
}
return preference;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<string name="none_of_these_places">None of these places</string>
<string name="check_pois_instruction">Click the button below to check for points of interest around your current location.</string>
<string name="check_now_button">Check Now!</string>
<string name="logged_in_as">You are currently logged in to OpenStreetMap.\nWhat\'s Nearby? will notify you when you\'re in a location where there is missing information in OpenStreetMap.</string>
<string name="logged_in_as">You are currently logged in to OpenStreetMap as %s.\nWhat\'s Nearby? will notify you when you\'re in a location where there is missing information in OpenStreetMap.</string>
<string name="log_out">Log Out</string>
<string name="changeset_comment">Added details to %s</string>
<string name="changeset_source" translatable="false">survey</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.URI;
import java.net.URISyntaxException;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
Expand All @@ -27,7 +28,9 @@ public class MainActivityPresenterTest {
private MainActivityContract.Presenter presenter;

@Before
public void setUp() {
public void

setUp() {
presenter = new MainActivityPresenter(view, preferences);
}

Expand All @@ -39,7 +42,7 @@ public void logoutButtonClicked() {
verify(preferences).setStringPreference(eq("oauth_token"), eq(""));
verify(preferences).setStringPreference(eq("oauth_token_secret"), eq(""));
verify(view).startOAuth();
verify(view).showIfLoggedIn(anyInt(), anyInt());
verify(view).showIfLoggedIn(anyInt(), anyInt(), (String) any());
}

@Test
Expand All @@ -54,4 +57,10 @@ public void checkIfInOAuthFlow() {

verify(view).startOAuth();
}

@Test
public void checkIfUsernameIsSetCorrectly() {
presenter.showIfLoggedIn();
verify(view).showIfLoggedIn(anyInt(), anyInt(), (String) any());
}
}

0 comments on commit 6d2ab1e

Please sign in to comment.