Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.

Commit

Permalink
* remove progressDialog and animate the s**t out of the fab :D and th…
Browse files Browse the repository at this point in the history
…e progressfab :D

because EVERYONE loves animations :D
  • Loading branch information
mikepenz committed Jan 23, 2015
1 parent 89f9e88 commit a383fb6
Showing 1 changed file with 135 additions and 68 deletions.
203 changes: 135 additions & 68 deletions app/src/main/java/com/mikepenz/unsplash/activities/DetailActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.mikepenz.unsplash.activities;

import android.animation.Animator;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -22,8 +21,10 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lzyzsd.circleprogress.DonutProgress;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.ProgressCallback;
import com.koushikdutta.ion.Response;
import com.koushikdutta.ion.future.ResponseFuture;
import com.mikepenz.iconics.IconicsDrawable;
Expand All @@ -41,93 +42,52 @@
public class DetailActivity extends ActionBarActivity {

private ImageView mFabButton;
private DonutProgress mFabProgress;
private View mTitleContainer;
private View mTitlesContainer;
private Image mSelectedImage;

private Drawable mDrawablePhoto;
private Drawable mDrawableClose;
private Drawable mDrawableSuccess;

private ResponseFuture<InputStream> future = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

findViewById(R.id.scroll).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
coolBack();
}
});

// Recover items from the intent
final int position = getIntent().getIntExtra("position", 0);
mSelectedImage = (Image) getIntent().getSerializableExtra("selected_image");

mDrawablePhoto = new IconicsDrawable(this, FontAwesome.Icon.faw_photo).color(Color.WHITE).sizeDp(24);
mDrawableClose = new IconicsDrawable(this, FontAwesome.Icon.faw_close).color(Color.WHITE).sizeDp(24);
mDrawableSuccess = new IconicsDrawable(this, FontAwesome.Icon.faw_check).color(Color.WHITE).sizeDp(24);

mTitlesContainer = findViewById(R.id.activity_detail_titles);

// Fab progress
mFabProgress = (DonutProgress) findViewById(R.id.activity_detail_progress);
mFabProgress.setMax(100);
mFabProgress.setScaleX(0);
mFabProgress.setScaleY(0);

// Fab button
mFabButton = (ImageView) findViewById(R.id.activity_detail_fab);
mFabButton.setScaleX(0);
mFabButton.setScaleY(0);
mFabButton.setImageDrawable(new IconicsDrawable(this, FontAwesome.Icon.faw_photo).color(Color.WHITE).sizeDp(24));
mFabButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog mProgressDialog = new ProgressDialog(DetailActivity.this);

//get the desired wallpaper size so older phones won't die :D
int wallpaperWidth = WallpaperManager.getInstance(DetailActivity.this).getDesiredMinimumWidth();
int wallpaperHeight = WallpaperManager.getInstance(DetailActivity.this).getDesiredMinimumHeight();

//prepare the call
final ResponseFuture<InputStream> future =
Ion.with(DetailActivity.this)
.load(mSelectedImage.getHighResImage(wallpaperWidth, wallpaperHeight))
.progressDialog(mProgressDialog)
.asInputStream();

//setup the progressDialog
mProgressDialog.setTitle(R.string.dialog_setting_title);
mProgressDialog.setMessage(getString(R.string.dialog_setting_text));
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(true);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
future.cancel(true);
}
});
mProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
future.cancel(true);
}
});
mProgressDialog.show();

//set the callback and start downloading
future.withResponse().setCallback(new FutureCallback<Response<InputStream>>() {
@Override
public void onCompleted(Exception e, Response<InputStream> result) {
if (e == null && result != null && result.getResult() != null) {
try {
WallpaperManager.getInstance(DetailActivity.this).setStream(result.getResult());

//some nice animations so the user knows the wallpaper was set properly
mFabButton.animate().rotationBy(720).setDuration(700).start();
mFabButton.setImageDrawable(new IconicsDrawable(DetailActivity.this, FontAwesome.Icon.faw_check).color(Color.WHITE).sizeDp(24));

//animate the butotn to green. just do it the first time
if (mFabButton.getTag() == null) {
TransitionDrawable transition = (TransitionDrawable) mFabButton.getBackground();
transition.startTransition(500);
mFabButton.setTag("");
}

} catch (Exception ex) {
Log.e("un:splash", ex.toString());
}
}
mProgressDialog.dismiss();
}
});
}
});
mFabButton.setImageDrawable(mDrawablePhoto);
mFabButton.setOnClickListener(onFabButtonListener);
mFabButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Expand Down Expand Up @@ -173,6 +133,111 @@ public boolean onLongClick(View v) {
}


private View.OnClickListener onFabButtonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (future == null) {
//reset progress to prevent jumping
mFabProgress.setProgress(0);

//get the desired wallpaper size so older phones won't die :D
int wallpaperWidth = WallpaperManager.getInstance(DetailActivity.this).getDesiredMinimumWidth();
int wallpaperHeight = WallpaperManager.getInstance(DetailActivity.this).getDesiredMinimumHeight();

//prepare the call
future = Ion.with(DetailActivity.this)
.load(mSelectedImage.getHighResImage(wallpaperWidth, wallpaperHeight))
.progressHandler(new ProgressCallback() {
@Override
public void onProgress(long downloaded, long total) {
mFabProgress.setProgress((int) (downloaded * 100.0 / total));
}
})
.asInputStream();

//some nice button animations
Utils.showViewByScale(mFabProgress).setDuration(500).start();
mFabButton.setImageDrawable(mDrawableClose);
mFabButton.animate().rotationBy(360).setDuration(400).setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {
downloadAndSetImage();
}

@Override
public void onAnimationCancel(Animator animation) {
downloadAndSetImage();
}

@Override
public void onAnimationRepeat(Animator animation) {

}
}).start();

} else {
future.cancel(true);
future = null;

//animating everything back to default :D
Utils.hideViewByScaleXY(mFabProgress).setDuration(500).start();
//Utils.animateViewElevation(mFabButton, 0, mElavationPx);
mFabButton.setImageDrawable(mDrawablePhoto);
mFabButton.animate().rotationBy(360).setDuration(400).start();
}
}
};


private void downloadAndSetImage() {
if (future != null) {
//set the callback and start downloading
future.withResponse().setCallback(new FutureCallback<Response<InputStream>>() {
@Override
public void onCompleted(Exception e, Response<InputStream> result) {
boolean success = false;
if (e == null && result != null && result.getResult() != null) {
try {
WallpaperManager.getInstance(DetailActivity.this).setStream(result.getResult());

//some nice animations so the user knows the wallpaper was set properly
mFabButton.animate().rotationBy(720).setDuration(700).start();
mFabButton.setImageDrawable(mDrawableSuccess);

//animate the butotn to green. just do it the first time
if (mFabButton.getTag() == null) {
TransitionDrawable transition = (TransitionDrawable) mFabButton.getBackground();
transition.startTransition(500);
mFabButton.setTag("");
}

success = true;
} catch (Exception ex) {
Log.e("un:splash", ex.toString());
}
}

//hide the progress again :D
Utils.hideViewByScaleXY(mFabProgress).setDuration(500).start();

// if we were not successful remove the x again :D
if (!success) {
//Utils.animateViewElevation(mFabButton, 0, mElavationPx);
mFabButton.setImageDrawable(mDrawablePhoto);
mFabButton.animate().rotationBy(360).setDuration(400).start();
}
future = null;
}
});
}
}


/**
* I use a listener to get notified when the enter transition ends, and with that notifications
* build my own choreography built with the elements of the UI
Expand Down Expand Up @@ -214,6 +279,8 @@ public void onAnimationEnd(Animator animation) {
public void onBackPressed() {

ViewPropertyAnimator hideTitleAnimator = Utils.hideViewByScaleXY(mFabButton);
hideTitleAnimator.setDuration(500);
Utils.hideViewByScaleXY(mFabProgress).setDuration(500).start();

mTitlesContainer.startAnimation(AnimationUtils.loadAnimation(DetailActivity.this, R.anim.alpha_off));
mTitlesContainer.setVisibility(View.INVISIBLE);
Expand Down

0 comments on commit a383fb6

Please sign in to comment.