Skip to content

Instantly share code, notes, and snippets.

@butelo
Last active August 29, 2015 14:25
Show Gist options
  • Save butelo/76b360003c13955072fd to your computer and use it in GitHub Desktop.
Save butelo/76b360003c13955072fd to your computer and use it in GitHub Desktop.
public class InterstitialActivity extends BaseTotoActivity implements View.OnClickListener {
private NetworkImageView mInterstitialImage;
private View mLoader;
private InterstitialPresenter mPresenter;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load view info
setContentView(R.layout.activity_intersitital);
mInterstitialImage = (NetworkImageView) findViewById(R.id.interstitial_image);
mLoader = findViewById(R.id.loader);
View closeButton = findViewById(R.id.interstitial_close);
//Add listeners
closeButton.setOnClickListener(this);
loadInterstitial();
}
private void loadInterstitial (){
mLoader.setVisibility(View.VISIBLE);
mViewTranslator.startLoading();
InterstitialWebService.fetchInterstitial(new IBackgroundTaskCallback<Interstitial>() {
@Override
public void onCompleted (CustomException e, Interstitial interstitial) {
mLoader.setVisibility(View.GONE);
TotoLog.d("Interstitial loaded");
mViewTranslator.stopLoading();
if (interstitial != null) {
mInterstitialImage.setImageUrl(interstitial.getImage(), VolleyUtil.getImageLoader());
mInterstitialImage.setOnClickListener(InterstitialActivity.this);
startTimer();
} else {
close();
}
}
});
}
@Override
public void onPause (Bundle savedInstanceState) {
cancelTimerCallback();
saveState();
}
@Override
public void onDestroy(){
cancelTimerCallback();
}
private class CloseRunnable implements Runnable {
@Override
public void run () {
close();
}
}
public void openNextActivity () {
Intent intent = new Intent(this, AnotherActivity.class);
}
@Override
public void onClick (View v) {
switch (v.getId()){
case R.id.interstitial_close:
close();
break;
case R.id.interstitial_image:
goToInterstitial();
break;
}
}
public class Interstitial implements Parcelable {
private String mTitle;
private String mImage;
private String mHref;
public static Interstitial create(JSONObject json){
Interstitial interstitial = new Interstitial();
interstitial.mTitle = json.optString("title");
interstitial.mImage = json.optString("filename");
interstitial.mHref = json.optString("href");
return interstitial;
}
public String getTitle () {
return mTitle;
}
public String getImage () {
return mImage;
}
public String getHref () {
return mHref;
}
@Override
public int describeContents () {
return 0;
}
@Override
public void writeToParcel (Parcel dest, int flags) {
dest.writeString(this.mTitle);
dest.writeString(this.mImage);
dest.writeString(this.mHref);
}
private Interstitial (Parcel in) {
this.mTitle = in.readString();
this.mImage = in.readString();
this.mHref = in.readString();
}
public static final Parcelable.Creator<Interstitial> CREATOR = new Parcelable.Creator<Interstitial>() {
public Interstitial createFromParcel (Parcel source) {
return new Interstitial(source);
}
public Interstitial[] newArray (int size) {
return new Interstitial[size];
}
};
}
public class InterstitialActivity extends BaseTotoActivity implements View.OnClickListener, InterstitialViewTranslator {
private NetworkImageView mInterstitialImage;
private View mLoader;
private InterstitialPresenter mPresenter;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load view info
setContentView(R.layout.activity_intersitital);
mInterstitialImage = (NetworkImageView) findViewById(R.id.interstitial_image);
mLoader = findViewById(R.id.loader);
View closeButton = findViewById(R.id.interstitial_close);
//Add listeners
closeButton.setOnClickListener(this);
mPresenter = new InterstitialPresenter(this, this);
}
@Override
public void startLoading () {
mLoader.setVisibility(View.VISIBLE);
}
@Override
public void stopLoading () {
mLoader.setVisibility(View.GONE);
}
@Override
public void openNextActivity () {
Intent intent = new Intent(this, AnotherActivity.class);
}
@Override
public void showInterstitial (Interstitial interstitial) {
if(interstitial != null) {
mInterstitialImage.setImageUrl(interstitial.getImage(), VolleyUtil.getImageLoader());
mInterstitialImage.setOnClickListener(this);
}
}
@Override
public void onClick (View v) {
switch (v.getId()){
case R.id.interstitial_close:
mPresenter.close();
break;
case R.id.interstitial_image:
mPresenter.goToInterstitial();
break;
}
}
}
public class InterstitialPresenter {
private InterstitialViewTranslator mViewTranslator;
private CloseRunnable mCloseRunnable;
private Handler mHandler;
public InterstitialPresenter(InterstitialViewTranslator viewTranslator){
mViewTranslator = viewTranslator;
if(mViewTranslator == null || mContext == null){
throw new NullPointerException("The presenter must have context and a view translator instances to work properly.");
}
mHandler = new Handler();
restoreState();
loadInterstitial();
}
private void loadInterstitial (){
mViewTranslator.startLoading();
InterstitialWebService.fetchInterstitial(new IBackgroundTaskCallback<Interstitial>() {
@Override
public void onCompleted (CustomException e, Interstitial interstitial) {
TotoLog.d("Interstitial loaded");
mViewTranslator.stopLoading();
if (interstitial != null) {
onLoadInterstitial(interstitial);
} else {
close();
}
}
});
}
private void onLoadInterstitial(Interstitial interstitial){
mViewTranslator.showInterstitial(interstitial);
startTimer();
}
public void pause () {
cancelTimerCallback();
saveState();
}
public void destroy(){
cancelTimerCallback();
}
private class CloseRunnable implements Runnable {
@Override
public void run () {
close();
}
}
}
public interface InterstitialViewTranslator {
void startLoading();
void stopLoading();
void openNextActivity()
void showInterstitial(Interstitial interstitial);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment