Skip to content

Commit

Permalink
feat(*): transition for shared elements
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan-fueled committed Nov 21, 2017
1 parent 38f3cac commit 2cf1804
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 4 deletions.
51 changes: 51 additions & 0 deletions flowr/src/main/java/com/fueled/flowr/Flowr.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
Expand All @@ -18,6 +20,7 @@
import com.fueled.flowr.internal.FlowrDeepLinkHandler;
import com.fueled.flowr.internal.FlowrDeepLinkInfo;
import com.fueled.flowr.internal.TransactionData;
import com.fueled.flowr.internal.TransitionConfig;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -267,6 +270,10 @@ protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionD
Fragment fragment = data.getFragmentClass().newInstance();
fragment.setArguments(data.getArgs());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && data.getTransitionConfig() != null) {
setTransitions(fragment, data.getTransitionConfig());
}

FragmentTransaction transaction = screen.getScreenFragmentManager().beginTransaction();

if (!data.isSkipBackStack()) {
Expand All @@ -282,6 +289,11 @@ protected <T extends Fragment & FlowrFragment> void displayFragment(TransactionD
transaction.add(mainContainerId, fragment);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && data.getTransitionConfig() != null &&
data.getSharedElements() != null && data.getSharedElements().length > 0) {
addSharedElements(transaction, data.getSharedElements());
}

transaction.commit();

if (data.isSkipBackStack()) {
Expand Down Expand Up @@ -323,6 +335,33 @@ private <T extends Fragment & FlowrFragment> void injectDeepLinkInfo(Transaction
}
}

/**
* Set transitions to the destination fragment from @{@link TransitionConfig}.
*
* @param fragment The destination Fragment.
* @param transitionConfig The transition configuration @{@link TransitionConfig}.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setTransitions(Fragment fragment, TransitionConfig transitionConfig) {
fragment.setEnterTransition(transitionConfig.enter);
fragment.setSharedElementEnterTransition(transitionConfig.sharedElementEnter);
fragment.setExitTransition(transitionConfig.exit);
fragment.setSharedElementReturnTransition(transitionConfig.sharedElementExit);
}

/**
* Add shared elements to a Fragment Transaction.
*
* @param transaction The transaction that will.
* @param views The shared elements.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void addSharedElements(FragmentTransaction transaction, View[] views) {
for (View view : views) {
transaction.addSharedElement(view, view.getTransitionName());
}
}

/**
* Set a Custom Animation to a Fragment transaction
*
Expand Down Expand Up @@ -730,6 +769,18 @@ public Builder setCustomTransactionAnimation(@AnimRes int enterAnim, @AnimRes in

}

/**
* Set transition between fragments.
*
* @param transitionConfig builder class for configuring fragment transitions
* @param sharedElements array of shared elements
*/
public Builder setTransition(TransitionConfig transitionConfig, View... sharedElements) {
data.setTransitionConfig(transitionConfig);
data.setSharedElements(sharedElements);
return this;
}

/**
* Don't use any animations for this transaction
*/
Expand Down
19 changes: 19 additions & 0 deletions flowr/src/main/java/com/fueled/flowr/internal/TransactionData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.fueled.flowr.FlowrFragment;

Expand All @@ -23,6 +24,8 @@ public final class TransactionData<T extends Fragment & FlowrFragment> {
private int popEnterAnim;
private int popExitAnim;
private Intent deepLinkIntent;
private View[] sharedElements;
private TransitionConfig transitionConfig;

public TransactionData(Class<? extends T> fragmentClass) {
this(fragmentClass, FragmentTransaction.TRANSIT_NONE, FragmentTransaction.TRANSIT_NONE);
Expand Down Expand Up @@ -121,4 +124,20 @@ public int getExitAnim() {
public void setExitAnim(int exitAnim) {
this.exitAnim = exitAnim;
}

public View[] getSharedElements() {
return sharedElements;
}

public void setSharedElements(View[] sharedElements) {
this.sharedElements = sharedElements;
}

public TransitionConfig getTransitionConfig() {
return transitionConfig;
}

public void setTransitionConfig(TransitionConfig transitionConfig) {
this.transitionConfig = transitionConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.fueled.flowr.internal;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.transition.ChangeBounds;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.transition.Transition;
import android.view.Gravity;

/**
* Copyright (c) 2017 Fueled. All rights reserved.
*
* @author chetansachdeva on 21/11/17
*/

public class TransitionConfig {

public Transition sharedElementEnter;
public Transition sharedElementExit;
public Transition enter;
public Transition exit;

private TransitionConfig(Builder builder) {
sharedElementEnter = builder.sharedElementEnter;
sharedElementExit = builder.sharedElementExit;
enter = builder.enter;
exit = builder.exit;
}

public static final class Builder {
private Transition sharedElementEnter;
private Transition sharedElementExit;
private Transition enter;
private Transition exit;

public Builder() {
}

public Builder sharedElementEnter(Transition val) {
sharedElementEnter = val;
return this;
}

public Builder sharedElementExit(Transition val) {
sharedElementExit = val;
return this;
}

public Builder enter(Transition val) {
enter = val;
return this;
}

public Builder exit(Transition val) {
exit = val;
return this;
}

public TransitionConfig build() {
return new TransitionConfig(this);
}
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static class Provider {
public static Transition fade = new Fade();
public static Transition explode = new Explode();
public static Transition slideRight = new Slide(Gravity.RIGHT);
public static Transition slideLeft = new Slide(Gravity.LEFT);
public static Transition changeBounds = new ChangeBounds();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import android.view.View;

import com.fueled.flowr.NavigationIconType;
import com.fueled.flowr.annotations.DeepLink;
import com.fueled.flowr.sample.core.AbstractFragment;

/**
* Created by [email protected] on 18/05/2017.
* Copyright (c) 2017 Fueled. All rights reserved.
*/

@DeepLink(value = {"/categories"})
public class CategoriesFragment extends AbstractFragment {

@Override
Expand Down
40 changes: 36 additions & 4 deletions sample/src/main/java/com/fueled/flowr/sample/HomeFragment.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.fueled.flowr.sample;

import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.View;

import com.fueled.flowr.NavigationIconType;
import com.fueled.flowr.internal.TransitionConfig;
import com.fueled.flowr.sample.core.AbstractFragment;
import com.fueled.flowr.sample.databinding.FragmentHomeBinding;

Expand All @@ -25,6 +28,7 @@ protected void setupView(View view) {
binding = DataBindingUtil.bind(view);
binding.homeOpenViewButton.setOnClickListener(this);
binding.homeOpenLinkButton.setOnClickListener(this);
binding.homeOpenTransitionButton.setOnClickListener(this);
}

@Override
Expand All @@ -39,10 +43,18 @@ public NavigationIconType getNavigationIconType() {

@Override
public void onClick(View view) {
if (view.getId() == R.id.home_open_view_button) {
displayViewFragment();
} else {
displayLinkFragment();
switch (view.getId()) {
case R.id.home_open_view_button:
displayViewFragment();
break;
case R.id.home_open_link_button:
displayLinkFragment();
break;
case R.id.home_open_transition_button:
displayTransitionFragment();
break;
default:
break;
}
}

Expand All @@ -57,4 +69,24 @@ private void displayLinkFragment() {
getFlowr().open("/hello")
.displayFragment();
}

private void displayTransitionFragment() {
getFlowr().open("/transition")
.replaceCurrentFragment(true)
.setTransition(getTransitionConfig(), binding.flowrTextView)
.displayFragment();
}

@NonNull
private TransitionConfig getTransitionConfig() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return new TransitionConfig.Builder()
.sharedElementEnter(TransitionConfig.Provider.changeBounds)
.sharedElementExit(TransitionConfig.Provider.changeBounds)
.enter(TransitionConfig.Provider.fade)
.exit(TransitionConfig.Provider.fade)
.build();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.fueled.flowr.sample;

import android.view.View;

import com.fueled.flowr.NavigationIconType;
import com.fueled.flowr.annotations.DeepLink;
import com.fueled.flowr.sample.core.AbstractFragment;

/**
* Created by [email protected] on 18/05/2017.
* Copyright (c) 2017 Fueled. All rights reserved.
*/

@DeepLink(value = {"/transition"})
public class TransitionFragment extends AbstractFragment {

@Override
public int getLayoutId() {
return R.layout.fragment_transition;
}

@Override
protected void setupView(View view) {

}

@Override
public String getTitle() {
return "Transition Fragment";
}

@Override
public NavigationIconType getNavigationIconType() {
return NavigationIconType.HAMBURGER;
}
}
19 changes: 19 additions & 0 deletions sample/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/flowr_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="4dp"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="16sp"
android:transitionName="transitionText"/>

<Button
android:id="@+id/home_open_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/open_view_fragment"/>

<Button
Expand All @@ -22,6 +34,13 @@
android:layout_marginTop="10dp"
android:text="@string/open_link_fragment"/>

<Button
android:id="@+id/home_open_transition_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/open_transition_fragment"/>

</LinearLayout>

</layout>
27 changes: 27 additions & 0 deletions sample/src/main/res/layout/fragment_transition.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/transition_message"/>

<TextView
android:id="@+id/flowr_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
android:padding="8dp"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="20sp"
android:transitionName="transitionText"/>

</FrameLayout>
Loading

0 comments on commit 2cf1804

Please sign in to comment.