Skip to content
This repository was archived by the owner on Apr 18, 2021. It is now read-only.

Commit b6b0c1d

Browse files
committed
Optimize exception message when depends on support library version is older than 25.1.0.
1 parent 8771295 commit b6b0c1d

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

MainScope/src/main/java/com/bennyhuo/kotlin/coroutines/android/mainscope/MainScope.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface MainScope: CoroutineScope{
1818

1919
val isFragmentSupported by lazy {
2020
try {
21-
Class.forName("android.support.v4.app.Fragment")
21+
Class.forName("android.support.v4.app.FragmentManager\$FragmentLifecycleCallbacks")
2222
Logcat.debug("Fragment enabled.")
2323
true
2424
}catch (e: ClassNotFoundException){
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.bennyhuo.kotlin.coroutines.android.mainscope.exception
2+
3+
class UnsupportedTypeException(type: Class<*>, vararg supportedTypes: String)
4+
: Exception("Unsupported type: $type. ${supportedTypes.joinToString()} ${if(supportedTypes.size == 1) "is" else "are" } needed.")
5+
6+
class UnsupportedVersionException(library: String, version: String): Exception("Unsupported version: $version of $library")

MainScope/src/main/java/com/bennyhuo/kotlin/coroutines/android/mainscope/scope/MainScoped.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import android.support.v4.app.Fragment
66
import com.bennyhuo.kotlin.coroutines.android.mainscope.EmptyScope
77
import com.bennyhuo.kotlin.coroutines.android.mainscope.MainScope
88
import com.bennyhuo.kotlin.coroutines.android.mainscope.MainScopeImpl
9+
import com.bennyhuo.kotlin.coroutines.android.mainscope.exception.UnsupportedTypeException
10+
import com.bennyhuo.kotlin.coroutines.android.mainscope.exception.UnsupportedVersionException
911
import com.bennyhuo.kotlin.coroutines.android.mainscope.utils.Logcat
1012
import kotlinx.coroutines.cancel
1113
import java.util.*
@@ -44,7 +46,17 @@ private fun MainScoped.isDestroyed(): Boolean {
4446
this.activity?.isFinishing?: true || this.isRemoving ||this.view == null
4547
}
4648
else ->{
47-
throw IllegalAccessException("An Activity or android.support.v4.app.Fragment is needed!")
49+
val fragmentClass = try {
50+
Class.forName("android.support.v4.app.Fragment")
51+
} catch (e: Exception) {
52+
null
53+
}
54+
fragmentClass?.let {
55+
if(it.isAssignableFrom(this.javaClass)){
56+
throw UnsupportedVersionException("com.android.support:support-fragment", "<25.1.0")
57+
}
58+
}
59+
throw UnsupportedTypeException(this.javaClass, "android.app.Activity", "android.support.v4.app.Fragment")
4860
}
4961
}
5062
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ Most of the listeners borrowed from [Anko](https://github.com/kotlin/anko) are e
118118

119119
Since `android.app.Fragment` is deprecated, we choose to support `android.support.v4.app.Fragment` only. If you include the library into the classpath, Fragment support will be automatically enabled for subtypes of `FragmentActivity`. Otherwise if you never use Fragment, don't worry, nothing will happen.
120120

121+
`FragmentLifecycleCallbacks` was added from v25.1.0 so older versions will not be supported.
122+
121123
## Issue
122124

123125
Please feel free to issue and pull request.

app-fragment/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ android {
2222
}
2323
}
2424

25+
def android_support_version_sample = "25.1.0"
26+
2527
dependencies {
2628
implementation fileTree(dir: 'libs', include: ['*.jar'])
2729
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2830
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
29-
implementation "com.android.support:recyclerview-v7:$android_support_version"
30-
implementation "com.android.support:design:$android_support_version"
31-
implementation "com.android.support:appcompat-v7:$android_support_version"
31+
implementation "com.android.support:recyclerview-v7:$android_support_version_sample"
32+
implementation "com.android.support:design:$android_support_version_sample"
33+
implementation "com.android.support:appcompat-v7:$android_support_version_sample"
3234

3335
testImplementation 'junit:junit:4.12'
3436
androidTestImplementation 'com.android.support.test:runner:1.0.2'

app-fragment/src/main/java/com/bennyhuo/kotlin/coroutines/android/sample/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MainActivity : AppCompatActivity(), BasicScoped {
1313

1414
button.onClick {
1515
supportFragmentManager.beginTransaction()
16-
.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out)
16+
.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out)
1717
.add(R.id.fragmentContainer, MainFragment())
1818
.addToBackStack("Main").commit()
1919
}

0 commit comments

Comments
 (0)