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
// for LiveData | |
override suspend fun getCryptoList(): LiveData<PagingData<CryptoItemResponse>> { | |
return Pager( | |
config = PagingConfig( | |
pageSize = NETWORK_PAGE_SIZE, | |
enablePlaceholders = false | |
), | |
pagingSourceFactory = { | |
CryptoListPagingSource(service, mapper) | |
} |
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 CryptoListAdapter(private val cryptoClickListener: (cryptoItem: CryptoItemResponse) -> Unit) : | |
PagingDataAdapter<CryptoItemResponse, CryptoListAdapter.ViewHolder>(DiffUtilCallBack) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
val binding = DataBindingUtil.inflate<ItemCryptoBinding>( | |
LayoutInflater.from(parent.context), | |
R.layout.item_crypto, | |
parent, | |
false | |
) |
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
private val cryptoListAdapter by lazy { | |
CryptoListAdapter { | |
showToastMessage(getString(R.string.item_clicked, it.symbol)) | |
} | |
} | |
private fun initView() { | |
... | |
// initial recyclerView |
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 CryptoListViewModel(private val repository: CryptoListRepository) : BaseViewModel() { | |
private val _cryptoList = MutableLiveData<PagingData<CryptoItemResponse>>() | |
suspend fun getCryptoList(): LiveData<PagingData<CryptoItemResponse>> { | |
val response = repository.getCryptoList().cachedIn(viewModelScope) | |
_cryptoList.value = response.value | |
return response | |
} | |
} |
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
const val NETWORK_PAGE_SIZE = 500 | |
private const val INITIAL_LOAD_SIZE = 1 | |
class CryptoListPagingSource( | |
private val service: APIService, | |
private val mapper: CryptoListMapper | |
) : PagingSource<Int, CryptoItemResponse>() { | |
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CryptoItemResponse> { | |
// Start refresh at position 1 if undefined. |
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
interface CryptoListRepository { | |
suspend fun getCryptoList(): LiveData<PagingData<CryptoItemResponse>> | |
} | |
class CryptoRepositoryImpl( | |
private val service: APIService, | |
private val mapper: CryptoListMapper | |
) : CryptoListRepository { | |
override suspend fun getCryptoList(): LiveData<PagingData<CryptoItemResponse>> { | |
return Pager( |
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
private fun getPaymentDetailToString(payment: Payment): String { | |
return when (payment) { | |
is Payment.CreditCard -> | |
"Credit Card \nName: ${payment.name} \nCard Number: ${payment.number} \nExpired Date: ${payment.expiredDate} \nTransfer Amount: ${payment.transferAmount}" | |
is Payment.CashBank -> | |
"Cash Bank \nBank Code: ${payment.bankCode} \nBank Name: ${payment.bankName} \nTransfer Amount: ${payment.transferAmount}" | |
is Payment.Promptpay -> | |
"Promptpay \nName: ${payment.name} \nCitizen Id: ${payment.citizenId} \nMobile Number: ${payment.mobileNumber} \nTransfer Amount: ${payment.transferAmount}" | |
Payment.NonePayment -> "None Payment" | |
} |
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 MainViewModel : ViewModel() { | |
private val _paymentData = MutableLiveData<Payment>() | |
val paymentData = _paymentData | |
private val paymentByCreditCardData: Payment = Payment.CreditCard( | |
name = "AnnChar", | |
number = "123456789123", | |
expiredDate = "02/23", | |
transferAmount = 100.50 | |
) |
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
enum class PaymentEnumNormal { | |
CREDIT_CARD, CASH_BANK, PROMTPAY, NONE | |
} | |
data class CreditCard( | |
val name: String, | |
val number: String, | |
val expiredDate: String, | |
val transferAmount: Double | |
) |
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
sealed class Payment { | |
data class CreditCard( | |
val name: String, | |
val number: String, | |
val expiredDate: String, | |
val transferAmount: Double | |
) : Payment() | |
data class CashBank( | |
val bankCode: String, |
NewerOlder