-
Notifications
You must be signed in to change notification settings - Fork 4
/
MainActivity.java
169 lines (147 loc) · 6.55 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.servilat.intouch;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.servilat.intouch.dialog.DialogsListFragment;
import com.servilat.intouch.friends.FriendListFragment;
import com.servilat.intouch.login.LoginVkFragment;
import com.squareup.picasso.Picasso;
import com.vk.sdk.VKAccessToken;
import com.vk.sdk.VKSdk;
import com.vk.sdk.api.VKApi;
import com.vk.sdk.api.VKApiConst;
import com.vk.sdk.api.VKParameters;
import com.vk.sdk.api.VKRequest;
import com.vk.sdk.api.VKResponse;
import com.vk.sdk.api.model.VKApiUserFull;
import com.vk.sdk.api.model.VKList;
import org.json.JSONException;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
private Fragment visibleFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorVk));
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (visibleFragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, visibleFragment, "visible_fragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
}
};
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
if (!VKSdk.isLoggedIn()) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
visibleFragment = new LoginVkFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, visibleFragment, "visible_fragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
} else {
showUserDialogs();
vkRequestForHeader();
}
}
/*
@Override
protected void onStart() {
super.onStart();
}
*/
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
visibleFragment = null;
switch (id) {
case R.id.nav_friends:
showUserFriends();
break;
case R.id.nav_messages:
showUserDialogs();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void showUserFriends() {
FriendListFragment friendListFragment = new FriendListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, friendListFragment, "visible_friends");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
public void showUserDialogs() {
DialogsListFragment dialogsListFragment = new DialogsListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, dialogsListFragment, "visible_dialogs");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
public void vkRequestForHeader() {
VKRequest vkRequest = VKApi.users().get(VKParameters.from(
VKApiConst.USER_IDS, VKAccessToken.currentToken().userId,
VKApiConst.FIELDS, "photo_100, status"));
vkRequest.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
super.onComplete(response);
VKApiUserFull currentVKUser = (VKApiUserFull) ((VKList) response.parsedModel).get(0);
String status = "";
try {
status = currentVKUser.fields.getString("status");
} catch (JSONException ignored) {
}
fillNavigationDrawerHeader(currentVKUser.toString(), status, currentVKUser.photo_100);
}
});
}
private void fillNavigationDrawerHeader(String fullName, String status, String photo) {
View header = ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0);
((TextView) header.findViewById(R.id.nav_user_name)).setText(fullName);
((TextView) header.findViewById(R.id.nav_user_status)).setText(status);
Picasso.with(getApplicationContext())
.load(photo)
.placeholder(R.drawable.placeholder_person)
.error(R.drawable.placeholder_person)
.into((ImageView) header.findViewById(R.id.nav_user_photo));
}
}