Skip to content

Commit

Permalink
0.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Badakhshan committed Dec 25, 2023
1 parent e2a134e commit b628ca6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/alirezabdn/whygoogle/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainFragment : WhyGoogleFragment<MainFragmentBinding>() {
start(MainFragment())
}
val sec = SecondFragment().also {
it.name = "hi"
it.name = "hi there"
}
go2.setOnClickListener {
start(sec)
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/alirezabdn/whygoogle/SecondFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import com.alirezabdn.whygoogle.databinding.MainFragmentBinding
import ir.ayantech.whygoogle.fragment.WhyGoogleFragment
import ir.ayantech.whygoogle.helper.fragmentArgument
import ir.ayantech.whygoogle.fragment.WhyGoogleFragment
import ir.ayantech.whygoogle.helper.makeGone
import ir.ayantech.whygoogle.helper.nullableFragmentArgument

class SecondFragment : WhyGoogleFragment<MainFragmentBinding>() {

var name: String? by fragmentArgument()
var name: String by fragmentArgument()
var name2: String? by nullableFragmentArgument()
override fun onCreate() {
super.onCreate()
binding.go2.makeGone()
Expand Down
2 changes: 1 addition & 1 deletion whygoogle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "0.8.0"
versionName "0.8.2"
}
buildFeatures {
viewBinding true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,80 @@ package ir.ayantech.whygoogle.helper

import android.os.Bundle
import android.os.Parcelable
import ir.ayantech.whygoogle.fragment.WhyGoogleFragment
import java.io.Serializable
import androidx.fragment.app.Fragment
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
import java.io.Serializable

class FragmentArgumentDelegate<T : Any?>(
class FragmentArgumentDelegate<T : Any>(
private val defaultValue: T? = null,
private val key: String? = null
) : ReadWriteProperty<Any?, T> {
) : ReadWriteProperty<Fragment, T> {

override fun getValue(thisRef: Any?, property: KProperty<*>): T {
val arguments = (thisRef as? WhyGoogleFragment<*>)?.arguments
return arguments?.get(key ?: property.name) as? T
override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
val finalKey = this.key ?: property.name
return thisRef.arguments?.get(finalKey) as? T
?: defaultValue
?: throw IllegalStateException("Property ${property.name} not initialized")
}

override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
val arguments = (thisRef as? WhyGoogleFragment<*>)?.arguments ?: Bundle()
override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) {
val finalKey = this.key ?: property.name
val arguments = thisRef.arguments ?: Bundle()
arguments.putArgument(finalKey, value)
thisRef.arguments = arguments
}
}

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)
}
class FragmentNullableArgumentDelegate<T : Any?>(
private val defaultValue: T?,
private val key: String? = null
) : ReadWriteProperty<Fragment, T> {

(thisRef as? WhyGoogleFragment<*>)?.arguments = arguments
override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
val finalKey = this.key ?: property.name
return thisRef.arguments?.get(finalKey) as? T ?: defaultValue ?: getDefault(property)
}

override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) {
val finalKey = this.key ?: property.name
val arguments = thisRef.arguments ?: Bundle()
arguments.putArgument(finalKey, value)
thisRef.arguments = arguments
}

private fun getDefault(property: KProperty<*>): T {
if (property.returnType.isMarkedNullable) {
@Suppress("UNCHECKED_CAST")
return null as T
} else {
throw IllegalStateException("Property ${property.name} not initialized")
}
}
}

inline fun <reified T : Any?> fragmentArgument(key: String? = null) = FragmentArgumentDelegate<T>(key)
inline fun <reified T : Any> fragmentArgument(defaultValue: T? = null, key: String? = null) =
FragmentArgumentDelegate(defaultValue, key)

inline fun <reified T : Any?> nullableFragmentArgument(
defaultValue: T? = null,
key: String? = null
) = FragmentNullableArgumentDelegate(defaultValue, key)

fun Bundle.putArgument(key: String?, value: Any?) {
when (value) {
null -> putSerializable(key, null)
is String -> putString(key, value)
is Int -> putInt(key, value)
is Long -> putLong(key, value)
is Double -> putDouble(key, value)
is Boolean -> putBoolean(key, value)
is Float -> putFloat(key, value)
is Char -> putChar(key, value)
is Short -> putShort(key, value)
is Byte -> putByte(key, value)
is Serializable -> putSerializable(key, value)
is Parcelable -> putParcelable(key, value)
else -> throw IllegalArgumentException("Unsupported argument type: ${value::class.java.simpleName}")
}
}

0 comments on commit b628ca6

Please sign in to comment.