-
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
A.Badakhshan
committed
Dec 23, 2023
1 parent
bc72468
commit e2a134e
Showing
6 changed files
with
82 additions
and
34 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
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
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
42 changes: 42 additions & 0 deletions
42
whygoogle/src/main/java/ir/ayantech/whygoogle/helper/FragmentArgumentDelegate.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,42 @@ | ||
package ir.ayantech.whygoogle.helper | ||
|
||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import ir.ayantech.whygoogle.fragment.WhyGoogleFragment | ||
import java.io.Serializable | ||
import kotlin.properties.ReadWriteProperty | ||
import kotlin.reflect.KProperty | ||
|
||
class FragmentArgumentDelegate<T : Any?>( | ||
private val key: String? = null | ||
) : ReadWriteProperty<Any?, T> { | ||
|
||
override fun getValue(thisRef: Any?, property: KProperty<*>): T { | ||
val arguments = (thisRef as? WhyGoogleFragment<*>)?.arguments | ||
return arguments?.get(key ?: property.name) as? T | ||
?: throw IllegalStateException("Property ${property.name} not initialized") | ||
} | ||
|
||
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { | ||
val arguments = (thisRef as? WhyGoogleFragment<*>)?.arguments ?: Bundle() | ||
val finalKey = this.key ?: property.name | ||
|
||
when (value) { | ||
is String -> arguments.putString(finalKey, value) | ||
is Int -> arguments.putInt(finalKey, value) | ||
is Long -> arguments.putLong(finalKey, value) | ||
is Double -> arguments.putDouble(finalKey, value) | ||
is Boolean -> arguments.putBoolean(finalKey, value) | ||
is Float -> arguments.putFloat(finalKey, value) | ||
is Char -> arguments.putChar(finalKey, value) | ||
is Short -> arguments.putShort(finalKey, value) | ||
is Byte -> arguments.putByte(finalKey, value) | ||
is Serializable -> arguments.putSerializable(finalKey, value) | ||
is Parcelable -> arguments.putParcelable(finalKey, value) | ||
} | ||
|
||
(thisRef as? WhyGoogleFragment<*>)?.arguments = arguments | ||
} | ||
} | ||
|
||
inline fun <reified T : Any?> fragmentArgument(key: String? = null) = FragmentArgumentDelegate<T>(key) |