Skip to content

Commit

Permalink
Merge pull request #24 from selimtoksal/feature/fragmentTransition
Browse files Browse the repository at this point in the history
building feature/fragmentTransition
  • Loading branch information
muratcanbur authored Nov 11, 2019
2 parents 5eb73b0 + cdac38e commit 2787b2c
Show file tree
Hide file tree
Showing 25 changed files with 217 additions and 61 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ android {
dependencies {
implementation project(':medusalib')
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/trendyol/medusa/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open class BaseFragment : Fragment() {

var multipleStackNavigator: MultipleStackNavigator? = null

override fun onAttach(context: Context?) {
override fun onAttach(context: Context) {
super.onAttach(context)
initStackNavigator(context)
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/trendyol/medusa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.trendyol.medusalib.navigator.MultipleStackNavigator
import com.trendyol.medusalib.navigator.Navigator
import com.trendyol.medusalib.navigator.NavigatorConfiguration
import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType

class MainActivity : AppCompatActivity(), Navigator.NavigatorListener {

Expand Down
52 changes: 27 additions & 25 deletions app/src/main/java/com/trendyol/medusa/MainActivity2.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.trendyol.medusalib.navigator.Navigator;
import com.trendyol.medusalib.navigator.NavigatorConfiguration;
import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction;
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType;

import java.util.Arrays;
import java.util.List;
Expand All @@ -28,26 +29,26 @@ public class MainActivity2 extends AppCompatActivity implements Navigator.Naviga
MultipleStackNavigator multipleStackNavigator;

private List<Function0<Fragment>> rootsFragmentProvider = Arrays
.asList(
new Function0<Fragment>() {
@Override
public Fragment invoke() {
return FragmentGenerator.generateNewFragment();
}
},
new Function0<Fragment>() {
@Override
public Fragment invoke() {
return FragmentGenerator.generateNewFragment();
}
},
new Function0<Fragment>() {
@Override
public Fragment invoke() {
return FragmentGenerator.generateNewFragment();
}
}
);
.asList(
new Function0<Fragment>() {
@Override
public Fragment invoke() {
return FragmentGenerator.generateNewFragment();
}
},
new Function0<Fragment>() {
@Override
public Fragment invoke() {
return FragmentGenerator.generateNewFragment();
}
},
new Function0<Fragment>() {
@Override
public Fragment invoke() {
return FragmentGenerator.generateNewFragment();
}
}
);

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
Expand Down Expand Up @@ -75,11 +76,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
navigation = findViewById(R.id.navigation);

multipleStackNavigator = new MultipleStackNavigator(
getSupportFragmentManager(),
R.id.fragmentContainer,
rootsFragmentProvider,
this,
new NavigatorConfiguration(1, true, NavigatorTransaction.SHOW_HIDE));
getSupportFragmentManager(),
R.id.fragmentContainer,
rootsFragmentProvider,
this,
new NavigatorConfiguration(1, true, NavigatorTransaction.SHOW_HIDE),
null);

multipleStackNavigator.initialize(savedInstanceState);
final SwitchCompat restartRootFragmentCheckBox = findViewById(R.id.restartSwitch);
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/trendyol/medusa/SampleFragment.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package com.trendyol.medusa

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType
import kotlin.random.Random

class SampleFragment : BaseFragment(){
class SampleFragment : BaseFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_sample, container, false)!!
view.findViewById<TextView>(R.id.textview).text = arguments!!.getString(KEY)

view.findViewById<LinearLayout>(R.id.root).setBackgroundColor(Color.argb(255, Random.nextInt(256), Random.nextInt(256), Random.nextInt(256)))

view.findViewById<Button>(R.id.button).setOnClickListener {
multipleStackNavigator!!.start(FragmentGenerator.generateNewFragment())
multipleStackNavigator!!.start(FragmentGenerator.generateNewFragment(), TransitionAnimationType.RIGHT_TO_LEFT)
}
return view
}
Expand Down
29 changes: 15 additions & 14 deletions app/src/main/res/layout/fragment_sample.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

<LinearLayout android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:background="#ccc000"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">


<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start another"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start another"/>

</LinearLayout>
</LinearLayout>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.21'
ext.kotlin_version = '1.3.50'

repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.5.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jan 21 10:31:44 EET 2019
#Tue Oct 22 10:09:51 EET 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
4 changes: 2 additions & 2 deletions medusalib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 6
versionName "0.7.0"
versionCode 8
versionName "0.8"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import com.trendyol.medusalib.navigator.data.FragmentData
import com.trendyol.medusalib.navigator.data.StackItem
import com.trendyol.medusalib.navigator.tag.TagCreator
import com.trendyol.medusalib.navigator.tag.UniqueTagCreator
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType

open class MultipleStackNavigator(
fragmentManager: FragmentManager,
containerId: Int,
private val rootFragmentProvider: List<() -> Fragment>,
private var navigatorListener: Navigator.NavigatorListener? = null,
private val navigatorConfiguration: NavigatorConfiguration = NavigatorConfiguration()
private val navigatorConfiguration: NavigatorConfiguration = NavigatorConfiguration(),
private val transitionAnimationType: TransitionAnimationType? = null
) : Navigator {

private val tagCreator: TagCreator = UniqueTagCreator()
Expand Down Expand Up @@ -43,14 +45,23 @@ open class MultipleStackNavigator(
}

override fun start(fragment: Fragment, fragmentGroupName: String) {
start(fragment, fragmentGroupName, transitionAnimationType)
}

override fun start(fragment: Fragment, transitionAnimation: TransitionAnimationType) {
start(fragment, DEFAULT_GROUP_NAME, transitionAnimation)
}

override fun start(fragment: Fragment, fragmentGroupName: String, transitionAnimation: TransitionAnimationType?) {

val createdTag = tagCreator.create(fragment)
val currentTabIndex = fragmentStackState.getSelectedTabIndex()
val fragmentData = FragmentData(fragment, createdTag)
val fragmentData = FragmentData(fragment, createdTag, transitionAnimation)

if (fragmentStackState.isSelectedTabEmpty()) {
val rootFragment = getRootFragment(currentTabIndex)
val rootFragmentTag = tagCreator.create(rootFragment)
val rootFragmentData = FragmentData(rootFragment, rootFragmentTag)
val rootFragmentData = FragmentData(rootFragment, rootFragmentTag, transitionAnimation)
fragmentManagerController.disableAndStartFragment(
getCurrentFragmentTag(),
rootFragmentData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.trendyol.medusalib.navigator
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType

interface Navigator {

Expand Down Expand Up @@ -46,6 +47,27 @@ interface Navigator {
*/
fun start(fragment: Fragment, tabIndex: Int, fragmentGroupName: String)

/**
* Adds new fragment to the fragment stack with given tab index.
* Hide currently active fragment, switches to given tab Index
* and show newly added fragment to the user.
* @param fragment new fragment
* @param fragmentGroupName will be used in case of you want to remove
* @param transitionAnimation will be used start and remove fragment transition animation
* all fragments which has the same group name.
*/
fun start(fragment: Fragment, fragmentGroupName: String, transitionAnimation: TransitionAnimationType?)

/**
* Adds new fragment to the fragment stack with given tab index.
* Hide currently active fragment, switches to given tab Index
* and show newly added fragment to the user.
* @param fragment new fragment
* @param transitionAnimation will be used start and remove fragment transition animation
* all fragments which has the same group name.
*/
fun start(fragment: Fragment, transitionAnimation: TransitionAnimationType)

/**
* Modifies fragment stack. Pops current fragment from
* fragment stack and detaches it. Peeks from fragment stack
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.trendyol.medusalib.navigator.controller

import androidx.annotation.AnimRes
import androidx.annotation.AnimatorRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.trendyol.medusalib.R
import com.trendyol.medusalib.common.extensions.attach
import com.trendyol.medusalib.common.extensions.detach
import com.trendyol.medusalib.common.extensions.hide
Expand All @@ -12,13 +15,16 @@ import com.trendyol.medusalib.navigator.Navigator
import com.trendyol.medusalib.navigator.data.FragmentData
import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction
import com.trendyol.medusalib.navigator.transaction.TransactionType
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType

class FragmentManagerController(private val fragmentManager: FragmentManager,
private val containerId: Int,
private val navigatorTransaction: NavigatorTransaction) {

private var currentTransaction: FragmentTransaction? = null

private var currentTransitionAnimationType: TransitionAnimationType? = null

fun enableFragment(fragmentTag: String) {
val navigatorTransaction = getFragmentNavigatorTransaction(fragmentTag)

Expand All @@ -39,6 +45,15 @@ class FragmentManagerController(private val fragmentManager: FragmentManager,

fun removeFragment(fragmentTag: String) {
checkAndCreateTransaction()

when (currentTransitionAnimationType) {
TransitionAnimationType.LEFT_TO_RIGHT -> setCustomAnimations(R.anim.empty_animation, R.anim.exit_to_left)
TransitionAnimationType.RIGHT_TO_LEFT -> setCustomAnimations(R.anim.empty_animation, R.anim.exit_to_right)
TransitionAnimationType.BOTTOM_TO_TOP -> setCustomAnimations(R.anim.empty_animation, R.anim.exit_to_bottom)
TransitionAnimationType.TOP_TO_BOTTOM -> setCustomAnimations(R.anim.empty_animation, R.anim.exit_to_top)
TransitionAnimationType.FADE_IN_OUT -> setCustomAnimations(R.anim.empty_animation, R.anim.fade_out)
}

currentTransaction?.remove(getFragment(fragmentTag))
commitAllowingStateLoss()
}
Expand All @@ -64,20 +79,31 @@ class FragmentManagerController(private val fragmentManager: FragmentManager,

checkAndCreateTransaction()

val disabledFragmentNavigatorTransaction = getFragmentNavigatorTransaction(disableFragmentTag)
for (fragmentData in fragmentDataArgs) {
currentTransitionAnimationType = fragmentData.transitionAnimation
when (currentTransitionAnimationType) {
TransitionAnimationType.LEFT_TO_RIGHT -> setCustomAnimations(R.anim.enter_from_left, R.anim.empty_animation)
TransitionAnimationType.RIGHT_TO_LEFT -> setCustomAnimations(R.anim.enter_from_right, R.anim.empty_animation)
TransitionAnimationType.BOTTOM_TO_TOP -> setCustomAnimations(R.anim.enter_from_bottom, R.anim.empty_animation)
TransitionAnimationType.TOP_TO_BOTTOM -> setCustomAnimations(R.anim.enter_from_top, R.anim.empty_animation)
TransitionAnimationType.FADE_IN_OUT -> setCustomAnimations(R.anim.fade_in, R.anim.empty_animation)
}
currentTransaction?.add(containerId, fragmentData.fragment, fragmentData.fragmentTag)
}

val disabledFragmentNavigatorTransaction = getFragmentNavigatorTransaction(disableFragmentTag)
when (disabledFragmentNavigatorTransaction.transactionType) {
TransactionType.SHOW_HIDE -> currentTransaction?.hide(disabledFragment)
TransactionType.ATTACH_DETACH -> currentTransaction?.detach(disabledFragment)
}

for (fragmentData in fragmentDataArgs) {
currentTransaction?.add(containerId, fragmentData.fragment, fragmentData.fragmentTag)
}

commitAllowingStateLoss()
}

private fun setCustomAnimations(@AnimatorRes @AnimRes enter: Int, @AnimatorRes @AnimRes exit: Int) {
currentTransaction?.setCustomAnimations(enter, exit)
}

fun isFragmentNull(fragmentTag: String): Boolean {
return getFragment(fragmentTag) == null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.medusalib.navigator.data

import androidx.fragment.app.Fragment
import com.trendyol.medusalib.navigator.transitionanimation.TransitionAnimationType

data class FragmentData(val fragment: Fragment, val fragmentTag: String)
data class FragmentData(val fragment: Fragment, val fragmentTag: String, val transitionAnimation: TransitionAnimationType? = null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.trendyol.medusalib.navigator.transitionanimation

enum class TransitionAnimationType {
LEFT_TO_RIGHT, RIGHT_TO_LEFT, BOTTOM_TO_TOP, TOP_TO_BOTTOM, FADE_IN_OUT
}
Loading

0 comments on commit 2787b2c

Please sign in to comment.