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