Skip to content

Commit

Permalink
feat: fragment general result and new intent
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyMuse committed Jun 18, 2021
1 parent 66ae8ac commit fb9c65c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,178 @@ import androidx.fragment.app.setFragmentResultListener
* Created by crazy on 9/15/20 to long live and prosper !
*/

inline fun Fragment.fragmentBooleanResult(requestKey: String, bundleKey: String, crossinline onDenied: () -> Unit = {}, crossinline onGranted: () -> Unit) {
inline fun Fragment.fragmentBooleanResult(
requestKey: String,
bundleKey: String,
crossinline onDenied: () -> Unit = {},
crossinline onGranted: () -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
if (bundle.getBoolean(bundleKey, false)) onGranted() else onDenied()
}
}

inline fun Fragment.fragmentStringResult(requestKey: String, bundleKey: String, defaultValue: String? = null, crossinline action: String?.() -> Unit) {
inline fun Fragment.fragmentStringResult(
requestKey: String,
bundleKey: String,
defaultValue: String? = null,
crossinline action: String?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getString(bundleKey, defaultValue).action()
}
}

inline fun Fragment.fragmentBooleanResult(requestKey: String, bundleKey: String, defaultValue: Boolean = false, crossinline action: Boolean.() -> Unit) {
inline fun Fragment.fragmentBooleanResult(
requestKey: String,
bundleKey: String,
defaultValue: Boolean = false,
crossinline action: Boolean.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getBoolean(bundleKey, defaultValue).action()
}
}

inline fun Fragment.fragmentStringArrayListResult(requestKey: String, bundleKey: String, crossinline action: ArrayList<String>?.() -> Unit) {
inline fun Fragment.fragmentStringArrayListResult(
requestKey: String,
bundleKey: String,
crossinline action: ArrayList<String>?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getStringArrayList(bundleKey).action()
}
}

inline fun Fragment.fragmentByteArrayResult(requestKey: String, bundleKey: String, crossinline action: ByteArray?.() -> Unit) {
inline fun Fragment.fragmentByteArrayResult(
requestKey: String,
bundleKey: String,
crossinline action: ByteArray?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getByteArray(bundleKey).action()
}
}

inline fun Fragment.fragmentByteResult(requestKey: String, bundleKey: String, crossinline action: Byte?.() -> Unit) {
inline fun Fragment.fragmentByteResult(
requestKey: String,
bundleKey: String,
crossinline action: Byte?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getByte(bundleKey).action()
}
}

inline fun Fragment.fragmentCharResult(requestKey: String, bundleKey: String, crossinline action: Char?.() -> Unit) {
inline fun Fragment.fragmentCharResult(
requestKey: String,
bundleKey: String,
crossinline action: Char?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getChar(bundleKey).action()
}
}

inline fun Fragment.fragmentCharArrayResult(requestKey: String, bundleKey: String, crossinline action: CharArray?.() -> Unit) {
inline fun Fragment.fragmentCharArrayResult(
requestKey: String,
bundleKey: String,
crossinline action: CharArray?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getCharArray(bundleKey).action()
}
}

inline fun <T : Parcelable> Fragment.fragmentParcelableResult(requestKey: String, bundleKey: String, crossinline action: T?.() -> Unit) {
inline fun <T : Parcelable> Fragment.fragmentParcelableResult(
requestKey: String,
bundleKey: String,
crossinline action: T?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getParcelable<T>(bundleKey).action()
}
}

inline fun <T : Parcelable> Fragment.fragmentParcelableListResult(requestKey: String, bundleKey: String, crossinline action: ArrayList<T>?.() -> Unit) {
inline fun <T : Parcelable> Fragment.fragmentParcelableListResult(
requestKey: String,
bundleKey: String,
crossinline action: ArrayList<T>?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getParcelableArrayList<T>(bundleKey).action()
}
}

inline fun Fragment.fragmentDoubleResult(requestKey: String, bundleKey: String, crossinline action: Double?.() -> Unit) {
inline fun Fragment.fragmentDoubleResult(
requestKey: String,
bundleKey: String,
crossinline action: Double?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getDouble(bundleKey).action()
}
}

inline fun Fragment.fragmentDoubleArrayResult(requestKey: String, bundleKey: String, crossinline action: DoubleArray?.() -> Unit) {
inline fun Fragment.fragmentDoubleArrayResult(
requestKey: String,
bundleKey: String,
crossinline action: DoubleArray?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getDoubleArray(bundleKey).action()
}
}


inline fun Fragment.fragmentFloatResult(requestKey: String, bundleKey: String, crossinline action: Float?.() -> Unit) {
inline fun Fragment.fragmentFloatResult(
requestKey: String,
bundleKey: String,
crossinline action: Float?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getFloat(bundleKey).action()
}
}

inline fun Fragment.fragmentFloatArrayResult(requestKey: String, bundleKey: String, crossinline action: FloatArray?.() -> Unit) {
inline fun Fragment.fragmentFloatArrayResult(
requestKey: String,
bundleKey: String,
crossinline action: FloatArray?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getFloatArray(bundleKey).action()
}
}


inline fun Fragment.fragmentIntResult(requestKey: String, bundleKey: String, crossinline action: Int?.() -> Unit) {
inline fun Fragment.fragmentIntResult(
requestKey: String,
bundleKey: String,
crossinline action: Int?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getInt(bundleKey).action()
}
}

inline fun Fragment.fragmentIntArrayResult(requestKey: String, bundleKey: String, crossinline action: IntArray?.() -> Unit) {
inline fun Fragment.fragmentIntArrayResult(
requestKey: String,
bundleKey: String,
crossinline action: IntArray?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
bundle.getIntArray(bundleKey).action()
}
}

inline fun <T : Any?> Fragment.fragmentResult(
requestKey: String,
bundleKey: String,
crossinline action: T?.() -> Unit
) {
setFragmentResultListener(requestKey) { _, bundle ->
val value = bundle[bundleKey] as? T
value.action()
}
}
47 changes: 32 additions & 15 deletions intent/src/main/java/com/crazylegend/intent/IntentExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ fun Intent.canBeHandled(context: Context) = this.resolveActivity(context.package
fun Context.intentCanBeHandled(intent: Intent) = intent.resolveActivity(packageManager) != null


fun Context.getActivityPendingIntent(requestCode: Int = 0, intent: Intent, flags: Int = PendingIntent.FLAG_ONE_SHOT): PendingIntent =
PendingIntent.getActivity(this, requestCode, intent, flags)

fun Context.getBroadcastPendingIntent(requestCode: Int = 0, intent: Intent, flags: Int = PendingIntent.FLAG_ONE_SHOT): PendingIntent =
PendingIntent.getBroadcast(this, requestCode, intent, flags)
fun Context.getActivityPendingIntent(
requestCode: Int = 0,
intent: Intent,
flags: Int = PendingIntent.FLAG_ONE_SHOT
): PendingIntent =
PendingIntent.getActivity(this, requestCode, intent, flags)

fun Context.getBroadcastPendingIntent(
requestCode: Int = 0,
intent: Intent,
flags: Int = PendingIntent.FLAG_ONE_SHOT
): PendingIntent =
PendingIntent.getBroadcast(this, requestCode, intent, flags)

fun Context.getProductionApplicationId(): String {
val applicationId = packageName
Expand All @@ -78,7 +86,8 @@ fun Context.getProductionApplicationId(): String {

fun Context?.openGoogleMaps(query: String, placeId: String) {
val queryEncoded = Uri.encode(query)
val gmmIntentUri = Uri.parse("https://www.google.com/maps/search/?api=1&query=$queryEncoded&query_place_id=$placeId")
val gmmIntentUri =
Uri.parse("https://www.google.com/maps/search/?api=1&query=$queryEncoded&query_place_id=$placeId")
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.maps")
this?.apply {
Expand All @@ -89,26 +98,34 @@ fun Context?.openGoogleMaps(query: String, placeId: String) {

}

inline fun Activity.pickImage(PICK_IMAGES_CODE: Int, allowMultiple: Boolean = false, title: String = "Pick images",
onCantHandleIntent: () -> Unit = {}) {
inline fun Activity.pickImage(
PICK_IMAGES_CODE: Int, allowMultiple: Boolean = false, title: String = "Pick images",
onCantHandleIntent: () -> Unit = {}
) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple)
.setType("image/*")
.addCategory(Intent.CATEGORY_OPENABLE)
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple)
.setType("image/*")
if (intentCanBeHandled(intent))
startActivityForResult(Intent.createChooser(intent, title), PICK_IMAGES_CODE)
else
onCantHandleIntent()
}

inline fun Activity.openFile(mimeType: String, requestCode: Int, title: String = "Select file via",
onCantHandleIntent: () -> Unit = {}) {
inline fun Activity.openFile(
mimeType: String, requestCode: Int, title: String = "Select file via",
onCantHandleIntent: () -> Unit = {}
) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType(mimeType)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType(mimeType)

if (intentCanBeHandled(intent))
startActivityForResult(Intent.createChooser(intent, title), requestCode)
else
onCantHandleIntent()
}

inline fun <reified T> Context.newIntent(): Intent {
return Intent(this, T::class.java)
}

0 comments on commit fb9c65c

Please sign in to comment.