Skip to content

Commit

Permalink
Handle dribbble urls intents.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Feb 10, 2016
1 parent 4d9122e commit b533387
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 56 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
android:name=".ui.DribbbleShot"
android:parentActivityName=".ui.HomeActivity"
android:theme="@style/Plaid.Translucent.Dribbble.Shot">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="dribbble.com"
android:pathPrefix="/shots/"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.HomeActivity" />
Expand Down
158 changes: 102 additions & 56 deletions app/src/main/java/io/plaidapp/ui/DribbbleShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import android.os.Bundle;
import android.os.Parcelable;
import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.graphics.Palette;
import android.text.Spanned;
Expand Down Expand Up @@ -106,6 +107,8 @@
import io.plaidapp.util.customtabs.CustomTabActivityHelper;
import io.plaidapp.util.glide.CircleTransform;
import io.plaidapp.util.glide.GlideUtils;
import okhttp3.HttpUrl;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
Expand Down Expand Up @@ -157,12 +160,10 @@ public class DribbbleShot extends Activity {
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dribbble_shot);
shot = getIntent().getParcelableExtra(EXTRA_SHOT);
setupDribbble();
setExitSharedElementCallback(fabLoginSharedElementCallback);
getWindow().getSharedElementReturnTransition().addListener(shotReturnHomeListener);
circleTransform = new CircleTransform(this);
Resources res = getResources();

ButterKnife.bind(this);
View shotDescription = getLayoutInflater().inflate(R.layout.dribbble_shot_description,
Expand Down Expand Up @@ -195,6 +196,92 @@ public void onDragDismissed() {
}
};

final Intent intent = getIntent();
if (intent.hasExtra(EXTRA_SHOT)) {
shot = intent.getParcelableExtra(EXTRA_SHOT);
bindShot(true, savedInstanceState != null);
} else if (intent.getData() != null) {
final HttpUrl url = HttpUrl.parse(intent.getDataString());
if (url.pathSize() == 2 && url.pathSegments().get(0).equals("shots")) {
try {
final String shotPath = url.pathSegments().get(1);
final long id = Long.parseLong(shotPath.substring(0, shotPath.indexOf("-")));

dribbbleApi.getShot(id, new Callback<Shot>() {
@Override
public void success(Shot shot, Response response) {
DribbbleShot.this.shot = shot;
bindShot(false, true);
}

@Override
public void failure(RetrofitError error) {
reportUrlError();
}
});
} catch (NumberFormatException|StringIndexOutOfBoundsException ex) {
reportUrlError();
}
} else {
reportUrlError();
}
}
}

@Override
protected void onResume() {
super.onResume();
if (!performingLike) {
checkLiked();
}
draggableFrame.addListener(chromeFader);
}

@Override
protected void onPause() {
draggableFrame.removeListener(chromeFader);
super.onPause();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_LOGIN_LIKE:
if (resultCode == RESULT_OK) {
setupDribbble(); // recreate to capture the new access token
// TODO when we add more authenticated actions will need to keep track of what
// the user was trying to do when forced to login
fab.setChecked(true);
doLike();
setupCommenting();
}
break;
case RC_LOGIN_COMMENT:
if (resultCode == RESULT_OK) {
setupCommenting();
}
}
}

@Override
public void onBackPressed() {
expandImageAndFinish();
}

@Override
public boolean onNavigateUp() {
expandImageAndFinish();
return true;
}

@Override @TargetApi(Build.VERSION_CODES.M)
public void onProvideAssistContent(AssistContent outContent) {
outContent.setWebUri(Uri.parse(shot.url));
}

private void bindShot(final boolean postponeEnterTransition, final boolean animateFabManually) {
final Resources res = getResources();

// load the main image
final int[] imageSize = shot.images.bestSize();
Glide.with(this)
Expand All @@ -207,15 +294,15 @@ public void onDragDismissed() {
imageView.setOnClickListener(shotClick);
shotSpacer.setOnClickListener(shotClick);

postponeEnterTransition();
if (postponeEnterTransition) postponeEnterTransition();
imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver
.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
imageView.getViewTreeObserver().removeOnPreDrawListener(this);
calculateFabPosition();
enterAnimation(savedInstanceState != null);
startPostponedEnterTransition();
enterAnimation(animateFabManually);
if (postponeEnterTransition) startPostponedEnterTransition();
return true;
}
});
Expand Down Expand Up @@ -317,55 +404,14 @@ public void onClick(View v) {
}
}

@Override
protected void onResume() {
super.onResume();
if (!performingLike) {
checkLiked();
}
draggableFrame.addListener(chromeFader);
}

@Override
protected void onPause() {
draggableFrame.removeListener(chromeFader);
super.onPause();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_LOGIN_LIKE:
if (resultCode == RESULT_OK) {
setupDribbble(); // recreate to capture the new access token
// TODO when we add more authenticated actions will need to keep track of what
// the user was trying to do when forced to login
fab.setChecked(true);
doLike();
setupCommenting();
}
break;
case RC_LOGIN_COMMENT:
if (resultCode == RESULT_OK) {
setupCommenting();
}
}
}

@Override
public void onBackPressed() {
expandImageAndFinish();
}

@Override
public boolean onNavigateUp() {
expandImageAndFinish();
return true;
}

@Override @TargetApi(Build.VERSION_CODES.M)
public void onProvideAssistContent(AssistContent outContent) {
outContent.setWebUri(Uri.parse(shot.url));
private void reportUrlError() {
Snackbar.make(draggableFrame, R.string.bad_dribbble_shot_url, Snackbar.LENGTH_SHORT).show();
draggableFrame.postDelayed(new Runnable() {
@Override
public void run() {
finishAfterTransition();
}
}, 3000L);
}

private void setupCommenting() {
Expand Down Expand Up @@ -664,7 +710,7 @@ private void calculateFabPosition() {
* are within the ListView so do it manually. Also handle the FAB tanslation here so that it
* plays nicely with #calculateFabPosition
*/
private void enterAnimation(boolean isOrientationChange) {
private void enterAnimation(boolean animateFabManually) {
Interpolator interp = getFastOutSlowInInterpolator(this);
int offset = title.getHeight();
viewEnterAnimation(title, offset, interp);
Expand Down Expand Up @@ -693,7 +739,7 @@ private void enterAnimation(boolean isOrientationChange) {
.setInterpolator(interp)
.start();

if (isOrientationChange) {
if (animateFabManually) {
// we rely on the window enter content transition to show the fab. This isn't run on
// orientation changes so manually show it.
Animator showFab = ObjectAnimator.ofPropertyValuesHolder(fab,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
</plurals>
<string name="share">share</string>
<string name="prospects_cant_post">Sorry but prospects cannot comment :(</string>
<string name="bad_dribbble_shot_url">Can\'t open that link :(</string>

<!-- dribbble player -->
<string name="follow">Follow</string>
Expand Down

0 comments on commit b533387

Please sign in to comment.