-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace kotlin synthetic with view binding
- Loading branch information
Showing
13 changed files
with
140 additions
and
67 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
11 changes: 2 additions & 9 deletions
11
presentation/src/main/kotlin/app/web/drjackycv/presentation/base/adapter/Cell.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 |
---|---|---|
@@ -1,27 +1,20 @@ | ||
package app.web.drjackycv.presentation.base.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.annotation.LayoutRes | ||
import androidx.recyclerview.widget.RecyclerView | ||
import app.web.drjackycv.domain.base.RecyclerItem | ||
|
||
abstract class Cell<T> { | ||
abstract class Cell<T, R> { | ||
|
||
abstract fun belongsTo(item: T?): Boolean | ||
abstract fun type(): Int | ||
abstract fun binding(parent: ViewGroup): R | ||
abstract fun holder(parent: ViewGroup): RecyclerView.ViewHolder | ||
abstract fun bind( | ||
holder: RecyclerView.ViewHolder, | ||
item: T?, | ||
onItemClick: ((RecyclerItem, View) -> Unit)? | ||
) | ||
|
||
protected fun ViewGroup.viewOf(@LayoutRes resource: Int): View { | ||
return LayoutInflater | ||
.from(context) | ||
.inflate(resource, this, false) | ||
} | ||
|
||
} |
10 changes: 5 additions & 5 deletions
10
presentation/src/main/kotlin/app/web/drjackycv/presentation/base/adapter/CellTypes.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
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
8 changes: 4 additions & 4 deletions
8
...ion/src/main/kotlin/app/web/drjackycv/presentation/base/adapter/LoadingStateViewHolder.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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package app.web.drjackycv.presentation.base.adapter | ||
|
||
import android.view.View | ||
import androidx.core.view.isVisible | ||
import androidx.paging.LoadState | ||
import androidx.recyclerview.widget.RecyclerView | ||
import kotlinx.android.synthetic.main.item_loading_row.view.* | ||
import app.web.drjackycv.presentation.databinding.ItemLoadingRowBinding | ||
|
||
class LoadingStateViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { | ||
class LoadingStateViewHolder(private val itemBinding: ItemLoadingRowBinding) : | ||
RecyclerView.ViewHolder(itemBinding.root) { | ||
|
||
fun bind(loadState: LoadState) { | ||
view.itemLoadingRowContainer.isVisible = loadState is LoadState.Loading | ||
itemBinding.itemLoadingRowContainer.isVisible = loadState is LoadState.Loading | ||
} | ||
|
||
} |
64 changes: 63 additions & 1 deletion
64
presentation/src/main/kotlin/app/web/drjackycv/presentation/extension/Extensions.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 |
---|---|---|
@@ -1,9 +1,71 @@ | ||
package app.web.drjackycv.presentation.extension | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
import androidx.viewbinding.ViewBinding | ||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KProperty | ||
|
||
fun <T> LifecycleOwner.observe(liveData: LiveData<T>, action: (t: T) -> Unit) { | ||
liveData.observe(this, Observer { it?.let { t -> action(t) } }) | ||
} | ||
} | ||
|
||
/*fun <T> Fragment.viewBinding(initialise: () -> T): ReadOnlyProperty<Fragment, T> = | ||
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {*/ | ||
class FragmentViewBindingDelegate<T : ViewBinding>( | ||
val fragment: Fragment, | ||
val viewBindingFactory: (View) -> T | ||
) : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver { | ||
|
||
// A backing property to hold our value | ||
private var binding: T? = null | ||
|
||
private var viewLifecycleOwner: LifecycleOwner? = null | ||
|
||
init { | ||
// Observe the View Lifecycle of the Fragment | ||
this.fragment | ||
.viewLifecycleOwnerLiveData | ||
.observe(fragment, Observer { newLifecycleOwner -> | ||
viewLifecycleOwner | ||
?.lifecycle | ||
?.removeObserver(this) | ||
|
||
viewLifecycleOwner = newLifecycleOwner.also { | ||
it.lifecycle.addObserver(this) | ||
} | ||
}) | ||
} | ||
|
||
override fun onDestroy(owner: LifecycleOwner) { | ||
super.onDestroy(owner) | ||
binding = null | ||
|
||
} | ||
|
||
override fun getValue( | ||
thisRef: Fragment, | ||
property: KProperty<*> | ||
): T { | ||
// Return the backing property if it's set, or initialise | ||
return this.binding ?: viewBindingFactory(fragment.requireView()).also { | ||
this.binding = it | ||
} | ||
} | ||
} | ||
|
||
fun <T : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> T) = | ||
FragmentViewBindingDelegate(this, viewBindingFactory) | ||
|
||
inline fun <T : ViewBinding> AppCompatActivity.viewBinding( | ||
crossinline bindingInflater: (LayoutInflater) -> T | ||
) = | ||
lazy(LazyThreadSafetyMode.NONE) { | ||
bindingInflater.invoke(layoutInflater) | ||
} |
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
18 changes: 9 additions & 9 deletions
18
...entation/src/main/kotlin/app/web/drjackycv/presentation/products/entity/BeerViewHolder.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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
package app.web.drjackycv.presentation.products.entity | ||
|
||
import android.view.View | ||
import androidx.recyclerview.widget.RecyclerView | ||
import app.web.drjackycv.presentation.databinding.ItemProductBinding | ||
import app.web.drjackycv.presentation.extension.load | ||
import kotlinx.android.synthetic.main.item_product.view.* | ||
|
||
class BeerViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||
class BeerViewHolder(val itemBinding: ItemProductBinding) : | ||
RecyclerView.ViewHolder(itemBinding.root) { | ||
fun bind(beer: BeerUI) = with(itemView) { | ||
itemProductContainer.transitionName = beer.id.toString() | ||
itemProductIdTxv.text = beer.id.toString() | ||
itemProductImv.load(beer.imageUrl) | ||
itemProductNameTxv.text = beer.name | ||
itemProductAbvTxv.text = beer.abv.toString() | ||
//itemProductTypeTxv.text = beer.type | ||
itemBinding.itemProductContainer.transitionName = beer.id.toString() | ||
itemBinding.itemProductIdTxv.text = beer.id.toString() | ||
itemBinding.itemProductImv.load(beer.imageUrl) | ||
itemBinding.itemProductNameTxv.text = beer.name | ||
itemBinding.itemProductAbvTxv.text = beer.abv.toString() | ||
//itemBinding.itemProductTypeTxv.text = beer.type | ||
} | ||
|
||
} |
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
Oops, something went wrong.