Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance problems #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-annotations:22.2.0'
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/example/fragnums/BackstackFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class BackstackFrame implements Parcelable {

public final static Creator<BackstackFrame> CREATOR = new Creator<BackstackFrame>() {
@Override public BackstackFrame createFromParcel(Parcel source) {
Screen screen = Screen.values()[source.readInt()];
@Screen int screen = source.readInt();
Bundle bundle = source.readBundle();
SparseArray<Parcelable> viewState = bundle.getSparseParcelableArray("viewState");
return new BackstackFrame(screen, viewState);
Expand All @@ -21,16 +21,16 @@ public class BackstackFrame implements Parcelable {
}
};

public final Screen screen;
@Screen public final int screen;

private final SparseArray<Parcelable> viewState;

private BackstackFrame(Screen screen, SparseArray<Parcelable> viewState) {
private BackstackFrame(@Screen int screen, SparseArray<Parcelable> viewState) {
this.screen = screen;
this.viewState = viewState;
}

public BackstackFrame(Screen screen, View view) {
public BackstackFrame(@Screen int screen, View view) {
this.screen = screen;
viewState = new SparseArray<>();
view.saveHierarchyState(viewState);
Expand All @@ -45,7 +45,7 @@ public void restore(View view) {
}

@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(screen.ordinal());
dest.writeInt(screen);
Bundle bundle = new Bundle();
bundle.putSparseParcelableArray("viewState", viewState);
dest.writeBundle(bundle);
Expand Down
38 changes: 17 additions & 21 deletions app/src/main/java/com/example/fragnums/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.fragnums;

import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
Expand All @@ -14,7 +15,7 @@
public final class MainActivity extends AppCompatActivity {

ArrayList<BackstackFrame> backstack;
Screen currentScreen;
@Screen int currentScreen;

FrameLayout container;
View currentView;
Expand All @@ -24,29 +25,25 @@ public final class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
container = (FrameLayout) findViewById(R.id.main_container);
if (savedInstanceState == null) {
currentScreen = Screen.values()[0];
currentScreen = Screen.MAIN;
backstack = new ArrayList<>();
} else {
currentScreen = Screen.values()[savedInstanceState.getInt("currentScreen")];
//noinspection ResourceType
currentScreen = savedInstanceState.getInt("currentScreen");
backstack = savedInstanceState.getParcelableArrayList("backstack");
}
currentView = currentScreen.inflate(container);
currentView = Screens.inflate(currentScreen, container);
container.addView(currentView);
currentScreen.bind(currentView);
Screens.bind(currentScreen, currentView);
updateActionBar();
}

@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentScreen", currentScreen.ordinal());
outState.putInt("currentScreen", currentScreen);
outState.putParcelableArrayList("backstack", backstack);
}

@Override protected void onDestroy() {
super.onDestroy();
currentScreen.unbind();
}

@Override public void onBackPressed() {
if (backstack.size() > 0) {
goBack();
Expand All @@ -65,45 +62,44 @@ public final class MainActivity extends AppCompatActivity {
}
}

public void goTo(Screen screen) {
currentScreen.unbind();
public void goTo(@Screen int screen) {
currentView.startAnimation(loadAnimation(this, R.anim.exit_forward));
container.removeView(currentView);
BackstackFrame backstackFrame = new BackstackFrame(currentScreen, currentView);
backstack.add(backstackFrame);

currentScreen = screen;
currentView = currentScreen.inflate(container);
currentView = Screens.inflate(currentScreen, container);
currentView.startAnimation(loadAnimation(this, R.anim.enter_forward));
container.addView(currentView);
currentScreen.bind(currentView);
Screens.bind(currentScreen, currentView);

updateActionBar();
}

public void goBack() {
currentScreen.unbind();
currentView.startAnimation(loadAnimation(this, R.anim.exit_backward));
container.removeView(currentView);

BackstackFrame latest = backstack.remove(backstack.size() - 1);
currentScreen = latest.screen;
currentView = currentScreen.inflate(container);
currentView = Screens.inflate(currentScreen, container);
currentView.startAnimation(loadAnimation(this, R.anim.enter_backward));
container.addView(currentView, 0);
latest.restore(currentView);
currentScreen.bind(currentView);
Screens.bind(currentScreen, currentView);

updateActionBar();
}

private void updateActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(backstack.size() != 0);
CharSequence title = currentScreen.getTitle();
@StringRes Integer title = Screens.getTitle(currentScreen);
if (title == null) {
title = getTitle();
actionBar.setTitle(getTitle());
} else {
actionBar.setTitle(title);
}
actionBar.setTitle(title);
}
}
106 changes: 9 additions & 97 deletions app/src/main/java/com/example/fragnums/Screen.java
Original file line number Diff line number Diff line change
@@ -1,101 +1,13 @@
package com.example.fragnums;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.annotation.IntDef;
import com.squareup.enumsbatter.R;

import static android.content.Intent.ACTION_VIEW;

public enum Screen {

MAIN(R.layout.main) {
@Override protected void onBind() {
findViewById(R.id.enumsmatter).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
goTo(ENUMSMATTER);
}
});
findViewById(R.id.tshirt).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
goTo(T_SHIRT);
}
});
}
},

ENUMSMATTER(R.layout.enumsmatter, R.string.enumsmatter_title),

T_SHIRT(R.layout.tshirt, R.string.tshirt_title) {
@Override protected void onBind() {
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent = new Intent(ACTION_VIEW, Uri.parse("https://teespring.com/enumsmatter"));
view.getContext().startActivity(intent);
}
});
}
};

private final int layoutResId;
private final int titleResId;
protected View view;

Screen(@LayoutRes int layoutResId) {
this(layoutResId, -1);
}

Screen(@LayoutRes int layoutResId, @StringRes int titleResId) {
this.layoutResId = layoutResId;
this.titleResId = titleResId;
}

public View inflate(ViewGroup container) {
Context context = container.getContext();
return LayoutInflater.from(context).inflate(layoutResId, container, false);
}

public void bind(View view) {
this.view = view;
onBind();
}

public void unbind() {
onUnbind();
view = null;
}

protected void onBind() {
}

protected void onUnbind() {
}

public CharSequence getTitle() {
if (titleResId == -1) {
return null;
}
return view.getContext().getString(titleResId);
}

protected void goBack() {
MainActivity activity = (MainActivity) view.getContext();
activity.goBack();
}

protected void goTo(Screen screen) {
MainActivity activity = (MainActivity) view.getContext();
activity.goTo(screen);
}

@SuppressWarnings({ "unchecked", "UnusedDeclaration" })
protected <T extends View> T findViewById(int id) {
return (T) view.findViewById(id);
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@IntDef({ Screen.MAIN, Screen.ENUMSMATTER, Screen.T_SHIRT }) @Retention(RetentionPolicy.SOURCE)
public @interface Screen {
int MAIN = R.layout.main;
int ENUMSMATTER = R.layout.enumsmatter;
int T_SHIRT = R.layout.tshirt;
}
66 changes: 66 additions & 0 deletions app/src/main/java/com/example/fragnums/Screens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.fragnums;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.squareup.enumsbatter.R;

import static android.content.Intent.ACTION_VIEW;

public final class Screens {

public static void bind(@Screen @LayoutRes int screen, View view) {
switch (screen) {
case Screen.MAIN:
view.findViewById(R.id.enumsmatter).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
goTo(Screen.ENUMSMATTER, v);
}
});
view.findViewById(R.id.tshirt).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
goTo(Screen.T_SHIRT, v);
}
});
break;
case Screen.ENUMSMATTER:
break;
case Screen.T_SHIRT:
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent = new Intent(ACTION_VIEW, Uri.parse("https://teespring.com/enumsmatter"));
view.getContext().startActivity(intent);
}
});
break;
}
}

@Nullable @StringRes public static Integer getTitle(@Screen int screen) {
switch(screen) {
case Screen.ENUMSMATTER:
return R.string.enumsmatter_title;
case Screen.T_SHIRT:
return R.string.tshirt_title;
case Screen.MAIN:
default:
return null;
}
}

public static View inflate(@Screen @LayoutRes int screen, ViewGroup container) {
Context context = container.getContext();
return LayoutInflater.from(context).inflate(screen, container, false);
}

private static void goTo(@Screen int screen, View view) {
MainActivity activity = (MainActivity) view.getContext();
activity.goTo(screen);
}
}