Просмотр исходного кода

using fragment onAttach instead of consumer setters

develop
Robin Thoni 8 лет назад
Родитель
Сommit
ab510cdcbf

+ 7
- 7
app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java Просмотреть файл

@@ -49,6 +49,7 @@ import com.rthoni.stssaguenay.dbo.UserDbo;
49 49
 import com.rthoni.stssaguenay.dbo.UsersSettingsDbo;
50 50
 import com.rthoni.stssaguenay.ui.fragments.HomeFragment;
51 51
 import com.rthoni.stssaguenay.ui.fragments.StopViewerFragment;
52
+import com.rthoni.stssaguenay.ui.interfaces.OnFavouriteStopClicked;
52 53
 
53 54
 import org.json.JSONObject;
54 55
 
@@ -57,7 +58,7 @@ import java.util.List;
57 58
 import butterknife.BindView;
58 59
 import butterknife.ButterKnife;
59 60
 
60
-public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
61
+public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnFavouriteStopClicked {
61 62
 
62 63
     public static int ADD_FAVOURITE_STOP_REQUEST_CODE = 1;
63 64
     public static int SEARCH_STOP_REQUEST_CODE = 2;
@@ -216,12 +217,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
216 217
     {
217 218
         _fab.setVisibility(View.VISIBLE);
218 219
         HomeFragment f = new HomeFragment();
219
-        f.setOnFavouriteStopClicked(new LuConsumer<UserFavouriteStopsDbo>() {
220
-            @Override
221
-            public void execute(UserFavouriteStopsDbo data) {
222
-                goToStop(data.getStop());
223
-            }
224
-        });
225 220
         getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commit();
226 221
     }
227 222
 
@@ -463,4 +458,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
463 458
             }
464 459
         });
465 460
     }
461
+
462
+    @Override
463
+    public void onFavouriteStopClicked(UserFavouriteStopsDbo favouriteStopsDbo) {
464
+        goToStop(favouriteStopsDbo.getStop());
465
+    }
466 466
 }

+ 18
- 20
app/src/main/java/com/rthoni/stssaguenay/ui/activities/StopPickerActivity.java Просмотреть файл

@@ -16,8 +16,10 @@ import com.rthoni.stssaguenay.dbo.StopsDbo;
16 16
 import com.rthoni.stssaguenay.ui.fragments.StopListPickerFragment;
17 17
 import com.rthoni.stssaguenay.ui.fragments.StopMapPickerFragment;
18 18
 import com.rthoni.stssaguenay.ui.fragments.StopRoutesPickerFragment;
19
+import com.rthoni.stssaguenay.ui.interfaces.OnRoutesSelected;
20
+import com.rthoni.stssaguenay.ui.interfaces.OnStopClicked;
19 21
 
20
-public class StopPickerActivity extends AppCompatActivity {
22
+public class StopPickerActivity extends AppCompatActivity implements OnStopClicked, OnRoutesSelected {
21 23
 
22 24
     public static String MAP_EXTRA_NAME = "MAP_EXTRA";
23 25
 
@@ -61,20 +63,8 @@ public class StopPickerActivity extends AppCompatActivity {
61 63
 
62 64
     public void goToStops()
63 65
     {
64
-        LuConsumer<StopsDbo> consumer = new LuConsumer<StopsDbo>() {
65
-            @Override
66
-            public void execute(StopsDbo data) {
67
-                if (_selectRoutes) {
68
-                    goToRoutes(data);
69
-                }
70
-                else {
71
-                    saveStop(data);
72
-                }
73
-            }
74
-        };
75 66
         if (_map) {
76 67
             StopMapPickerFragment f = new StopMapPickerFragment();
77
-            f.setOnStopSelectedConsumer(consumer);
78 68
             getSupportFragmentManager()
79 69
                     .beginTransaction()
80 70
                     .replace(R.id.container, f)
@@ -82,7 +72,6 @@ public class StopPickerActivity extends AppCompatActivity {
82 72
         }
83 73
         else {
84 74
             StopListPickerFragment f = new StopListPickerFragment();
85
-            f.setOnStopSelectedConsumer(consumer);
86 75
             getSupportFragmentManager()
87 76
                     .beginTransaction()
88 77
                     .replace(R.id.container, f)
@@ -94,12 +83,6 @@ public class StopPickerActivity extends AppCompatActivity {
94 83
     {
95 84
         StopRoutesPickerFragment f = new StopRoutesPickerFragment();
96 85
         f.setStopsDbo(stopDbo);
97
-        f.setOnRoutesSelected(new LuConsumer<UserFavouriteStopsDbo>() {
98
-            @Override
99
-            public void execute(UserFavouriteStopsDbo data) {
100
-                saveFavouriteStop(data);
101
-            }
102
-        });
103 86
         getSupportFragmentManager()
104 87
                 .beginTransaction()
105 88
                 .replace(R.id.container, f)
@@ -122,4 +105,19 @@ public class StopPickerActivity extends AppCompatActivity {
122 105
         setResult(Activity.RESULT_OK, result);
123 106
         finish();
124 107
     }
108
+
109
+    @Override
110
+    public void onStopSlicker(StopsDbo stopsDbo) {
111
+        if (_selectRoutes) {
112
+            goToRoutes(stopsDbo);
113
+        }
114
+        else {
115
+            saveStop(stopsDbo);
116
+        }
117
+    }
118
+
119
+    @Override
120
+    public void onRoutesSelected(UserFavouriteStopsDbo favouriteStopsDbo) {
121
+        saveFavouriteStop(favouriteStopsDbo);
122
+    }
125 123
 }

+ 13
- 5
app/src/main/java/com/rthoni/stssaguenay/ui/fragments/HomeFragment.java Просмотреть файл

@@ -1,5 +1,7 @@
1 1
 package com.rthoni.stssaguenay.ui.fragments;
2 2
 
3
+import android.app.Activity;
4
+import android.content.Context;
3 5
 import android.content.DialogInterface;
4 6
 import android.os.Bundle;
5 7
 import android.support.annotation.Nullable;
@@ -23,6 +25,7 @@ import com.rthoni.stssaguenay.business.SchedulesBusiness;
23 25
 import com.rthoni.stssaguenay.dbo.StopsDbo;
24 26
 import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
25 27
 import com.rthoni.stssaguenay.dbo.SchedulesDbo;
28
+import com.rthoni.stssaguenay.ui.interfaces.OnFavouriteStopClicked;
26 29
 
27 30
 import org.joda.time.DateTimeZone;
28 31
 import org.joda.time.LocalDateTime;
@@ -52,7 +55,7 @@ public class HomeFragment extends Fragment {
52 55
 
53 56
     private FavouriteStopsAdapter _adapter;
54 57
 
55
-    private LuConsumer<UserFavouriteStopsDbo> _onFavouriteStopClicked;
58
+    private OnFavouriteStopClicked _onFavouriteStopClicked;
56 59
 
57 60
     public static class ViewHolder extends RecyclerView.ViewHolder {
58 61
         public View _parentView;
@@ -172,6 +175,14 @@ public class HomeFragment extends Fragment {
172 175
         return v;
173 176
     }
174 177
 
178
+    @Override
179
+    public void onAttach(Context context) {
180
+        super.onAttach(context);
181
+        if (context instanceof OnFavouriteStopClicked) {
182
+            _onFavouriteStopClicked = (OnFavouriteStopClicked) context;
183
+        }
184
+    }
185
+
175 186
     public void setRefreshing(final boolean refreshing)
176 187
     {
177 188
         _swipeRefreshLayout.post(new Runnable() {
@@ -193,7 +204,7 @@ public class HomeFragment extends Fragment {
193 204
     public void onFavouriteClicked(UserFavouriteStopsDbo favouriteStopDbo)
194 205
     {
195 206
         if (_onFavouriteStopClicked != null) {
196
-            _onFavouriteStopClicked.execute(favouriteStopDbo);
207
+            _onFavouriteStopClicked.onFavouriteStopClicked(favouriteStopDbo);
197 208
         }
198 209
     }
199 210
 
@@ -250,7 +261,4 @@ public class HomeFragment extends Fragment {
250 261
                 });
251 262
     }
252 263
 
253
-    public void setOnFavouriteStopClicked(LuConsumer<UserFavouriteStopsDbo> onFavouriteStopClicked) {
254
-        _onFavouriteStopClicked = onFavouriteStopClicked;
255
-    }
256 264
 }

+ 13
- 6
app/src/main/java/com/rthoni/stssaguenay/ui/fragments/StopListPickerFragment.java Просмотреть файл

@@ -1,6 +1,7 @@
1 1
 package com.rthoni.stssaguenay.ui.fragments;
2 2
 
3 3
 import android.app.ProgressDialog;
4
+import android.content.Context;
4 5
 import android.os.Bundle;
5 6
 import android.os.Handler;
6 7
 import android.os.Looper;
@@ -27,6 +28,8 @@ import com.rthoni.stssaguenay.business.STSBusiness;
27 28
 import com.rthoni.stssaguenay.business.StopsBusiness;
28 29
 import com.rthoni.stssaguenay.business.Utils;
29 30
 import com.rthoni.stssaguenay.dbo.StopsDbo;
31
+import com.rthoni.stssaguenay.ui.activities.StopPickerActivity;
32
+import com.rthoni.stssaguenay.ui.interfaces.OnStopClicked;
30 33
 
31 34
 import java.util.List;
32 35
 import java.util.TimerTask;
@@ -48,7 +51,7 @@ public class StopListPickerFragment extends Fragment {
48 51
 
49 52
     private StopsAdapter _stopsAdapter;
50 53
 
51
-    private LuConsumer<StopsDbo> _onStopSelectedConsumer;
54
+    private OnStopClicked _onStopSelectedConsumer;
52 55
 
53 56
     public static class ViewHolder extends RecyclerView.ViewHolder {
54 57
         public TextView _textView;
@@ -170,6 +173,14 @@ public class StopListPickerFragment extends Fragment {
170 173
         return v;
171 174
     }
172 175
 
176
+    @Override
177
+    public void onAttach(Context context) {
178
+        super.onAttach(context);
179
+        if (context instanceof OnStopClicked) {
180
+            _onStopSelectedConsumer = (OnStopClicked) context;
181
+        }
182
+    }
183
+
173 184
     public void loadStops()
174 185
     {
175 186
         final ProgressDialog progressDialog = new ProgressDialog(getContext());
@@ -199,11 +210,7 @@ public class StopListPickerFragment extends Fragment {
199 210
     public void onStopSelected(StopsDbo stopsDbo)
200 211
     {
201 212
         if (_onStopSelectedConsumer != null) {
202
-            _onStopSelectedConsumer.execute(stopsDbo);
213
+            _onStopSelectedConsumer.onStopSlicker(stopsDbo);
203 214
         }
204 215
     }
205
-
206
-    public void setOnStopSelectedConsumer(LuConsumer<StopsDbo> onStopSelectedConsumer) {
207
-        _onStopSelectedConsumer = onStopSelectedConsumer;
208
-    }
209 216
 }

+ 12
- 6
app/src/main/java/com/rthoni/stssaguenay/ui/fragments/StopMapPickerFragment.java Просмотреть файл

@@ -31,6 +31,8 @@ import com.rthoni.stssaguenay.R;
31 31
 import com.rthoni.stssaguenay.business.STSBusiness;
32 32
 import com.rthoni.stssaguenay.business.StopsBusiness;
33 33
 import com.rthoni.stssaguenay.dbo.StopsDbo;
34
+import com.rthoni.stssaguenay.ui.activities.StopPickerActivity;
35
+import com.rthoni.stssaguenay.ui.interfaces.OnStopClicked;
34 36
 
35 37
 import java.util.HashMap;
36 38
 import java.util.List;
@@ -92,7 +94,7 @@ public class StopMapPickerFragment extends Fragment {
92 94
     @BindView(R.id.mapView)
93 95
     MapView _mapView;
94 96
 
95
-    private LuConsumer<StopsDbo> _onStopSelectedConsumer;
97
+    private OnStopClicked _onStopSelectedConsumer;
96 98
 
97 99
     private GoogleMap _map;
98 100
 
@@ -162,6 +164,14 @@ public class StopMapPickerFragment extends Fragment {
162 164
         return v;
163 165
     }
164 166
 
167
+    @Override
168
+    public void onAttach(Context context) {
169
+        super.onAttach(context);
170
+        if (context instanceof OnStopClicked) {
171
+            _onStopSelectedConsumer = (OnStopClicked) context;
172
+        }
173
+    }
174
+
165 175
     @Override
166 176
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
167 177
         if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
@@ -245,11 +255,7 @@ public class StopMapPickerFragment extends Fragment {
245 255
     public void onStopSelected(StopsDbo stopsDbo)
246 256
     {
247 257
         if (_onStopSelectedConsumer != null) {
248
-            _onStopSelectedConsumer.execute(stopsDbo);
258
+            _onStopSelectedConsumer.onStopSlicker(stopsDbo);
249 259
         }
250 260
     }
251
-
252
-    public void setOnStopSelectedConsumer(LuConsumer<StopsDbo> onStopSelectedConsumer) {
253
-        _onStopSelectedConsumer = onStopSelectedConsumer;
254
-    }
255 261
 }

+ 12
- 6
app/src/main/java/com/rthoni/stssaguenay/ui/fragments/StopRoutesPickerFragment.java Просмотреть файл

@@ -1,6 +1,7 @@
1 1
 package com.rthoni.stssaguenay.ui.fragments;
2 2
 
3 3
 import android.app.ProgressDialog;
4
+import android.content.Context;
4 5
 import android.graphics.Color;
5 6
 import android.os.Bundle;
6 7
 import android.support.annotation.Nullable;
@@ -25,6 +26,7 @@ import com.rthoni.stssaguenay.business.STSBusiness;
25 26
 import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
26 27
 import com.rthoni.stssaguenay.dbo.RoutesDbo;
27 28
 import com.rthoni.stssaguenay.dbo.StopsDbo;
29
+import com.rthoni.stssaguenay.ui.interfaces.OnRoutesSelected;
28 30
 
29 31
 import java.util.List;
30 32
 import java.util.Vector;
@@ -50,7 +52,7 @@ public class StopRoutesPickerFragment extends Fragment {
50 52
 
51 53
     private RoutesAdapter _routesAdapter;
52 54
 
53
-    private LuConsumer<UserFavouriteStopsDbo> _onRoutesSelected;
55
+    private OnRoutesSelected _onRoutesSelected;
54 56
 
55 57
     public static class ViewHolder extends RecyclerView.ViewHolder {
56 58
         public ImageView _imageView;
@@ -145,6 +147,14 @@ public class StopRoutesPickerFragment extends Fragment {
145 147
         return v;
146 148
     }
147 149
 
150
+    @Override
151
+    public void onAttach(Context context) {
152
+        super.onAttach(context);
153
+        if (context instanceof OnRoutesSelected) {
154
+            _onRoutesSelected = (OnRoutesSelected) context;
155
+        }
156
+    }
157
+
148 158
     public void setStopsDbo(StopsDbo stopsDbo) {
149 159
         _stopsDbo = stopsDbo;
150 160
     }
@@ -182,11 +192,7 @@ public class StopRoutesPickerFragment extends Fragment {
182 192
             UserFavouriteStopsDbo favourite = new UserFavouriteStopsDbo();
183 193
             favourite.setStop(_stopsDbo);
184 194
             favourite.setRoutes(_routesAdapter.getSelectRoutes());
185
-            _onRoutesSelected.execute(favourite);
195
+            _onRoutesSelected.onRoutesSelected(favourite);
186 196
         }
187 197
     }
188
-
189
-    public void setOnRoutesSelected(LuConsumer<UserFavouriteStopsDbo> onRoutesSelected) {
190
-        _onRoutesSelected = onRoutesSelected;
191
-    }
192 198
 }

+ 11
- 0
app/src/main/java/com/rthoni/stssaguenay/ui/interfaces/OnFavouriteStopClicked.java Просмотреть файл

@@ -0,0 +1,11 @@
1
+package com.rthoni.stssaguenay.ui.interfaces;
2
+
3
+import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
4
+
5
+/**
6
+ * Created by robin on 12/10/16.
7
+ */
8
+
9
+public interface OnFavouriteStopClicked {
10
+    void onFavouriteStopClicked(UserFavouriteStopsDbo favouriteStopsDbo);
11
+}

+ 11
- 0
app/src/main/java/com/rthoni/stssaguenay/ui/interfaces/OnRoutesSelected.java Просмотреть файл

@@ -0,0 +1,11 @@
1
+package com.rthoni.stssaguenay.ui.interfaces;
2
+
3
+import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
4
+
5
+/**
6
+ * Created by robin on 12/10/16.
7
+ */
8
+
9
+public interface OnRoutesSelected {
10
+    void onRoutesSelected(UserFavouriteStopsDbo favouriteStopsDbo);
11
+}

+ 11
- 0
app/src/main/java/com/rthoni/stssaguenay/ui/interfaces/OnStopClicked.java Просмотреть файл

@@ -0,0 +1,11 @@
1
+package com.rthoni.stssaguenay.ui.interfaces;
2
+
3
+import com.rthoni.stssaguenay.dbo.StopsDbo;
4
+
5
+/**
6
+ * Created by robin on 12/10/16.
7
+ */
8
+
9
+public interface OnStopClicked {
10
+    void onStopSlicker(StopsDbo stopsDbo);
11
+}

Загрузка…
Отмена
Сохранить