Browse Source

add serach stop

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
9a450e9d6b

+ 72
- 19
app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java View File

@@ -9,7 +9,6 @@ import android.support.design.widget.NavigationView;
9 9
 import android.support.multidex.MultiDex;
10 10
 import android.support.v4.app.FragmentManager;
11 11
 import android.support.v4.widget.DrawerLayout;
12
-import android.support.v7.app.ActionBar;
13 12
 import android.support.v7.app.ActionBarDrawerToggle;
14 13
 import android.support.v7.app.AlertDialog;
15 14
 import android.support.v7.app.AppCompatActivity;
@@ -60,8 +59,7 @@ import butterknife.ButterKnife;
60 59
 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
61 60
 
62 61
     public static int ADD_FAVOURITE_STOP_REQUEST_CODE = 1;
63
-
64
-    private boolean _goToHome = false;
62
+    public static int SEARCH_STOP_REQUEST_CODE = 2;
65 63
 
66 64
     @BindView(R.id.fab)
67 65
     FloatingActionMenu _fab;
@@ -85,10 +83,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
85 83
 
86 84
     private CallbackManager _callbackManager;
87 85
 
88
-    private ActionBar _actionBar;
89
-
90 86
     private FragmentManager _fragmentManager;
91 87
 
88
+    private DrawerLayout _drawer;
89
+
90
+    private Runnable _onResumeAction;
91
+
92 92
     @Override
93 93
     protected void onCreate(Bundle savedInstanceState) {
94 94
         super.onCreate(savedInstanceState);
@@ -108,10 +108,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
108 108
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
109 109
         setSupportActionBar(toolbar);
110 110
 
111
-        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
111
+        _drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
112 112
         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
113
-                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
114
-        drawer.setDrawerListener(toggle);
113
+                this, _drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
114
+        _drawer.addDrawerListener(toggle);
115 115
         toggle.syncState();
116 116
 
117 117
         _navigationView.setNavigationItemSelectedListener(this);
@@ -184,8 +184,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
184 184
             }
185 185
         });
186 186
 
187
-        _actionBar = getSupportActionBar();
188
-
189 187
         updateLoginState();
190 188
         goToHome();
191 189
     }
@@ -207,9 +205,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
207 205
 
208 206
     @Override
209 207
     protected void onResume() {
210
-        if (_goToHome) {
211
-            _goToHome = false;
212
-            goToHome();
208
+        if (_onResumeAction != null) {
209
+            _onResumeAction.run();
213 210
         }
214 211
         super.onResume();
215 212
     }
@@ -241,17 +238,48 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
241 238
 
242 239
     public void goToAddStop(boolean map)
243 240
     {
241
+        _fab.close(true);
244 242
         Intent intent = new Intent(this, StopPickerActivity.class);
245 243
         intent.putExtra(StopPickerActivity.MAP_EXTRA_NAME, map);
246 244
         startActivityForResult(intent, ADD_FAVOURITE_STOP_REQUEST_CODE);
247 245
     }
248 246
 
247
+    public void goToSearchStop(boolean map)
248
+    {
249
+        _fab.close(true);
250
+        Intent intent = new Intent(this, StopPickerActivity.class);
251
+        intent.putExtra(StopPickerActivity.MAP_EXTRA_NAME, map);
252
+        intent.putExtra(StopPickerActivity.SELECT_ROUTES_EXTRA_NAME, false);
253
+        startActivityForResult(intent, SEARCH_STOP_REQUEST_CODE);
254
+    }
255
+
256
+    public void goToSearchStop()
257
+    {
258
+        final int item[] = new int[1];
259
+        AlertDialog.Builder builder = new AlertDialog.Builder(this);
260
+        String[] array = new String[]{getString(R.string.stop_add_map), getString(R.string.stop_add_list)};
261
+        builder.setSingleChoiceItems(array, 0, new DialogInterface.OnClickListener() {
262
+            @Override
263
+            public void onClick(DialogInterface dialog, int which) {
264
+                item[0] = which;
265
+            }
266
+        });
267
+        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
268
+            @Override
269
+            public void onClick(DialogInterface dialog, int which) {
270
+                dialog.dismiss();
271
+                goToSearchStop(item[0] == 0);
272
+            }
273
+        });
274
+        builder.show();
275
+    }
276
+
249 277
     @Override
250 278
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
251 279
         if (requestCode == ADD_FAVOURITE_STOP_REQUEST_CODE && resultCode == RESULT_OK) {
252 280
             UserFavouriteStopsDbo favouriteStopDbo = new UserFavouriteStopsDbo();
253 281
             try {
254
-                JSONObject obj = new JSONObject(data.getStringExtra(StopPickerActivity.STOP_EXTRA_NAME));
282
+                JSONObject obj = new JSONObject(data.getStringExtra(StopPickerActivity.FAVOURITE_STOP_EXTRA_NAME));
255 283
                 favouriteStopDbo.fromJson(obj);
256 284
             } catch (JSONException e) {
257 285
                 e.printStackTrace();
@@ -260,7 +288,28 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
260 288
             List<UserFavouriteStopsDbo> favouriteStopDbos = STSBusiness.getFavouriteStops(this);
261 289
             favouriteStopDbos.add(favouriteStopDbo);
262 290
             STSBusiness.setFavouriteStops(this, favouriteStopDbos);
263
-            _goToHome = true;
291
+            _onResumeAction = new Runnable() {
292
+                @Override
293
+                public void run() {
294
+                    goToHome();
295
+                }
296
+            };
297
+        }
298
+        else if (requestCode == SEARCH_STOP_REQUEST_CODE && resultCode == RESULT_OK) {
299
+            final StopsDbo stopDbo = new StopsDbo();
300
+            try {
301
+                JSONObject obj = new JSONObject(data.getStringExtra(StopPickerActivity.STOP_EXTRA_NAME));
302
+                stopDbo.fromJson(obj);
303
+            } catch (JSONException e) {
304
+                e.printStackTrace();
305
+                return;
306
+            }
307
+            _onResumeAction = new Runnable() {
308
+                @Override
309
+                public void run() {
310
+                    goToStop(stopDbo);
311
+                }
312
+            };
264 313
         }
265 314
         else {
266 315
             _callbackManager.onActivityResult(requestCode, resultCode, data);
@@ -270,7 +319,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
270 319
 
271 320
     @Override
272 321
     public boolean onNavigationItemSelected(MenuItem item) {
273
-        if (item.getItemId() == R.id.nav_restore) {
322
+        if (item.getItemId() == R.id.nav_search_stop) {
323
+            goToSearchStop();
324
+        }
325
+        else if (item.getItemId() == R.id.nav_restore) {
274 326
             restoreSettings();
275 327
         }
276 328
         else if (item.getItemId() == R.id.nav_backup) {
@@ -284,6 +336,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
284 336
         else {
285 337
             return false;
286 338
         }
339
+        _drawer.closeDrawers();
287 340
         return true;
288 341
     }
289 342
 
@@ -292,9 +345,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
292 345
         UserDbo user = STSBusiness.getLoggedUser(this);
293 346
         _btnLogin.setVisibility(user == null ? View.VISIBLE : View.GONE);
294 347
         Menu menu = _navigationView.getMenu();
295
-        for (int i = 0; i < menu.size(); i++) {
296
-            menu.getItem(i).setVisible(user != null);
297
-        }
348
+        menu.findItem(R.id.nav_restore).setVisible(user != null);
349
+        menu.findItem(R.id.nav_backup).setVisible(user != null);
350
+        menu.findItem(R.id.nav_logout).setVisible(user != null);
298 351
         _textUserName.setText(user == null ? "" : user.getName());
299 352
         _textUserName.setVisibility(user == null ? View.GONE : View.VISIBLE);
300 353
         _textUserEmail.setText(user == null ? "" : user.getEmail());

+ 31
- 15
app/src/main/java/com/rthoni/stssaguenay/ui/activities/StopPickerActivity.java View File

@@ -20,16 +20,23 @@ public class StopPickerActivity extends AppCompatActivity {
20 20
 
21 21
     public static String MAP_EXTRA_NAME = "MAP_EXTRA";
22 22
 
23
+    public static String SELECT_ROUTES_EXTRA_NAME = "SELECT_ROUTES_EXTRA";
24
+
25
+    public static String FAVOURITE_STOP_EXTRA_NAME = "FAVOURITE_STOP_EXTRA";
26
+
23 27
     public static String STOP_EXTRA_NAME = "STOP_EXTRA";
24 28
 
25 29
     private boolean _map;
26 30
 
31
+    private boolean _selectRoutes;
32
+
27 33
     @Override
28 34
     protected void onCreate(Bundle savedInstanceState) {
29 35
         super.onCreate(savedInstanceState);
30 36
         setContentView(R.layout.activity_stop_picker);
31 37
 
32 38
         _map = getIntent().getBooleanExtra(MAP_EXTRA_NAME, false);
39
+        _selectRoutes = getIntent().getBooleanExtra(SELECT_ROUTES_EXTRA_NAME, true);
33 40
 
34 41
         MobileAds.initialize(getApplicationContext(), getString(R.string.ad_init));
35 42
         AdView mAdView = (AdView) findViewById(R.id.adView);
@@ -53,14 +60,20 @@ public class StopPickerActivity extends AppCompatActivity {
53 60
 
54 61
     public void goToStops()
55 62
     {
56
-        if (_map) {
57
-            StopMapPickerFragment f = new StopMapPickerFragment();
58
-            f.setOnStopSelectedConsumer(new LuPromise.LuConsumer<StopsDbo>() {
59
-                @Override
60
-                public void execute(StopsDbo data) {
63
+        LuPromise.LuConsumer<StopsDbo> consumer = new LuPromise.LuConsumer<StopsDbo>() {
64
+            @Override
65
+            public void execute(StopsDbo data) {
66
+                if (_selectRoutes) {
61 67
                     goToRoutes(data);
62 68
                 }
63
-            });
69
+                else {
70
+                    saveStop(data);
71
+                }
72
+            }
73
+        };
74
+        if (_map) {
75
+            StopMapPickerFragment f = new StopMapPickerFragment();
76
+            f.setOnStopSelectedConsumer(consumer);
64 77
             getSupportFragmentManager()
65 78
                     .beginTransaction()
66 79
                     .replace(R.id.container, f)
@@ -68,12 +81,7 @@ public class StopPickerActivity extends AppCompatActivity {
68 81
         }
69 82
         else {
70 83
             StopListPickerFragment f = new StopListPickerFragment();
71
-            f.setOnStopSelectedConsumer(new LuPromise.LuConsumer<StopsDbo>() {
72
-                @Override
73
-                public void execute(StopsDbo data) {
74
-                    goToRoutes(data);
75
-                }
76
-            });
84
+            f.setOnStopSelectedConsumer(consumer);
77 85
             getSupportFragmentManager()
78 86
                     .beginTransaction()
79 87
                     .replace(R.id.container, f)
@@ -88,7 +96,7 @@ public class StopPickerActivity extends AppCompatActivity {
88 96
         f.setOnRoutesSelected(new LuPromise.LuConsumer<UserFavouriteStopsDbo>() {
89 97
             @Override
90 98
             public void execute(UserFavouriteStopsDbo data) {
91
-                saveStop(data);
99
+                saveFavouriteStop(data);
92 100
             }
93 101
         });
94 102
         getSupportFragmentManager()
@@ -98,10 +106,18 @@ public class StopPickerActivity extends AppCompatActivity {
98 106
                 .commit();
99 107
     }
100 108
 
101
-    public void saveStop(UserFavouriteStopsDbo favouriteStopDbo)
109
+    public void saveStop(StopsDbo stopDbo)
110
+    {
111
+        Intent result = new Intent();
112
+        result.putExtra(STOP_EXTRA_NAME, stopDbo.toString());
113
+        setResult(Activity.RESULT_OK, result);
114
+        finish();
115
+    }
116
+
117
+    public void saveFavouriteStop(UserFavouriteStopsDbo favouriteStopDbo)
102 118
     {
103 119
         Intent result = new Intent();
104
-        result.putExtra(STOP_EXTRA_NAME, favouriteStopDbo.toString());
120
+        result.putExtra(FAVOURITE_STOP_EXTRA_NAME, favouriteStopDbo.toString());
105 121
         setResult(Activity.RESULT_OK, result);
106 122
         finish();
107 123
     }

+ 9
- 0
app/src/main/res/drawable/ic_directions_bus_white_24dp.xml View File

@@ -0,0 +1,9 @@
1
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
2
+        android:width="24dp"
3
+        android:height="24dp"
4
+        android:viewportWidth="24.0"
5
+        android:viewportHeight="24.0">
6
+    <path
7
+        android:fillColor="#e9e9e9"
8
+        android:pathData="M4,16c0,0.88 0.39,1.67 1,2.22L5,20c0,0.55 0.45,1 1,1h1c0.55,0 1,-0.45 1,-1v-1h8v1c0,0.55 0.45,1 1,1h1c0.55,0 1,-0.45 1,-1v-1.78c0.61,-0.55 1,-1.34 1,-2.22L20,6c0,-3.5 -3.58,-4 -8,-4s-8,0.5 -8,4v10zM7.5,17c-0.83,0 -1.5,-0.67 -1.5,-1.5S6.67,14 7.5,14s1.5,0.67 1.5,1.5S8.33,17 7.5,17zM16.5,17c-0.83,0 -1.5,-0.67 -1.5,-1.5s0.67,-1.5 1.5,-1.5 1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5zM18,11L6,11L6,6h12v5z"/>
9
+</vector>

+ 7
- 2
app/src/main/res/menu/activity_main_drawer.xml View File

@@ -1,14 +1,19 @@
1 1
 <?xml version="1.0" encoding="utf-8"?>
2 2
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
3 3
 
4
+        <item
5
+            android:id="@+id/nav_search_stop"
6
+            android:icon="@drawable/ic_directions_bus_white_24dp"
7
+            android:title="@string/search_stop" />
8
+
4 9
         <item
5 10
             android:id="@+id/nav_restore"
6 11
             android:icon="@drawable/ic_archive_white_24dp"
7
-            android:title="@string/restore" />
12
+            android:title="@string/restore_settings" />
8 13
         <item
9 14
             android:id="@+id/nav_backup"
10 15
             android:icon="@drawable/ic_unarchive_white_24dp"
11
-            android:title="@string/backup" />
16
+            android:title="@string/backup_settings" />
12 17
         <item
13 18
             android:id="@+id/nav_logout"
14 19
             android:icon="@drawable/ic_open_in_new_white_24dp"

+ 4
- 4
app/src/main/res/values/strings.xml View File

@@ -20,8 +20,8 @@
20 20
     <string name="error_try_again">Error. Please try again.</string>
21 21
 
22 22
     <string name="stop_add">Add a favourite stop</string>
23
-    <string name="stop_add_map">Add from map</string>
24
-    <string name="stop_add_list">Add from list</string>
23
+    <string name="stop_add_map">Select from map</string>
24
+    <string name="stop_add_list">Select from list</string>
25 25
 
26 26
     <string name="loading">Loading&#8230;</string>
27 27
     <string name="loading_schedules">Loading schedules&#8230;</string>
@@ -34,9 +34,9 @@
34 34
 
35 35
     <string name="drawer_help_message">Log in to access more features</string>
36 36
 
37
+    <string name="restore_settings">Restore settings</string>
38
+    <string name="backup_settings">Backup settings</string>
37 39
     <string name="logout">Logout</string>
38
-    <string name="restore">Restore settings</string>
39
-    <string name="backup">Backup settings</string>
40 40
 
41 41
     <string name="encryption_password">Encryption password</string>
42 42
     <string name="encryption_password_encrypt">Please enter an encryption password (you do not need to enter the same as a previous backup):</string>

+ 1
- 1
build.gradle View File

@@ -9,7 +9,7 @@ buildscript {
9 9
         }
10 10
     }
11 11
     dependencies {
12
-        classpath 'com.android.tools.build:gradle:2.2.0'
12
+        classpath 'com.android.tools.build:gradle:2.2.2'
13 13
         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
14 14
         classpath 'com.google.gms:google-services:3.0.0'
15 15
 

Loading…
Cancel
Save