Permalink
Browse files

code refactoring

  • Loading branch information...
frogermcs committed Jan 21, 2015
1 parent 20705fa commit aa89624a8404532f0fd71993914a7bb0ade2316b
@@ -0,0 +1,60 @@
+package io.github.froger.instamaterial;
+
+import android.support.annotation.Nullable;
+import android.support.v7.app.ActionBarActivity;
+import android.support.v7.widget.Toolbar;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.widget.ImageView;
+
+import butterknife.ButterKnife;
+import butterknife.InjectView;
+import butterknife.Optional;
+
+/**
+ * Created by Miroslaw Stanek on 19.01.15.
+ */
+public class BaseActivity extends ActionBarActivity {
+
+ @InjectView(R.id.toolbar)
+ Toolbar toolbar;
+
+ @InjectView(R.id.ivLogo)
+ ImageView ivLogo;
+
+ private MenuItem inboxMenuItem;
+
+ @Override
+ public void setContentView(int layoutResID) {
+ super.setContentView(layoutResID);
+ ButterKnife.inject(this);
+ setupToolbar();
+ }
+
+ protected void setupToolbar() {
+ if (toolbar != null) {
+ setSupportActionBar(toolbar);
+ toolbar.setNavigationIcon(R.drawable.ic_menu_white);
+ }
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.menu_main, menu);
+ inboxMenuItem = menu.findItem(R.id.action_inbox);
+ inboxMenuItem.setActionView(R.layout.menu_item_view);
+ return true;
+ }
+
+ public Toolbar getToolbar() {
+ return toolbar;
+ }
+
+ public MenuItem getInboxMenuItem() {
+ return inboxMenuItem;
+ }
+
+ public ImageView getIvLogo() {
+ return ivLogo;
+ }
+}
@@ -4,13 +4,9 @@
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
-import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
-import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
-import android.view.Menu;
-import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
@@ -19,18 +15,15 @@
import android.widget.EditText;
import android.widget.LinearLayout;
-import butterknife.ButterKnife;
import butterknife.InjectView;
import io.github.froger.instamaterial.view.SendCommentButton;
/**
* Created by froger_mcs on 11.11.14.
*/
-public class CommentsActivity extends ActionBarActivity implements SendCommentButton.OnSendClickListener {
+public class CommentsActivity extends BaseActivity implements SendCommentButton.OnSendClickListener {
public static final String ARG_DRAWING_START_LOCATION = "arg_drawing_start_location";
- @InjectView(R.id.toolbar)
- Toolbar toolbar;
@InjectView(R.id.contentRoot)
LinearLayout contentRoot;
@InjectView(R.id.rvComments)
@@ -49,8 +42,6 @@
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
- ButterKnife.inject(this);
- setupToolbar();
setupComments();
setupSendCommentButton();
@@ -67,11 +58,6 @@ public boolean onPreDraw() {
}
}
- private void setupToolbar() {
- setSupportActionBar(toolbar);
- toolbar.setNavigationIcon(R.drawable.ic_menu_white);
- }
-
private void setupComments() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rvComments.setLayoutManager(linearLayoutManager);
@@ -95,7 +81,7 @@ private void setupSendCommentButton() {
}
private void startIntroAnimation() {
- ViewCompat.setElevation(toolbar, 0);
+ ViewCompat.setElevation(getToolbar(), 0);
contentRoot.setScaleY(0.1f);
contentRoot.setPivotY(drawingStartLocation);
llAddComment.setTranslationY(200);
@@ -107,7 +93,7 @@ private void startIntroAnimation() {
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
- ViewCompat.setElevation(toolbar, Utils.dpToPx(8));
+ ViewCompat.setElevation(getToolbar(), Utils.dpToPx(8));
animateContent();
}
})
@@ -122,17 +108,9 @@ private void animateContent() {
.start();
}
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- MenuItem inboxMenuItem = menu.findItem(R.id.action_inbox);
- inboxMenuItem.setActionView(R.layout.menu_item_view);
- return true;
- }
-
@Override
public void onBackPressed() {
- ViewCompat.setElevation(toolbar, 0);
+ ViewCompat.setElevation(getToolbar(), 0);
contentRoot.animate()
.translationY(Utils.getScreenHeight(this))
.setDuration(200)
@@ -4,39 +4,28 @@
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.os.Bundle;
-import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
-import android.support.v7.widget.Toolbar;
import android.view.Menu;
-import android.view.MenuItem;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageButton;
-import android.widget.ImageView;
-import android.widget.RelativeLayout;
-import butterknife.ButterKnife;
import butterknife.InjectView;
import io.github.froger.instamaterial.view.FeedContextMenu;
import io.github.froger.instamaterial.view.FeedContextMenuManager;
-public class MainActivity extends ActionBarActivity implements FeedAdapter.OnFeedItemClickListener,
+public class MainActivity extends BaseActivity implements FeedAdapter.OnFeedItemClickListener,
FeedContextMenu.OnFeedContextMenuItemClickListener {
private static final int ANIM_DURATION_TOOLBAR = 300;
private static final int ANIM_DURATION_FAB = 400;
- @InjectView(R.id.toolbar)
- Toolbar toolbar;
- @InjectView(R.id.ivLogo)
- ImageView ivLogo;
@InjectView(R.id.rvFeed)
RecyclerView rvFeed;
@InjectView(R.id.btnCreate)
ImageButton btnCreate;
- private MenuItem inboxMenuItem;
private FeedAdapter feedAdapter;
private boolean pendingIntroAnimation;
@@ -45,9 +34,6 @@
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
- ButterKnife.inject(this);
-
- setupToolbar();
setupFeed();
if (savedInstanceState == null) {
@@ -57,11 +43,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
}
- private void setupToolbar() {
- setSupportActionBar(toolbar);
- toolbar.setNavigationIcon(R.drawable.ic_menu_white);
- }
-
private void setupFeed() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
@Override
@@ -84,9 +65,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- inboxMenuItem = menu.findItem(R.id.action_inbox);
- inboxMenuItem.setActionView(R.layout.menu_item_view);
+ super.onCreateOptionsMenu(menu);
if (pendingIntroAnimation) {
pendingIntroAnimation = false;
startIntroAnimation();
@@ -98,19 +77,19 @@ private void startIntroAnimation() {
btnCreate.setTranslationY(2 * getResources().getDimensionPixelOffset(R.dimen.btn_fab_size));
int actionbarSize = Utils.dpToPx(56);
- toolbar.setTranslationY(-actionbarSize);
- ivLogo.setTranslationY(-actionbarSize);
- inboxMenuItem.getActionView().setTranslationY(-actionbarSize);
+ getToolbar().setTranslationY(-actionbarSize);
+ getIvLogo().setTranslationY(-actionbarSize);
+ getInboxMenuItem().getActionView().setTranslationY(-actionbarSize);
- toolbar.animate()
+ getToolbar().animate()
.translationY(0)
.setDuration(ANIM_DURATION_TOOLBAR)
.setStartDelay(300);
- ivLogo.animate()
+ getIvLogo().animate()
.translationY(0)
.setDuration(ANIM_DURATION_TOOLBAR)
.setStartDelay(400);
- inboxMenuItem.getActionView().animate()
+ getInboxMenuItem().getActionView().animate()
.translationY(0)
.setDuration(ANIM_DURATION_TOOLBAR)
.setStartDelay(500)

0 comments on commit aa89624

Please sign in to comment.