/
MainActivity.java
192 lines (165 loc) · 6.32 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package io.github.froger.instamaterial.ui.activity;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageButton;
import butterknife.InjectView;
import butterknife.OnClick;
import io.github.froger.instamaterial.ui.adapter.FeedAdapter;
import io.github.froger.instamaterial.R;
import io.github.froger.instamaterial.Utils;
import io.github.froger.instamaterial.ui.view.FeedContextMenu;
import io.github.froger.instamaterial.ui.view.FeedContextMenuManager;
public class MainActivity extends BaseActivity implements FeedAdapter.OnFeedItemClickListener,
FeedContextMenu.OnFeedContextMenuItemClickListener {
public static final String ACTION_SHOW_LOADING_ITEM = "action_show_loading_item";
private static final int ANIM_DURATION_TOOLBAR = 300;
private static final int ANIM_DURATION_FAB = 400;
@InjectView(R.id.rvFeed)
RecyclerView rvFeed;
@InjectView(R.id.btnCreate)
ImageButton btnCreate;
private FeedAdapter feedAdapter;
private boolean pendingIntroAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFeed();
if (savedInstanceState == null) {
pendingIntroAnimation = true;
} else {
feedAdapter.updateItems(false);
}
}
private void setupFeed() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
@Override
protected int getExtraLayoutSpace(RecyclerView.State state) {
return 300;
}
};
rvFeed.setLayoutManager(linearLayoutManager);
feedAdapter = new FeedAdapter(this);
feedAdapter.setOnFeedItemClickListener(this);
rvFeed.setAdapter(feedAdapter);
rvFeed.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
FeedContextMenuManager.getInstance().onScrolled(recyclerView, dx, dy);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (ACTION_SHOW_LOADING_ITEM.equals(intent.getAction())) {
showFeedLoadingItemDelayed();
}
}
private void showFeedLoadingItemDelayed() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
rvFeed.smoothScrollToPosition(0);
feedAdapter.showLoadingView();
}
}, 500);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
if (pendingIntroAnimation) {
pendingIntroAnimation = false;
startIntroAnimation();
}
return true;
}
private void startIntroAnimation() {
btnCreate.setTranslationY(2 * getResources().getDimensionPixelOffset(R.dimen.btn_fab_size));
int actionbarSize = Utils.dpToPx(56);
getToolbar().setTranslationY(-actionbarSize);
getIvLogo().setTranslationY(-actionbarSize);
getInboxMenuItem().getActionView().setTranslationY(-actionbarSize);
getToolbar().animate()
.translationY(0)
.setDuration(ANIM_DURATION_TOOLBAR)
.setStartDelay(300);
getIvLogo().animate()
.translationY(0)
.setDuration(ANIM_DURATION_TOOLBAR)
.setStartDelay(400);
getInboxMenuItem().getActionView().animate()
.translationY(0)
.setDuration(ANIM_DURATION_TOOLBAR)
.setStartDelay(500)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
startContentAnimation();
}
})
.start();
}
private void startContentAnimation() {
btnCreate.animate()
.translationY(0)
.setInterpolator(new OvershootInterpolator(1.f))
.setStartDelay(300)
.setDuration(ANIM_DURATION_FAB)
.start();
feedAdapter.updateItems(true);
}
@Override
public void onCommentsClick(View v, int position) {
final Intent intent = new Intent(this, CommentsActivity.class);
int[] startingLocation = new int[2];
v.getLocationOnScreen(startingLocation);
intent.putExtra(CommentsActivity.ARG_DRAWING_START_LOCATION, startingLocation[1]);
startActivity(intent);
overridePendingTransition(0, 0);
}
@Override
public void onMoreClick(View v, int itemPosition) {
FeedContextMenuManager.getInstance().toggleContextMenuFromView(v, itemPosition, this);
}
@Override
public void onProfileClick(View v) {
int[] startingLocation = new int[2];
v.getLocationOnScreen(startingLocation);
startingLocation[0] += v.getWidth() / 2;
UserProfileActivity.startUserProfileFromLocation(startingLocation, this);
overridePendingTransition(0, 0);
}
@Override
public void onReportClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@Override
public void onSharePhotoClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@Override
public void onCopyShareUrlClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@Override
public void onCancelClick(int feedItem) {
FeedContextMenuManager.getInstance().hideContextMenu();
}
@OnClick(R.id.btnCreate)
public void onTakePhotoClick() {
int[] startingLocation = new int[2];
btnCreate.getLocationOnScreen(startingLocation);
startingLocation[0] += btnCreate.getWidth() / 2;
TakePhotoActivity.startCameraFromLocation(startingLocation, this);
overridePendingTransition(0, 0);
}
}