Skip to content

Commit

Permalink
Disqus - showing from extenral source, get post id right. #9
Browse files Browse the repository at this point in the history
Changed AccidentManagr to allow only one instance of it.
Add documentation and checks, code enhancement
  • Loading branch information
samuelregev committed Mar 26, 2015
1 parent 786469f commit 42a576e
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

// get saved accident data
Bundle mArgs = getArguments();

Long id = mArgs.getLong("id");
String description = mArgs.getString("description");
String titleBySubType = mArgs.getString("titleBySubType");
Expand All @@ -36,16 +35,19 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
builder.setNeutralButton(getString(R.string.address_not_found_close), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
}
});

// update dialog TextViews with the accident details
TextView tv = (TextView) v.findViewById(R.id.accident_details_desc);
tv.setText(created + "\n" + address + "\n" + description);
TextView tv;
tv = (TextView) v.findViewById(R.id.accident_details_desc);
if (tv != null)
tv.setText(created + "\n" + address + "\n" + description);

tv = (TextView) v.findViewById(R.id.accident_details_title);
tv.setText(titleBySubType);
if (tv != null)
tv.setText(titleBySubType);

return builder.create();
}
Expand Down
54 changes: 51 additions & 3 deletions app/src/main/java/il/co/anyway/app/AccidentsManager.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
package il.co.anyway.app;


import android.util.Log;

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

public class AccidentsManager {

public static final boolean DO_RESET = true;
public static final boolean DO_NOT_RESET = false;

private final String LOG_TAG = AccidentsManager.class.getSimpleName();

private static AccidentsManager instance = null;
private List<Accident> accidentsList;

public AccidentsManager() {
// making the default constructor private make sure there will be only one instance of the accidents manager
private AccidentsManager() {
accidentsList = new ArrayList<>();
}

public static AccidentsManager getInstance() {
if (instance == null)
instance = new AccidentsManager();
return instance;
}

/**
* check if an accident exist in the accident manager
* @param toCheck the Accident object to check
* @return true if exist, false if not or toCheck is null
*/
private boolean isAccidentExist(Accident toCheck) {

if (toCheck == null)
return false;

for (Accident a : accidentsList)
if (a.getId() == toCheck.getId()) {

Expand All @@ -29,8 +44,16 @@ private boolean isAccidentExist(Accident toCheck) {
return false;
}

/**
* Add accident to the list
* @param toAdd the Accident object to add
* @return true if accident added, false if accident already exist, or toAdd is null
*/
public boolean addAccident(Accident toAdd) {

if (toAdd == null)
return false;

if (!isAccidentExist(toAdd)) {
accidentsList.add(toAdd);
//TODO updateMap();
Expand All @@ -40,11 +63,20 @@ public boolean addAccident(Accident toAdd) {
return true;
}

/**
* Add a list of accidents to the list
* @param toAddList the list of Accident objects
* @param reset use to select if you want to reset the list before adding the new list
* @return How many accidents from the list actually taken(duplicate accident will ignore)
*/
public int addAllAccidents(List<Accident> toAddList, boolean reset) {

if (reset == DO_RESET)
accidentsList.clear();

if (toAddList == null)
return 0;

int counter = 0;

for (Accident a : toAddList) {
Expand All @@ -60,8 +92,16 @@ public int addAllAccidents(List<Accident> toAddList, boolean reset) {
return counter;
}

/**
* find a accident by it's marker ID
* @param markerID the marker id
* @return The Accident object if found, null if not found
*/
public Accident getAccidentByMarkerID(String markerID) {

if (markerID == null)
return null;

for (Accident a : accidentsList) {
if (a.getMarkerID().equals(markerID)) {
return a;
Expand All @@ -71,10 +111,18 @@ public Accident getAccidentByMarkerID(String markerID) {
return null;
}

/**
*
* @return the list of all accidents in the list
*/
public List<Accident> getAllAccidents() {
return accidentsList;
}

/**
* Get all the accidents that not on the map
* @return a list of accidents that not on the map
*/
public List<Accident> getAllNewAccidents() {

List<Accident> newAccidents = new ArrayList<>();
Expand Down
105 changes: 75 additions & 30 deletions app/src/main/java/il/co/anyway/app/DisqusActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -14,31 +19,63 @@
import android.os.Build;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.maps.model.LatLng;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Locale;


public class DisqusActivity extends ActionBarActivity {

private static final String DISQUS_ID = "testforanyway";
public static final String DISQUS_LOCATION_ID = "il.co.anyway.app.DISQUS_LOCATION";

private static final String DISQUS_SHORT_NAME = "testforanyway";
private static final String BASE_URL = "https://anywaydisqus.herokuapp.com/";
private static final int PRECISION_LEVEL_OF_LOCATION = 6;

WebView mWebView;
String mDisqusPostID;
String mUrl;
String mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disqus);

mWebView = (WebView)findViewById(R.id.disqus);
mWebView.getSettings().setJavaScriptEnabled(true);

// get the location of the discussion
Intent intent = getIntent();
String location = intent.getStringExtra(DISQUS_LOCATION_ID);
LatLng location = (LatLng)intent.getExtras().get(DISQUS_LOCATION_ID);

showDisqus(location);
// set the id of the discussion
// it's the PRECISION_LEVEL_OF_LOCATION numbers of the latitude and then same of the longitude(without the dot)
mDisqusPostID = Double.toString(location.latitude).substring(0,PRECISION_LEVEL_OF_LOCATION).replace(".", "") +
Double.toString(location.longitude).substring(0,PRECISION_LEVEL_OF_LOCATION).replace(".", "");

// TODO - find what the title of the page should be
mTitle = mDisqusPostID;

// build url of Disqus
Uri builtUri = Uri.parse(BASE_URL).buildUpon()
.appendQueryParameter("diqus_shortname", DISQUS_SHORT_NAME)
.appendQueryParameter("disqus_id", mDisqusPostID)
.appendQueryParameter("title", mTitle)
.build();
mUrl = builtUri.toString();

// get the web view
mWebView = (WebView)findViewById(R.id.disqus);
if(mWebView != null) {
showDisqus();
}
}

@Override
Expand All @@ -56,46 +93,54 @@ public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
if (id == R.id.action_refresh_comments) {
mWebView.loadUrl(mUrl);
return true;
}

return super.onOptionsItemSelected(item);
}

private void showDisqus() {

private void showDisqus(String zone) {
WebSettings webSettings = mWebView.getSettings();

mWebView.getSettings().setJavaScriptEnabled(true);
// enable javascript
webSettings.setJavaScriptEnabled(true);

final Activity activity = this;
mWebView.requestFocusFromTouch();

// the WebChromeClient is for links to be open inside the app and not in another browser
mWebView.setWebChromeClient(new WebChromeClient() {

});

// the WebViewClient is here to solve a bug in the login procedure of Disqus
// when login the page stays show "busy" icon and do nothing.
// here we catch that situation and handle it
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);

if(url.indexOf("logout")>-1 || url.indexOf("disqus.com/next/login-success")>-1 ){
view.loadUrl(mUrl);

}
if(url.indexOf("disqus.com/_ax/twitter/complete")>-1||url.indexOf("disqus.com/_ax/facebook/complete")>-1||url.indexOf("disqus.com/_ax/google/complete")>-1){
view.loadUrl(BASE_URL + "login.php");

}
if(url.indexOf(BASE_URL + "login.php") > -1) {
view.loadUrl(mUrl);
}
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
Log.i("disqus error", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
}
});

String htmlComments = getHtmlComment(zone, DISQUS_ID);
mWebView.loadDataWithBaseURL("http://" + DISQUS_ID + ".disqus.com/", htmlComments, "text/html", "UTF-8", "");

}

private String getHtmlComment(String idPost, String shortName) {

return "<html><head></head><body><div id='disqus_thread'></div></body>"
+ "<script type='text/javascript'>"
+ "var disqus_identifier = '"
+ idPost
+ "';"
+ "var disqus_shortname = '"
+ shortName
+ "';"
+ " (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;"
+ "dsq.src = '/embed.js';"
+ "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();"
+ "</script></html>";
mWebView.loadUrl(mUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

// Start activity
// Start location settings activity
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);

Expand All @@ -27,6 +27,7 @@ public void onClick(DialogInterface dialog, int id) {
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
dialog.cancel();
}
});
// Create the AlertDialog object and return it
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/il/co/anyway/app/FetchAccidents.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ protected List<Accident> doInBackground(String... params) {

try {
// Construct the URL for the Anyway accidents query
final String FORECAST_BASE_URL = "http://www.anyway.co.il/markers?";
final String ANYWAY_BASE_URL = "http://www.anyway.co.il/markers?";

Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
Uri builtUri = Uri.parse(ANYWAY_BASE_URL).buildUpon()
.appendQueryParameter("ne_lat", params[Utility.JSON_STRING_NE_LAT])
.appendQueryParameter("ne_lng", params[Utility.JSON_STRING_NE_LNG])
.appendQueryParameter("sw_lat", params[Utility.JSON_STRING_SW_LAT])
Expand Down Expand Up @@ -134,6 +134,10 @@ protected void onPostExecute(List<Accident> accidents) {
}
}

/**
* set the calling activity, this activity will be called when the accidents fetching ends
* @param callingActivity the calling activity (MainActivity instance)
*/
public void setCallingActivity(MainActivity callingActivity) {
this.callingActivity = callingActivity;
}
Expand Down
16 changes: 3 additions & 13 deletions app/src/main/java/il/co/anyway/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
public class MainActivity extends ActionBarActivity implements OnInfoWindowClickListener,
OnMapLongClickListener, OnCameraChangeListener, LocationListener {



@SuppressWarnings("unused")
private final String LOG_TAG = MainActivity.class.getSimpleName();

Expand All @@ -61,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAccidentsManager = new AccidentsManager();
mAccidentsManager = AccidentsManager.getInstance();

// first run set to true only when this is the first time onCreate called
// used to handle the case of screen rotation
Expand Down Expand Up @@ -160,16 +158,8 @@ public void onInfoWindowClick(Marker marker) {
@Override
public void onMapLongClick(LatLng latLng) {

double latitude = latLng.latitude;
double longitude = latLng.longitude;

String disqusID = Double.toString(latitude).substring(0,5) + "|"
+ Double.toString(longitude).substring(0,5);

Toast.makeText(this, "disqusID:" + disqusID, Toast.LENGTH_LONG).show();

Intent disqusIntent = new Intent(this, DisqusActivity.class);
disqusIntent.putExtra(DisqusActivity.DISQUS_LOCATION_ID, disqusID);
disqusIntent.putExtra(DisqusActivity.DISQUS_LOCATION_ID, latLng);
startActivity(disqusIntent);
}

Expand Down Expand Up @@ -327,7 +317,7 @@ private void setUpMap(boolean firstRun) {
// Disable toolbar on the right bottom corner(taking user to google maps app)
mMap.getUiSettings().setMapToolbarEnabled(false);

mMap.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter(getLayoutInflater()));
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnCameraChangeListener(this);
Expand Down
Loading

0 comments on commit 42a576e

Please sign in to comment.