Skip to content

Commit 5ff7d31

Browse files
author
tans.tan
committed
[feat] Add v1.0.3, adjust fragment lifecycle handle.
1 parent a171f13 commit 5ff7d31

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

tuiutils/publish.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GROUP_ID=io.github.tans5
22
ARTIFACT_ID=tuiutils
3-
VERSION_NAME=1.0.2
3+
VERSION_NAME=1.0.3
44

55
RELEASE_REPOSITORY_URL=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2
66
SNAPSHOT_REPOSITORY_URL=https://s01.oss.sonatype.org/content/repositories/snapshots

tuiutils/src/main/java/com/tans/tuiutils/fragment/BaseCoroutineStateFragment.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ abstract class BaseCoroutineStateFragment<State : Any>(protected val defaultStat
2424
}
2525
}
2626

27+
open val firstLaunchCheckDefaultState: Boolean = true
28+
2729
protected var uiCoroutineScope: CoroutineScope? = null
2830
private set
2931

@@ -45,18 +47,24 @@ abstract class BaseCoroutineStateFragment<State : Any>(protected val defaultStat
4547

4648
final override fun firstLaunchInitData() {
4749
val newDataCoroutineScope = CoroutineScope(Dispatchers.IO + dataCoroutineExceptionHandler)
48-
newDataCoroutineScope.firstLaunchInitDataCoroutine()
4950
dataCoroutineScope?.let {
5051
if (it.isActive) {
5152
it.cancel("FirstLaunchInitData re invoke.")
5253
}
5354
}
5455
dataCoroutineScope = newDataCoroutineScope
56+
if (firstLaunchCheckDefaultState) {
57+
if (defaultState == currentState()) {
58+
newDataCoroutineScope.firstLaunchInitDataCoroutine()
59+
}
60+
} else {
61+
newDataCoroutineScope.firstLaunchInitDataCoroutine()
62+
}
5563
}
5664

5765
abstract fun CoroutineScope.bindContentViewCoroutine(contentView: View)
5866

59-
final override fun bindContentView(contentView: View) {
67+
final override fun bindContentView(contentView: View, useLastContentView: Boolean) {
6068
val newUiCoroutineScope =
6169
CoroutineScope(Dispatchers.Main.immediate + uiCoroutineExceptionHandler)
6270
newUiCoroutineScope.bindContentViewCoroutine(contentView)
@@ -76,10 +84,16 @@ abstract class BaseCoroutineStateFragment<State : Any>(protected val defaultStat
7684
throw exception
7785
}
7886

87+
override fun onDestroyView() {
88+
super.onDestroyView()
89+
uiCoroutineScope?.cancel("Fragment content view destroyed.")
90+
uiCoroutineScope = null
91+
}
92+
7993
override fun onDestroy() {
8094
super.onDestroy()
81-
uiCoroutineScope?.cancel("Fragment destroyed.")
8295
dataCoroutineScope?.cancel("Fragment destroyed..")
96+
dataCoroutineScope = null
8397
}
8498

8599
}

tuiutils/src/main/java/com/tans/tuiutils/fragment/BaseFragment.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ abstract class BaseFragment : Fragment() {
1717
@get:LayoutRes
1818
abstract val layoutId: Int
1919

20-
protected var configureChangeCreateNewContentView: Boolean = false
20+
open val configureChangeCreateNewContentView: Boolean = false
2121

2222
override fun onCreate(savedInstanceState: Bundle?) {
2323
super.onCreate(savedInstanceState)
2424
retainInstance = true
25-
if (savedInstanceState == null) {
26-
firstLaunchInitData()
27-
}
25+
firstLaunchInitData()
2826
if (savedInstanceState != null) {
2927
val needRemoveFragments = parentFragmentManager.fragments.filter { it.tag == tag }
3028
if (needRemoveFragments.isNotEmpty()) {
@@ -40,6 +38,7 @@ abstract class BaseFragment : Fragment() {
4038
}
4139

4240
private var lastContentView: View? = null
41+
4342
override fun onCreateView(
4443
inflater: LayoutInflater,
4544
container: ViewGroup?,
@@ -50,28 +49,34 @@ abstract class BaseFragment : Fragment() {
5049
tUiUtilsLog.d(BASE_FRAGMENT_TAG, "LastContentView is null, create new ContentView")
5150
val newContentView = inflater.inflate(layoutId, container, false)
5251
this.lastContentView = newContentView
53-
bindContentView(newContentView)
52+
bindContentView(newContentView, false)
5453
newContentView
5554
} else {
56-
if (savedInstanceState != null && configureChangeCreateNewContentView) {
55+
val contentView = if (savedInstanceState != null && configureChangeCreateNewContentView) {
56+
// Configure change and need create new view.
5757
tUiUtilsLog.d(BASE_FRAGMENT_TAG, "Config changed, create new ContentView")
5858
val newContentView = inflater.inflate(layoutId, container, false)
59-
this.lastContentView = newContentView
60-
bindContentView(newContentView)
59+
bindContentView(newContentView, false)
6160
newContentView
6261
} else {
62+
// Use last content view.
63+
bindContentView(lastContentView, true)
6364
lastContentView
6465
}
66+
this.lastContentView = contentView
67+
contentView
6568
}
6669
}
6770

6871
abstract fun firstLaunchInitData()
6972

70-
abstract fun bindContentView(contentView: View)
73+
abstract fun bindContentView(contentView: View, useLastContentView: Boolean)
7174

7275
override fun onDestroy() {
7376
super.onDestroy()
74-
lastContentView = null
77+
if (configureChangeCreateNewContentView) {
78+
lastContentView = null
79+
}
7580
}
7681

7782
}

tuiutils/src/main/java/com/tans/tuiutils/fragment/BaseRx3StateFragment.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.tans.tuiutils.state.Rx3State
77
@Suppress("MemberVisibilityCanBePrivate")
88
abstract class BaseRx3StateFragment<State : Any>(protected val defaultState: State) : BaseFragment(), Rx3State<State> by Rx3State(defaultState) {
99

10+
open val firstLaunchCheckDefaultState: Boolean = true
11+
1012
protected var uiRxLife: Rx3Life? = null
1113
private set
1214

@@ -18,22 +20,34 @@ abstract class BaseRx3StateFragment<State : Any>(protected val defaultState: Sta
1820
final override fun firstLaunchInitData() {
1921
dataRxLife?.lifeCompositeDisposable?.clear()
2022
val newDataRxLife = Rx3Life()
21-
newDataRxLife.firstLaunchInitDataRx()
2223
dataRxLife = newDataRxLife
24+
if (firstLaunchCheckDefaultState) {
25+
if (defaultState == bindState().firstOrError().blockingGet()) {
26+
newDataRxLife.firstLaunchInitDataRx()
27+
}
28+
} else {
29+
newDataRxLife.firstLaunchInitDataRx()
30+
}
2331
}
2432

2533
abstract fun Rx3Life.bindContentViewRx(contentView: View)
2634

27-
final override fun bindContentView(contentView: View) {
35+
final override fun bindContentView(contentView: View, useLastContentView: Boolean) {
2836
uiRxLife?.lifeCompositeDisposable?.clear()
2937
val newRxUiLife = Rx3Life()
3038
newRxUiLife.bindContentViewRx(contentView)
3139
uiRxLife = newRxUiLife
3240
}
3341

42+
override fun onDestroyView() {
43+
super.onDestroyView()
44+
uiRxLife?.lifeCompositeDisposable?.clear()
45+
uiRxLife = null
46+
}
47+
3448
override fun onDestroy() {
3549
super.onDestroy()
36-
uiRxLife?.lifeCompositeDisposable?.clear()
3750
dataRxLife?.lifeCompositeDisposable?.clear()
51+
dataRxLife = null
3852
}
3953
}

0 commit comments

Comments
 (0)