Browse Source

get stop routes

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
519cd51e9a

+ 56
- 0
app/src/main/java/com/rthoni/stssaguenay/business/RoutesBusiness.java View File

@@ -0,0 +1,56 @@
1
+package com.rthoni.stssaguenay.business;
2
+
3
+import com.luticate.utils.business.LuPromise;
4
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
5
+import com.rthoni.stssaguenay.dataaccess.RoutesDataAccess;
6
+import com.rthoni.stssaguenay.dbo.RoutesDbo;
7
+
8
+import java.util.List;
9
+import java.util.Vector;
10
+
11
+/**
12
+ * Created by robin on 9/30/16.
13
+ */
14
+
15
+public class RoutesBusiness {
16
+
17
+    private static List<RoutesDbo> _routesDbos = null;
18
+
19
+    public static LuPromise<List<RoutesDbo>> getAll(LuDataAccessConfigDbo config)
20
+    {
21
+        final LuPromise<List<RoutesDbo>> promise = new LuPromise<>();
22
+        if (_routesDbos == null) {
23
+            RoutesDataAccess.getAll(config).then(new LuPromise.LuConsumer<List<RoutesDbo>>() {
24
+                @Override
25
+                public void execute(List<RoutesDbo> data) {
26
+                    _routesDbos = data;
27
+                    promise.resolve(data);
28
+                }
29
+            }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
30
+                @Override
31
+                public void execute(LuPromise.LuPromiseError data) {
32
+                    promise.reject(data);
33
+                }
34
+            });
35
+        }
36
+        else {
37
+            promise.resolve(_routesDbos);
38
+        }
39
+        return promise;
40
+    }
41
+
42
+    public static List<RoutesDbo> getRoutesDbos(List<RoutesDbo> routesDbos, List<String> routes)
43
+    {
44
+        List<RoutesDbo> filteredRoutes = new Vector<>();
45
+
46
+        for (RoutesDbo routeDbo : routesDbos) {//TODO invert loops and sort Routes
47
+            for (String route : routes) {
48
+                if (routeDbo.getId().equals(route)) {
49
+                    filteredRoutes.add(routeDbo);
50
+                }
51
+            }
52
+        }
53
+
54
+        return filteredRoutes;
55
+    }
56
+}

+ 18
- 0
app/src/main/java/com/rthoni/stssaguenay/dataaccess/RoutesDataAccess.java View File

@@ -1,8 +1,26 @@
1 1
 package com.rthoni.stssaguenay.dataaccess;
2 2
 
3
+import com.luticate.utils.business.LuPromise;
4
+import com.luticate.utils.business.LuRequest;
5
+import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6
+import com.rthoni.stssaguenay.dbo.RoutesDbo;
7
+
8
+import java.util.List;
9
+
3 10
 /**
4 11
  * Created by robin on 9/30/16.
5 12
  */
6 13
 
7 14
 public class RoutesDataAccess {
15
+
16
+    public static LuPromise<List<RoutesDbo>> getAll(LuDataAccessConfigDbo config)
17
+    {
18
+        return LuRequest.get(config, RoutesDbo.PaginatedRoutesDbo.class, "routes")
19
+                .map(new LuPromise.LuConverter<RoutesDbo.PaginatedRoutesDbo, List<RoutesDbo>>() {
20
+                    @Override
21
+                    public List<RoutesDbo> convert(RoutesDbo.PaginatedRoutesDbo data) {
22
+                        return data.getData();
23
+                    }
24
+                });
25
+    }
8 26
 }

+ 0
- 1
app/src/main/java/com/rthoni/stssaguenay/dataaccess/StopsDataAccess.java View File

@@ -6,7 +6,6 @@ import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6 6
 import com.rthoni.stssaguenay.dbo.StopsDbo;
7 7
 
8 8
 import java.util.List;
9
-import java.util.Locale;
10 9
 
11 10
 /**
12 11
  * Created by robin on 9/29/16.

+ 78
- 0
app/src/main/java/com/rthoni/stssaguenay/dbo/RoutesDbo.java View File

@@ -0,0 +1,78 @@
1
+package com.rthoni.stssaguenay.dbo;
2
+
3
+import com.luticate.utils.dbo.LuDbo;
4
+import com.luticate.utils.dbo.LuPaginatedDbo;
5
+
6
+import org.json.JSONException;
7
+import org.json.JSONObject;
8
+
9
+import java.util.HashMap;
10
+
11
+/**
12
+ * Created by robin on 9/30/16.
13
+ */
14
+
15
+public class RoutesDbo extends LuDbo {
16
+
17
+    public static class PaginatedRoutesDbo extends LuPaginatedDbo<RoutesDbo>
18
+    {
19
+    }
20
+
21
+    protected String _id;
22
+
23
+    protected String _name;
24
+
25
+    protected String _bgColor;
26
+
27
+    protected String _fgColor;
28
+
29
+    @Override
30
+    public void fromJson(JSONObject json) throws JSONException {
31
+        _id = json.getString("id");
32
+        _name = json.getString("name");
33
+        _bgColor = json.getString("bgColor");
34
+        _fgColor = json.getString("fgColor");
35
+    }
36
+
37
+    @Override
38
+    public HashMap<String, Object> toArray() {
39
+        return null;
40
+    }
41
+
42
+    public String getId() {
43
+        return _id;
44
+    }
45
+
46
+    public void setId(String id) {
47
+        _id = id;
48
+    }
49
+
50
+    public String getFullName()
51
+    {
52
+        return String.format("%s - %s", _id, _name);
53
+    }
54
+
55
+    public String getName() {
56
+        return _name;
57
+    }
58
+
59
+    public void setName(String name) {
60
+        _name = name;
61
+    }
62
+
63
+    public String getBgColor() {
64
+        return _bgColor;
65
+    }
66
+
67
+    public void setBgColor(String bgColor) {
68
+        _bgColor = bgColor;
69
+    }
70
+
71
+    public String getFgColor() {
72
+        return _fgColor;
73
+    }
74
+
75
+    public void setFgColor(String fgColor) {
76
+        _fgColor = fgColor;
77
+    }
78
+}

+ 0
- 4
app/src/main/java/com/rthoni/stssaguenay/ui/fragments/StopPickerFragment.java View File

@@ -92,10 +92,6 @@ public class StopPickerFragment extends Fragment {
92 92
             getFilter().filter(query);
93 93
         }
94 94
 
95
-        public List<StopsDbo> getStops() {
96
-            return _allStops;
97
-        }
98
-
99 95
         @Override
100 96
         public Filter getFilter() {
101 97
             return new Filter() {

+ 85
- 0
app/src/main/java/com/rthoni/stssaguenay/ui/fragments/StopRoutesPickerFragment.java View File

@@ -1,17 +1,27 @@
1 1
 package com.rthoni.stssaguenay.ui.fragments;
2 2
 
3
+import android.app.ProgressDialog;
3 4
 import android.os.Bundle;
4 5
 import android.support.annotation.Nullable;
5 6
 import android.support.v4.app.Fragment;
7
+import android.support.v7.widget.LinearLayoutManager;
6 8
 import android.support.v7.widget.RecyclerView;
7 9
 import android.view.LayoutInflater;
8 10
 import android.view.View;
9 11
 import android.view.ViewGroup;
10 12
 import android.widget.TextView;
13
+import android.widget.Toast;
11 14
 
15
+import com.luticate.utils.business.LuPromise;
12 16
 import com.rthoni.stssaguenay.R;
17
+import com.rthoni.stssaguenay.business.RoutesBusiness;
18
+import com.rthoni.stssaguenay.business.STSBusiness;
19
+import com.rthoni.stssaguenay.dbo.RoutesDbo;
13 20
 import com.rthoni.stssaguenay.dbo.StopsDbo;
14 21
 
22
+import java.util.List;
23
+import java.util.Vector;
24
+
15 25
 import butterknife.BindView;
16 26
 import butterknife.ButterKnife;
17 27
 
@@ -28,6 +38,51 @@ public class StopRoutesPickerFragment extends Fragment {
28 38
 
29 39
     private StopsDbo _stopsDbo;
30 40
 
41
+    private RoutesAdapter _routesAdapter;
42
+
43
+    public static class ViewHolder extends RecyclerView.ViewHolder {
44
+        public TextView _textView;
45
+        public ViewHolder(TextView v) {
46
+            super(v);
47
+            _textView = v;
48
+        }
49
+    }
50
+
51
+    public class RoutesAdapter extends RecyclerView.Adapter<ViewHolder> {
52
+
53
+        private List<RoutesDbo> _routes;
54
+
55
+        @Override
56
+        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
57
+            View v = LayoutInflater.from(parent.getContext())
58
+                    .inflate(R.layout.simple_recycler_view_item, parent, false);
59
+            ViewHolder vh = new ViewHolder((TextView)v);
60
+            return vh;
61
+        }
62
+
63
+        @Override
64
+        public void onBindViewHolder(ViewHolder holder, int position) {
65
+            final RoutesDbo routeDbo = _routes.get(position);
66
+            holder._textView.setText(routeDbo.getFullName());
67
+            holder._textView.setOnClickListener(new View.OnClickListener() {
68
+                @Override
69
+                public void onClick(View v) {
70
+//                    onStopSelected(routeDbo);
71
+                }
72
+            });
73
+        }
74
+
75
+        @Override
76
+        public int getItemCount() {
77
+            return _routes == null ? 0 : _routes.size();
78
+        }
79
+
80
+        public void setRoutes(List<RoutesDbo> routes) {
81
+            _routes = routes;
82
+            notifyDataSetChanged();
83
+        }
84
+    }
85
+
31 86
     @Nullable
32 87
     @Override
33 88
     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -36,6 +91,12 @@ public class StopRoutesPickerFragment extends Fragment {
36 91
 
37 92
         _textView.setText(getResources().getString(R.string.pick_routes, _stopsDbo.getFullName()));
38 93
 
94
+        _routesAdapter = new RoutesAdapter();
95
+        _listRoutes.setAdapter(_routesAdapter);
96
+        _listRoutes.setLayoutManager(new LinearLayoutManager(getContext()));
97
+
98
+        loadRoutes();
99
+
39 100
         return v;
40 101
     }
41 102
 
@@ -46,4 +107,28 @@ public class StopRoutesPickerFragment extends Fragment {
46 107
     public void setStopsDbo(StopsDbo stopsDbo) {
47 108
         _stopsDbo = stopsDbo;
48 109
     }
110
+
111
+    public void loadRoutes()
112
+    {
113
+        final ProgressDialog progressDialog = new ProgressDialog(getContext());
114
+        progressDialog.setIndeterminate(true);
115
+        progressDialog.show();
116
+
117
+        RoutesBusiness.getAll(STSBusiness.getConfig())
118
+                .then(new LuPromise.LuConsumer<List<RoutesDbo>>() {
119
+                    @Override
120
+                    public void execute(List<RoutesDbo> data) {
121
+                        progressDialog.dismiss();
122
+                        List<RoutesDbo> routes = RoutesBusiness.getRoutesDbos(data, _stopsDbo.getRoutes());
123
+                        _routesAdapter.setRoutes(routes);
124
+                    }
125
+                }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
126
+                    @Override
127
+                    public void execute(LuPromise.LuPromiseError data) {
128
+                        progressDialog.dismiss();
129
+                        _routesAdapter.setRoutes(new Vector<RoutesDbo>());
130
+                        Toast.makeText(getContext(), data.getError(), Toast.LENGTH_LONG).show();
131
+                    }
132
+                });
133
+    }
49 134
 }

Loading…
Cancel
Save