Selaa lähdekoodia

EventArrayAdapter desmelled

Zachary Hatton 9 vuotta sitten
vanhempi
commit
0a4bc0209a

+ 1 - 1
app/src/main/AndroidManifest.xml

@@ -64,7 +64,7 @@
 
         <!-- Create  Activity -->
         <activity
-            android:name=".CreateEventScreen1"
+            android:name=".CreateEventScreen"
             android:parentActivityName=".MainActivity"
             android:theme="@style/AppTheme.NoActionBar"></activity>
 

+ 2 - 3
app/src/main/java/com/example/yiupang/freefoodfinder/Event.java

@@ -13,11 +13,10 @@ class Event
     private String foodType;
     private String description;
     private String place;
-    private double lat;
-    private double lng;
+    private double lat = 0;
+    private double lng = 0;
 
     Event(){
-        //Empty Constructor
     }
 
     Event(String name, String foodType, String description)

+ 0 - 3
app/src/main/java/com/example/yiupang/freefoodfinder/EventArrayAdapter.java

@@ -4,9 +4,6 @@ import android.app.Activity;
 import android.content.Context;
 import android.support.annotation.NonNull;
 
-//CODE SMELL: unused import
-import android.support.v7.widget.RecyclerView;
-
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;

+ 31 - 29
app/src/main/java/com/example/yiupang/freefoodfinder/EventsScreen.java

@@ -37,35 +37,8 @@ public class EventsScreen extends Fragment
         HttpCall httpCall = new HttpCall();
         httpCall.setMethodType(HttpCall.GET);
         httpCall.setUrl("http://free-food-finder.herokuapp.com/events");
-        new HttpRequest(){
-            @Override
-            public void onResponse(Object response, int code)
-            {
-                super.onResponse(response, code);
-                if (code != HttpURLConnection.HTTP_OK)
-                {
-                    Log.d("ON RESPONSE ERROR", "HTTP ERR: NOT OK");
-                }
-                else {
-                    ObjectMapper mapper = new ObjectMapper();
-                    TypeFactory typeFactory = mapper.getTypeFactory();
-                    List<Event> events = null; /*Parse to Event Objs*/
-                    try {
-                        events = mapper.reader(
-                                typeFactory.constructCollectionType(List.class, Event.class)
-                        ).readValue((JsonNode) response);
-                        ListView listView = (ListView) view.findViewById(R.id.events_screen);
-
-                        listView.setAdapter(new EventArrayAdapter(view.getContext(), R.layout.events_list_item, events));
-                        setItemListener(listView);
-                    } catch (IOException e) {
-                        /*handle error*/
-                        Log.d("size:  ", e +"");
-                    }
-
-                }
-            }
-        }.execute(httpCall);
+        HttpRequestSpecial hptr = new HttpRequestSpecial(view);
+        hptr.execute(httpCall);
 
         return view;
     }
@@ -95,4 +68,33 @@ public class EventsScreen extends Fragment
 
         startActivity(details);
     }
+
+    private class HttpRequestSpecial extends HttpRequest {
+        View view;
+
+        public HttpRequestSpecial(View view) {
+            this.view = view;
+        }
+
+        public void onResponse(Object response, int code) {
+            super.onResponse(response, code);
+            if (code != HttpURLConnection.HTTP_OK)
+                Log.d("ON RESPONSE ERROR", "HTTP ERR: NOT OK");
+            else {
+                ObjectMapper mapper = new ObjectMapper();
+                TypeFactory typeFactory = mapper.getTypeFactory();
+                List<Event> events = null; /*Parse to Event Objs*/
+                try {
+                    events = mapper.reader(
+                            typeFactory.constructCollectionType(List.class, Event.class)
+                    ).readValue((JsonNode) response);
+                    ListView listView = (ListView) view.findViewById(R.id.events_screen);
+                    listView.setAdapter(new EventArrayAdapter(view.getContext(), R.layout.events_list_item, events));
+                    setItemListener(listView);
+                } catch (IOException e) {
+                    Log.d("size:  ", e + "");
+                }
+            }
+        }
+    }
 }

+ 15 - 11
app/src/main/java/com/example/yiupang/freefoodfinder/HttpRequest.java

@@ -12,6 +12,8 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 /**
  * Created by yiupang on 5/27/2017.
@@ -33,9 +35,10 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
     protected String doInBackground(HttpCall... params)
     {
         HttpURLConnection urlConnection = null;
-
+        Logger logger = Logger.getLogger(HttpRequest.class.getName());
         try
         {
+
             HttpCall httpCall = params[0];
             URL url;
             OutputStream os;
@@ -72,14 +75,17 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
         }
         catch (Exception e)
         {
-            e.printStackTrace();
+            logger.log(Level.FINE,"context",e);
+            if(urlConnection != null) {
+                urlConnection.disconnect();
+            }
         }
-        finally
+        /* finally
         {
             if(urlConnection != null) {
                 urlConnection.disconnect();
             }
-        }
+        }*/
 
         return response.toString();
     }
@@ -96,18 +102,16 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
         onResponse(response, responseCode);
     }
 
-    /**
-     * It needs to be overwritten by the caller to handle the response
-    * */
+
     public void onResponse(Object response, int code)
     {
-
+         //It needs to be overwritten by the caller to handle the response
     }
 
     /**
      * Create the string for parameters in the URL based on the attributes and data passed by the caller
      * */
-    private String getDataString(Map<String, String> params) throws UnsupportedEncodingException
+    public String getDataString(Map<String, String> params) throws UnsupportedEncodingException
     {
         if (params == null)
         {
@@ -119,9 +123,9 @@ class HttpRequest extends AsyncTask<HttpCall, String, String>
         {
             result.append(isFirst? "?":"&");
             isFirst = false;
-            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
+            result.append(URLEncoder.encode(entry.getKey(), UTF_8));
             result.append("=");
-            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
+            result.append(URLEncoder.encode(entry.getValue(),UTF_8));
         }
         return result.toString();
     }

+ 0 - 1
app/src/main/java/com/example/yiupang/freefoodfinder/MainActivity.java

@@ -7,7 +7,6 @@ import android.support.v4.app.Fragment;
 
 import android.support.v7.app.AppCompatActivity;
 import android.view.Menu;
-import android.view.MenuItem;
 
 import com.roughike.bottombar.BottomBar;
 import com.roughike.bottombar.OnMenuTabClickListener;

+ 4 - 4
app/src/main/java/com/example/yiupang/freefoodfinder/MapScreen.java

@@ -29,10 +29,10 @@ import java.util.ArrayList;
  */
 public class MapScreen extends android.support.v4.app.Fragment implements OnMapReadyCallback
 {
-    ArrayList<Double> lats = new ArrayList<Double>();
-    ArrayList<Double> lngs = new ArrayList<Double>();
-    ArrayList<String> titles = new ArrayList<String>();
-    ArrayList<String> descrips = new ArrayList<String>();
+    ArrayList<Double> lats = new ArrayList<>();
+    ArrayList<Double> lngs = new ArrayList<>();
+    ArrayList<String> titles = new ArrayList<>();
+    ArrayList<String> descrips = new ArrayList<>();
     GoogleMap map;
 
     @Override