Skip to content

Instantly share code, notes, and snippets.

Avatar

Ahmad Jailani ajailani4

View GitHub Profile
View HomeFragment.kt
@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?
View HomeViewModel.kt
@HiltViewModel
class HomeViewModel @Inject constructor(
private val mainRepository: MainRepository
) : ViewModel() {
fun getBrandsHome() = mainRepository.getBrandsHome().cachedIn(viewModelScope)
}
View MainRepository.kt
class MainRepository @Inject constructor(
private val apiHelper: ApiHelper
) {
fun getBrandsHome() =
Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 5),
pagingSourceFactory = {
BrandsHomeDataSource(apiHelper)
}
).flow
View FooterAdapter.kt
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
)
View PhonesHomeAdapter.kt
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) {
View BrandsHomeAdapter.kt
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 {
View BrandsHomeDataSource.kt
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()
View item_phone_home.xml
<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"
View item_brand_home.xml
<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"
View fragment_home.xml
<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">