Skip to content

Instantly share code, notes, and snippets.

View ajailani4's full-sized avatar

Ahmad Jailani ajailani4

View GitHub Profile
@AndroidEntryPoint
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private val homeViewModel: HomeViewModel by viewModels()
private lateinit var brandHomeAdapter: BrandsHomeAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
@HiltViewModel
class HomeViewModel @Inject constructor(
private val mainRepository: MainRepository
) : ViewModel() {
fun getBrandsHome() = mainRepository.getBrandsHome().cachedIn(viewModelScope)
}
class MainRepository @Inject constructor(
private val apiHelper: ApiHelper
) {
fun getBrandsHome() =
Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 5),
pagingSourceFactory = {
BrandsHomeDataSource(apiHelper)
}
).flow
class FooterAdapter : LoadStateAdapter<FooterAdapter.ViewHolder>() {
private lateinit var binding: FooterProgressBarBinding
class ViewHolder(binding: FooterProgressBarBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): ViewHolder {
binding = FooterProgressBarBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
class PhonesHomeAdapter(
private val phonesHomeList: List<Phone>?,
private val listener: (String) -> Unit
) : RecyclerView.Adapter<PhonesHomeAdapter.ViewHolder>() {
private lateinit var binding: ItemPhoneHomeBinding
class ViewHolder(private val binding: ItemPhoneHomeBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(phone: Phone?, listener: (String) -> Unit) {
class BrandsHomeAdapter(
private val moreListener: (Brand) -> Unit,
private val phoneListener: (String, String) -> Unit
) : PagingDataAdapter<Brand, BrandsHomeAdapter.ViewHolder>(DataDifferentiator) {
private lateinit var binding: ItemBrandHomeBinding
private lateinit var phonesHomeAdapter: PhonesHomeAdapter
private val rvViewPool = RecyclerView.RecycledViewPool()
object DataDifferentiator : DiffUtil.ItemCallback<Brand>() {
override fun areItemsTheSame(oldItem: Brand, newItem: Brand): Boolean {
class BrandsHomeDataSource @Inject constructor(
private val apiHelper: ApiHelper
) : PagingSource<Int, Brand>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Brand> {
val currentLoadingPageKey = params.key ?: 1
return try {
val response = apiHelper.getBrands(currentLoadingPageKey, 5)
val data = response.body()?.data?.brands ?: emptyList()
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="230dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:clickable="true"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.view.fragment.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">