Skip to content

Commit

Permalink
Updated the version for adding android.os.fragment extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Nov 8, 2017
1 parent 054aafd commit 0ea137f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 70 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ allprojects {
And add our dependency to your app `bundle.gradle`.

```gradle
implementation 'com.devrapid.jieyi:kotlinknifer:1.0.15'
implementation 'com.devrapid.jieyi:kotlinknifer:1.0.16'
```

Then you can use it!!!
Expand All @@ -50,7 +50,7 @@ Then you can use it!!!
<dependency>
<groupId>com.devrapid.jieyi</groupId>
<artifactId>kotlinknifer</artifactId>
<version>1.0.15</version>
<version>1.0.16</version>
<type>pom</type>
</dependency>
```
Expand All @@ -64,7 +64,7 @@ Then you can use it!!!
If you'd not like to use them to your project, you can add the exclude as like below

```
implementation('com.devrapid.jieyi:kotlinknifer:1.0.15', {
implementation('com.devrapid.jieyi:kotlinknifer:1.0.16', {
exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
exclude group: 'io.reactivex.rxjava2', module: 'rxkotlin'
exclude group: 'com.google.code.gson', module: 'gson'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ proj_name=kotlinknifer
proj_libname=KotlinKnifer
proj_group=com.devrapid.jieyi
proj_artifactid=kotlinknifer
proj_version=1.0.15
proj_version=1.0.16
proj_description=For developing an Android conveniently and rapidly.
proj_websiteurl=https://github.com/pokk/KotlinKnifer
proj_issuetrackerurl=https://github.com/pokk/KotlinKnifer/issues
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.devrapid.kotlinknifer

import android.app.Fragment
import android.app.FragmentManager
import android.os.Build
import android.support.annotation.RequiresApi
import android.view.View

/**
* @author jieyi
* @since 11/8/17
*/
/** **************************************************************************
* Fragment for [Fragment]
** **************************************************************************/

/**
* Adds a [Fragment] to this manager's layout.
*
* @param containerViewId The container view to where add the fragment.
* @param fragment The fragment to be added.
* @param needBack Set that it can back to previous fragment.
* @param sharedElements Shared element objects and ids from layout xml [android:transitionName].
*
* @return the identifier of this transaction's back stack entry.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun FragmentManager.addFragment(containerViewId: Int,
fragment: Fragment,
needBack: Boolean = false,
sharedElements: HashMap<View, String> = hashMapOf()): Int = beginTransaction().apply {
replace(containerViewId, fragment, fragment.javaClass.name)
sharedElements.forEach { value -> addSharedElement(value.key, value.value) }
if (needBack)
addToBackStack(fragment.javaClass.name)
}.commit()

/**
* Pop a [Fragment] from the [FragmentManager].
*
* @return is success to pop a [Fragment].
*/
inline fun FragmentManager.popFragment(): Boolean = if (0 < backStackEntryCount) {
popBackStackImmediate()
true
}
else
false

/**
* Clear all [Fragment] in the stack.
*/
inline fun FragmentManager.popAllFragment() {
// Optimized by Kotlin.
for (i in 0..backStackEntryCount - 1) {
popFragment()
}
}

/**
* Remove a specific [Fragment] from [FragmentManager] stack.
*
* @param fragment Specific assigned [Fragment].
*/
@RequiresApi(Build.VERSION_CODES.N)
inline fun FragmentManager.removeFragment(fragment: Fragment) = beginTransaction().remove(
fragment).commitNow()
74 changes: 8 additions & 66 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Fragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package com.devrapid.kotlinknifer

import android.annotation.SuppressLint
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.view.View
Expand All @@ -27,9 +25,9 @@ import android.view.View
fun FragmentManager.addFragment(containerViewId: Int,
fragment: Fragment,
needBack: Boolean = false,
sharedElements: HashMap<View, String>? = null): Int = this.beginTransaction().also {
sharedElements: HashMap<View, String> = hashMapOf()): Int = beginTransaction().also {
it.replace(containerViewId, fragment, fragment.javaClass.name)
sharedElements?.forEach { value -> it.addSharedElement(value.key, value.value) }
sharedElements.forEach { value -> it.addSharedElement(value.key, value.value) }
if (needBack)
it.addToBackStack(fragment.javaClass.name)
}.commit()
Expand All @@ -39,8 +37,8 @@ fun FragmentManager.addFragment(containerViewId: Int,
*
* @return is success to pop a [Fragment].
*/
inline fun FragmentManager.popFragment(): Boolean = if (0 < this.backStackEntryCount) {
this.popBackStackImmediate()
inline fun FragmentManager.popFragment(): Boolean = if (0 < backStackEntryCount) {
popBackStackImmediate()
true
}
else
Expand All @@ -49,20 +47,20 @@ else
/**
* Clear all [Fragment] in the stack.
*/
inline fun FragmentManager.popAllFragment() = (0..this.backStackEntryCount - 1).forEach { this.popFragment() }
inline fun FragmentManager.popAllFragment() = (0..backStackEntryCount - 1).forEach { popFragment() }

/**
* Remove a specific [Fragment] from [FragmentManager] stack.
*
* @param fragment Specific assigned [Fragment].
*/
inline fun FragmentManager.removeFragment(fragment: Fragment) = this.beginTransaction().remove(fragment).commitNow()
inline fun FragmentManager.removeFragment(fragment: Fragment) = beginTransaction().remove(fragment).commitNow()

/**
* Remove all [Fragment] from [FragmentManager] stack.
*/
@SuppressLint("RestrictedApi")
fun FragmentManager.removeRecursiveFragment() = this.fragments?.forEach {
fun FragmentManager.removeRecursiveFragment() = fragments?.forEach {
it?.let { f ->
it.childFragmentManager?.fragments?.forEach {
it?.let { f.childFragmentManager.removeFragment(it) }
Expand All @@ -74,64 +72,8 @@ fun FragmentManager.removeRecursiveFragment() = this.fragments?.forEach {
* Testing code. For showing all fragments and children fragments.
*/
@SuppressLint("RestrictedApi")
fun FragmentManager.showAllFragment() = this.fragments?.forEach {
fun FragmentManager.showAllFragment() = fragments?.forEach {
it?.let {
it.childFragmentManager?.fragments?.forEach { Logs.d("child!!!! : $it") }
}
}

/** **************************************************************************
* Fragment for [android.app.Fragment]
** **************************************************************************/

/**
* Adds a [Fragment] to this manager's layout.
*
* @param containerViewId The container view to where add the fragment.
* @param fragment The fragment to be added.
* @param needBack Set that it can back to previous fragment.
* @param sharedElements Shared element objects and ids from layout xml [android:transitionName].
*
* @return the identifier of this transaction's back stack entry.
*/
fun android.app.FragmentManager.addFragment(containerViewId: Int,
fragment: android.app.Fragment,
needBack: Boolean = false,
sharedElements: HashMap<View, String> = hashMapOf()): Int = beginTransaction().apply {
replace(containerViewId, fragment, fragment.javaClass.name)
sharedElements.forEach { value -> addSharedElement(value.key, value.value) }
if (needBack)
addToBackStack(fragment.javaClass.name)
}.commit()

/**
* Pop a [Fragment] from the [FragmentManager].
*
* @return is success to pop a [Fragment].
*/
inline fun android.app.FragmentManager.popFragment(): Boolean = if (0 < backStackEntryCount) {
popBackStackImmediate()
true
}
else
false

/**
* Clear all [Fragment] in the stack.
*/
inline fun android.app.FragmentManager.popAllFragment() {
// Optimized by Kotlin.
for (i in 0..backStackEntryCount - 1) {
popFragment()
}
}

/**
* Remove a specific [Fragment] from [FragmentManager] stack.
*
* @param fragment Specific assigned [Fragment].
*/
@RequiresApi(Build.VERSION_CODES.N)
inline fun android.app.FragmentManager.removeFragment(fragment: android.app.Fragment) = beginTransaction().remove(
fragment).commitNow()

0 comments on commit 0ea137f

Please sign in to comment.