ソースを参照

Made comments wherever a code smell was. Fixed some code smells. Updated Event class.

Zachary Hatton 9 年 前
コミット
08e96f4b67

+ 2 - 0
app/src/main/java/com/example/yiupang/freefoodfinder/AboutUsScreen.java

@@ -1,6 +1,8 @@
 package com.example.yiupang.freefoodfinder;
 
+//CODE SMELL: Unused import
 import android.app.Fragment;
+
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;

+ 26 - 10
app/src/main/java/com/example/yiupang/freefoodfinder/Event.java

@@ -4,19 +4,39 @@ import java.util.Date;
 
 public class Event
 {
-    public long eventId;
-    public long userId;
+    private long eventId;
+    private long userId;
     private String name;
-    public Date time;
+    private Date time;
     private String foodType;
     private String description;
     private double latitude;
     private double longitude;
 
+    public Event(){
+        //Empty Constructor
+    }
+
+    public Event(String name, String foodType, String description)
+    {
+        this.name = name;
+        this.foodType = foodType;
+        this.description = description;
+    }
+
     public String getName() {
         return name;
     }
 
+    public long getEventId() { return eventId;}
+    public void setEventId(long id) { eventId = id; }
+
+    public long getUserId(){ return userId; }
+    public void setUserId(long id){ userId = id; }
+
+    public Date getTime(){ return time; }
+    public void setTime(Date d){ time = d; }
+
     public boolean setName(String name) {
         if (name.length() > 3 && name.length() < 31) {
             this.name = name;
@@ -59,7 +79,7 @@ public class Event
     }
 
     public boolean setLongitude(double longitude){
-        if(latitude >= -90.0 && latitude <= 90.0){
+        if(longitude >= -180.0 && longitude <= 180.0){
             this.longitude = longitude;
             return true;
         }
@@ -69,14 +89,10 @@ public class Event
     }
 
     public String getDescription(){return this.description;}
+    public void setDescription(String desc){description = desc;}
+
 
 
-    public Event(String name, String foodType, String description)
-    {
-        this.name = name;
-        this.foodType = foodType;
-        this.description = description;
-    }
 }
 
 

+ 23 - 14
app/src/main/java/com/example/yiupang/freefoodfinder/EventArrayAdapter.java

@@ -3,7 +3,10 @@ package com.example.yiupang.freefoodfinder;
 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;
@@ -26,9 +29,13 @@ public class EventArrayAdapter extends ArrayAdapter<Event>
     }
 
     private Context context;
-    private int layoutResourceId;
     List<Event> events;
 
+    // CODE SMELL: unused private field
+    private int layoutResourceId;
+
+
+
     public EventArrayAdapter(Context context, int layoutResourceId, List<Event> events)
     {
         super(context, layoutResourceId, events);
@@ -49,28 +56,30 @@ public class EventArrayAdapter extends ArrayAdapter<Event>
     @Override
     public View getView(int position, View convertView, ViewGroup parent)
     {
-        ViewHolder holder = null;
+        ViewHolder holder;
         LayoutInflater inflater = ((Activity) context).getLayoutInflater();
+        View newConvertView = convertView;
 
-        if(convertView == null) {
-            convertView = inflater.inflate(R.layout.events_list_item, parent, false);
+        if(newConvertView == null) {
+            newConvertView = inflater.inflate(R.layout.events_list_item, parent, false);
             holder = new ViewHolder();
 
-            holder.name = (TextView) convertView.findViewById(R.id.name);
-            holder.type = (TextView) convertView.findViewById(R.id.type);
-            holder.desc = (TextView) convertView.findViewById(R.id.desc);
+            holder.name = (TextView) newConvertView.findViewById(R.id.name);
+            holder.type = (TextView) newConvertView.findViewById(R.id.type);
+            holder.desc = (TextView) newConvertView.findViewById(R.id.desc);
 
-            Event row_pos = events.get(position);
+            Event rowPos = events.get(position);
 
-            holder.name.setText(row_pos.getName());
-            holder.type.setText(row_pos.getFoodType());
-            holder.desc.setText(row_pos.getDescription());
+            holder.name.setText(rowPos.getName());
+            holder.type.setText(rowPos.getFoodType());
+            holder.desc.setText(rowPos.getDescription());
 
-            convertView.setTag(holder);
+            newConvertView.setTag(holder);
         } else {
-            holder = (ViewHolder) convertView.getTag();
+            // CODE SMELL AND BUG: Useless Assignment
+            holder = (ViewHolder) newConvertView.getTag();
         }
 
-        return convertView;
+        return newConvertView;
     }
 }

+ 7 - 3
app/src/main/java/com/example/yiupang/freefoodfinder/EventsScreen.java

@@ -5,10 +5,14 @@ import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+
+//CODE SMELL: unused import
 import android.widget.ArrayAdapter;
+
 import android.widget.ListView;
 import java.util.ArrayList;
 
+//CODE SMELL: unised import
 import java.lang.reflect.Array;
 
 /**
@@ -25,9 +29,9 @@ public class EventsScreen extends android.support.v4.app.Fragment
         View view = inflater.inflate(R.layout.events_screen, container, false);
         ArrayList<Event> dataArray = new ArrayList();
         dataArray.add(new Event("Free Food 1", "Pizza", "Come !!"));
-        dataArray.add(new Event("Free Food 5", "GodPizza", "Come Again !!"));
-        dataArray.add(new Event("Free Food 4", "GodPizza", "Come Again !!"));
-        dataArray.add(new Event("Free Food 8", "GodPizza", "Come Again !!"));
+        dataArray.add(new Event("Free Food 5", "GoodPizza", "Come Again!"));
+        dataArray.add(new Event("Free Food 4", "NicePizza", "Come Again"));
+        dataArray.add(new Event("Free Food 8", "SuperPizza", "Come Again!!"));
 
         EventArrayAdapter adapter = new EventArrayAdapter(view.getContext(), R.layout.events_list_item, dataArray);
         ListView listView = (ListView) view.findViewById(R.id.events_screen);

+ 27 - 13
app/src/main/java/com/example/yiupang/freefoodfinder/MainActivity.java

@@ -2,21 +2,25 @@ package com.example.yiupang.freefoodfinder;
 
 import android.os.Bundle;
 import android.support.annotation.IdRes;
+import android.support.v7.app.ActionBarActivity;
+
+//CODE SMELLS: following 4 imports are unused
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
-import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
+
 import android.view.View;
-import com.roughike.bottombar.BottomBar;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.TextView;
 
 import com.roughike.bottombar.BottomBar;
-import com.roughike.bottombar.BottomBarBadge;
 import com.roughike.bottombar.OnMenuTabClickListener;
 
+//CODE SEMLL: unused import
+import com.roughike.bottombar.BottomBarBadge;
+
 import com.facebook.CallbackManager;
 import com.facebook.FacebookSdk;
 import com.facebook.login.widget.LoginButton;
@@ -24,17 +28,23 @@ import com.facebook.login.LoginResult;
 import com.facebook.FacebookCallback;
 import com.facebook.FacebookException;
 
+
+
 public class MainActivity extends ActionBarActivity
 {
+    static final String RED = "ff0000";
     BottomBar bottomBar;
-    private TextView info;
+
+    //CODE SMELL: next 3 lines of comments should be removeed
+    //private TextView info;
     //private LoginButton loginButton;
-    private CallbackManager callbackManager;
+    //private CallbackManager callbackManager;
 
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
+        //CODE SMELL: Remove comment block
 /*        FacebookSdk.sdkInitialize(getApplicationContext());
         callbackManager = CallbackManager.Factory.create();
         info = (TextView) findViewById(R.id.info);
@@ -60,6 +70,8 @@ public class MainActivity extends ActionBarActivity
         });*/
         setContentView(R.layout.activity_main);
         bottomBar = BottomBar.attach(this, savedInstanceState);
+
+        //CODE SMELL: It's complicated
         bottomBar.setItemsFromMenu(R.menu.menu_main, new OnMenuTabClickListener()
         {
             @Override
@@ -90,14 +102,14 @@ public class MainActivity extends ActionBarActivity
             @Override
             public void onMenuTabReSelected(@IdRes int menuItemId)
             {
-
+                //CODE SMELL: This method is empty
             }
         });
 
-        bottomBar.mapColorForTab(0, "#ff0000");
-        bottomBar.mapColorForTab(1, "#ff0000");
-        bottomBar.mapColorForTab(2, "#ff0000");
-        bottomBar.mapColorForTab(3, "#ff0000");
+        bottomBar.mapColorForTab(0, RED);
+        bottomBar.mapColorForTab(1, RED);
+        bottomBar.mapColorForTab(2, RED);
+        bottomBar.mapColorForTab(3, RED);
     }
 
     @Override
@@ -112,15 +124,17 @@ public class MainActivity extends ActionBarActivity
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
+
+        //CODE SMELL AND BUG: useless assignment
         int id = item.getItemId();
 
-/*
+        //CODE SMELL: remove comment block
+        /*
         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
             return true;
         }
-*/
-
+        */
         return super.onOptionsItemSelected(item);
     }
 }