|
@@ -3,10 +3,26 @@ package com.rthoni.stssaguenay.ui.activities;
|
3
|
3
|
import android.content.Intent;
|
4
|
4
|
import android.os.Bundle;
|
5
|
5
|
import android.support.design.widget.FloatingActionButton;
|
|
6
|
+import android.support.design.widget.NavigationView;
|
|
7
|
+import android.support.v4.widget.DrawerLayout;
|
|
8
|
+import android.support.v7.app.ActionBarDrawerToggle;
|
6
|
9
|
import android.support.v7.app.AppCompatActivity;
|
7
|
10
|
import android.support.v7.widget.Toolbar;
|
|
11
|
+import android.view.Menu;
|
|
12
|
+import android.view.MenuItem;
|
8
|
13
|
import android.view.View;
|
9
|
|
-
|
|
14
|
+import android.widget.TextView;
|
|
15
|
+import android.widget.Toast;
|
|
16
|
+
|
|
17
|
+import com.facebook.CallbackManager;
|
|
18
|
+import com.facebook.FacebookCallback;
|
|
19
|
+import com.facebook.FacebookException;
|
|
20
|
+import com.facebook.FacebookSdk;
|
|
21
|
+import com.facebook.GraphRequest;
|
|
22
|
+import com.facebook.GraphResponse;
|
|
23
|
+import com.facebook.login.LoginManager;
|
|
24
|
+import com.facebook.login.LoginResult;
|
|
25
|
+import com.facebook.login.widget.LoginButton;
|
10
|
26
|
import com.google.android.gms.ads.AdRequest;
|
11
|
27
|
import com.google.android.gms.ads.AdView;
|
12
|
28
|
import com.google.android.gms.ads.MobileAds;
|
|
@@ -14,6 +30,7 @@ import com.luticate.utils.business.LuRequest;
|
14
|
30
|
import com.rthoni.stssaguenay.R;
|
15
|
31
|
import com.rthoni.stssaguenay.business.STSBusiness;
|
16
|
32
|
import com.rthoni.stssaguenay.dbo.FavouriteStopDbo;
|
|
33
|
+import com.rthoni.stssaguenay.dbo.UserDbo;
|
17
|
34
|
import com.rthoni.stssaguenay.ui.fragments.HomeFragment;
|
18
|
35
|
|
19
|
36
|
import org.json.JSONException;
|
|
@@ -24,7 +41,7 @@ import java.util.List;
|
24
|
41
|
import butterknife.BindView;
|
25
|
42
|
import butterknife.ButterKnife;
|
26
|
43
|
|
27
|
|
-public class MainActivity extends AppCompatActivity {
|
|
44
|
+public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
|
28
|
45
|
|
29
|
46
|
public static int ADD_FAVOURITE_STOP_REQUEST_CODE = 1;
|
30
|
47
|
|
|
@@ -33,12 +50,25 @@ public class MainActivity extends AppCompatActivity {
|
33
|
50
|
@BindView(R.id.fab)
|
34
|
51
|
FloatingActionButton _fab;
|
35
|
52
|
|
|
53
|
+ @BindView(R.id.nav_view)
|
|
54
|
+ NavigationView _navigationView;
|
|
55
|
+
|
|
56
|
+ private LoginButton _btnLogin;
|
|
57
|
+
|
|
58
|
+ private TextView _textUserName;
|
|
59
|
+
|
|
60
|
+ private TextView _textUserEmail;
|
|
61
|
+
|
|
62
|
+ private CallbackManager _callbackManager;
|
|
63
|
+
|
36
|
64
|
@Override
|
37
|
65
|
protected void onCreate(Bundle savedInstanceState) {
|
38
|
66
|
super.onCreate(savedInstanceState);
|
39
|
67
|
|
40
|
68
|
LuRequest.init(this);
|
41
|
69
|
|
|
70
|
+ FacebookSdk.sdkInitialize(getApplicationContext());
|
|
71
|
+
|
42
|
72
|
setContentView(R.layout.activity_main);
|
43
|
73
|
ButterKnife.bind(this);
|
44
|
74
|
|
|
@@ -50,6 +80,18 @@ public class MainActivity extends AppCompatActivity {
|
50
|
80
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
51
|
81
|
setSupportActionBar(toolbar);
|
52
|
82
|
|
|
83
|
+ DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
|
|
84
|
+ ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
|
|
85
|
+ this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
|
|
86
|
+ drawer.setDrawerListener(toggle);
|
|
87
|
+ toggle.syncState();
|
|
88
|
+
|
|
89
|
+ _navigationView.setNavigationItemSelectedListener(this);
|
|
90
|
+
|
|
91
|
+ View headerView = _navigationView.getHeaderView(0);
|
|
92
|
+ _textUserName = ButterKnife.findById(headerView, R.id.textUserName);
|
|
93
|
+ _textUserEmail = ButterKnife.findById(headerView, R.id.textUserEmail);
|
|
94
|
+
|
53
|
95
|
_fab.setOnClickListener(new View.OnClickListener() {
|
54
|
96
|
@Override
|
55
|
97
|
public void onClick(View view) {
|
|
@@ -57,6 +99,45 @@ public class MainActivity extends AppCompatActivity {
|
57
|
99
|
}
|
58
|
100
|
});
|
59
|
101
|
|
|
102
|
+ _callbackManager = CallbackManager.Factory.create();
|
|
103
|
+ _btnLogin = ButterKnife.findById(headerView, R.id.btnLogin);
|
|
104
|
+ _btnLogin.setReadPermissions("email");
|
|
105
|
+ _btnLogin.registerCallback(_callbackManager, new FacebookCallback<LoginResult>() {
|
|
106
|
+ @Override
|
|
107
|
+ public void onSuccess(LoginResult loginResult) {
|
|
108
|
+ GraphRequest request = GraphRequest.newMeRequest(
|
|
109
|
+ loginResult.getAccessToken(),
|
|
110
|
+ new GraphRequest.GraphJSONObjectCallback() {
|
|
111
|
+ @Override
|
|
112
|
+ public void onCompleted(JSONObject object, GraphResponse response) {
|
|
113
|
+ try {
|
|
114
|
+ UserDbo userDbo = new UserDbo();
|
|
115
|
+ userDbo.fromJson(object);
|
|
116
|
+ onUserLogged(userDbo);
|
|
117
|
+ } catch (JSONException e) {
|
|
118
|
+ e.printStackTrace();
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+ });
|
|
122
|
+ Bundle parameters = new Bundle();
|
|
123
|
+ parameters.putString("fields", "id,email,name");
|
|
124
|
+ request.setParameters(parameters);
|
|
125
|
+ request.executeAsync();
|
|
126
|
+ loginResult.getAccessToken();
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ @Override
|
|
130
|
+ public void onCancel() {
|
|
131
|
+
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Override
|
|
135
|
+ public void onError(FacebookException error) {
|
|
136
|
+ Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
|
|
137
|
+ }
|
|
138
|
+ });
|
|
139
|
+
|
|
140
|
+ updateLoginState();
|
60
|
141
|
goToHome();
|
61
|
142
|
}
|
62
|
143
|
|
|
@@ -104,28 +185,39 @@ public class MainActivity extends AppCompatActivity {
|
104
|
185
|
STSBusiness.setFavouriteStops(this, favouriteStopDbos);
|
105
|
186
|
_goToHome = true;
|
106
|
187
|
}
|
|
188
|
+ else {
|
|
189
|
+ _callbackManager.onActivityResult(requestCode, resultCode, data);
|
|
190
|
+ }
|
107
|
191
|
super.onActivityResult(requestCode, resultCode, data);
|
108
|
192
|
}
|
109
|
193
|
|
110
|
|
- // @Override
|
111
|
|
-// public boolean onCreateOptionsMenu(Menu menu) {
|
112
|
|
-// // Inflate the menu; this adds items to the action bar if it is present.
|
113
|
|
-// getMenuInflater().inflate(R.menu.menu_main, menu);
|
114
|
|
-// return true;
|
115
|
|
-// }
|
116
|
|
-
|
117
|
|
-// @Override
|
118
|
|
-// public boolean onOptionsItemSelected(MenuItem item) {
|
119
|
|
-// // Handle action bar item clicks here. The action bar will
|
120
|
|
-// // automatically handle clicks on the Home/Up button, so long
|
121
|
|
-// // as you specify a parent activity in AndroidManifest.xml.
|
122
|
|
-// int id = item.getItemId();
|
123
|
|
-//
|
124
|
|
-// //noinspection SimplifiableIfStatement
|
125
|
|
-// if (id == R.id.action_settings) {
|
126
|
|
-// return true;
|
127
|
|
-// }
|
128
|
|
-//
|
129
|
|
-// return super.onOptionsItemSelected(item);
|
130
|
|
-// }
|
|
194
|
+ @Override
|
|
195
|
+ public boolean onNavigationItemSelected(MenuItem item) {
|
|
196
|
+ if (item.getItemId() == R.id.nav_logout) {
|
|
197
|
+ LoginManager.getInstance().logOut();
|
|
198
|
+ STSBusiness.setLoggedUser(this, null);
|
|
199
|
+ updateLoginState();
|
|
200
|
+ }
|
|
201
|
+ return false;
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+ public void updateLoginState()
|
|
205
|
+ {
|
|
206
|
+ UserDbo user = STSBusiness.getLoggedUser(this);
|
|
207
|
+ _btnLogin.setVisibility(user == null ? View.VISIBLE : View.GONE);
|
|
208
|
+ Menu menu = _navigationView.getMenu();
|
|
209
|
+ for (int i = 0; i < menu.size(); i++) {
|
|
210
|
+ menu.getItem(i).setVisible(user != null);
|
|
211
|
+ }
|
|
212
|
+ _textUserName.setText(user == null ? "" : user.getName());
|
|
213
|
+ _textUserName.setVisibility(user == null ? View.GONE : View.VISIBLE);
|
|
214
|
+ _textUserEmail.setText(user == null ? "" : user.getEmail());
|
|
215
|
+ _textUserEmail.setVisibility(user == null ? View.GONE : View.VISIBLE);
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+ public void onUserLogged(UserDbo userDbo)
|
|
219
|
+ {
|
|
220
|
+ STSBusiness.setLoggedUser(this, userDbo);
|
|
221
|
+ updateLoginState();
|
|
222
|
+ }
|
131
|
223
|
}
|