-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package com.br3ant.tools | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class MainActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.br3ant.utils | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import java.io.Serializable | ||
|
||
/** | ||
* <pre> | ||
* copyright: datedu | ||
* @author : br3ant | ||
* e-mail : xxx@xx | ||
* time : 2020/10/22 | ||
* desc : | ||
* version: 1.0 | ||
* </pre> | ||
*/ | ||
inline fun <reified T : Context> Context.getIntent(vararg pairs: Pair<String, Any>): Intent = | ||
Intent(this, T::class.java).apply { | ||
pairs.forEach { pair -> | ||
val name = pair.first | ||
when (val value = pair.second) { | ||
is Int -> putExtra(name, value) | ||
is Byte -> putExtra(name, value) | ||
is Char -> putExtra(name, value) | ||
is Short -> putExtra(name, value) | ||
is Boolean -> putExtra(name, value) | ||
is Long -> putExtra(name, value) | ||
is Float -> putExtra(name, value) | ||
is Double -> putExtra(name, value) | ||
is String -> putExtra(name, value) | ||
is CharSequence -> putExtra(name, value) | ||
is Parcelable -> putExtra(name, value) | ||
is Array<*> -> putExtra(name, value) | ||
is ArrayList<*> -> putExtra(name, value) | ||
is Serializable -> putExtra(name, value) | ||
is BooleanArray -> putExtra(name, value) | ||
is ByteArray -> putExtra(name, value) | ||
is ShortArray -> putExtra(name, value) | ||
is CharArray -> putExtra(name, value) | ||
is IntArray -> putExtra(name, value) | ||
is LongArray -> putExtra(name, value) | ||
is FloatArray -> putExtra(name, value) | ||
is DoubleArray -> putExtra(name, value) | ||
is Bundle -> putExtra(name, value) | ||
is Intent -> putExtra(name, value) | ||
else -> { | ||
} | ||
} | ||
} | ||
} |
81 changes: 0 additions & 81 deletions
81
utils/src/main/java/com/br3ant/utils/launcher/DLauncher.java
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
utils/src/main/java/com/br3ant/utils/launcher/DLauncher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.br3ant.utils.launcher | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentActivity | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
/** | ||
* <pre> | ||
* copyright: datedu | ||
* @author : houqiqi | ||
* e-mail : xxx@xx | ||
* time : 2019-11-21 | ||
* desc : Activity跳转封装类,把OnActivityResult方式改为Callback方式 | ||
* version: 1.0 | ||
</pre> * | ||
*/ | ||
class DLauncher constructor(activity: FragmentActivity) { | ||
private val mContext: Context | ||
private val mRouterFragmentX: DRouterX? | ||
|
||
constructor(fragment: Fragment) : this(fragment.requireActivity()) | ||
|
||
constructor(activity: Activity) : this(activity as FragmentActivity) | ||
|
||
|
||
private fun getRouterFragmentX(activity: FragmentActivity): DRouterX? { | ||
|
||
return findRouterFragmentX(activity) ?: DRouterX.newInstance().also { | ||
val fragmentManager = activity.supportFragmentManager | ||
fragmentManager | ||
.beginTransaction() | ||
.add(it, TAG) | ||
.commitAllowingStateLoss() | ||
fragmentManager.executePendingTransactions() | ||
} | ||
} | ||
|
||
private fun findRouterFragmentX(activity: FragmentActivity): DRouterX? { | ||
return activity.supportFragmentManager.findFragmentByTag(TAG) as DRouterX? | ||
} | ||
|
||
fun startActivityForResult(clazz: Class<*>?, callback: Callback?) { | ||
val intent = Intent(mContext, clazz) | ||
startActivityForResult(intent, callback) | ||
} | ||
|
||
fun startActivityForResult(intent: Intent?, callback: Callback?) { | ||
if (mRouterFragmentX != null) { | ||
mRouterFragmentX.startActivityForResult(intent, callback) | ||
} else { | ||
throw RuntimeException("please do init first!") | ||
} | ||
} | ||
|
||
suspend fun launcher(intent: Intent?): Bundle? = | ||
suspendCancellableCoroutine { continuation -> | ||
try { | ||
startActivityForResult(intent) { _, data -> | ||
continuation.resume(data?.extras) | ||
} | ||
} catch (e: Exception) { | ||
continuation.resumeWithException(e) | ||
} | ||
} | ||
|
||
fun interface Callback { | ||
fun onActivityResult(resultCode: Int, data: Intent?) | ||
} | ||
|
||
companion object { | ||
private const val TAG = "DLauncher" | ||
} | ||
|
||
init { | ||
mContext = activity | ||
mRouterFragmentX = getRouterFragmentX(activity) | ||
} | ||
} |
70 changes: 0 additions & 70 deletions
70
utils/src/main/java/com/br3ant/utils/launcher/DRouterX.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.