This file contains hidden or 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
| object TFCViewModelFactory : ViewModelProvider.Factory { | |
| lateinit var application: Application | |
| lateinit var dependencies: UseCases | |
| fun inject(application: Application, dependencies: UseCases) { | |
| TFCViewModelFactory.application = application | |
| TFCViewModelFactory.dependencies = dependencies | |
| } | |
| override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
| if(BaseViewModel::class.java.isAssignableFrom(modelClass)) { | |
| return modelClass.getConstructor(Application::class.java, UseCases::class.java) |
This file contains hidden or 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 TFCApplication : Application() { | |
| @ExperimentalSerializationApi | |
| override fun onCreate() { | |
| super.onCreate() | |
| val apiService = Retrofit.Builder().baseUrl(Constant.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build().create(ApiService::class.java) | |
| val coinRepository = CoinRepository(CoinDataSource(apiService)) | |
| TFCViewModelFactory.inject(this, UseCases(GetAllCoinsUseCase(coinRepository = coinRepository))) | |
| } |
This file contains hidden or 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
| open class BaseViewModel(application: Application, protected val useCases: UseCases) : AndroidViewModel(application) { | |
| protected val application: TFCApplication = getApplication() | |
| } |
This file contains hidden or 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
| data class UseCases(val getAllCoinsUseCase: GetAllCoinsUseCase) |
This file contains hidden or 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
| /** | |
| * Implementation class of coin data source | |
| */ | |
| class CoinDataSource(private val apiService: ApiService) : CoinDataSource { | |
| override suspend fun getAll(): List<CoinEntity> { | |
| return apiService.getAllCoinsFirst100() | |
| } | |
| } |
This file contains hidden or 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
| /** | |
| * Mapper class for Coin | |
| */ class CoinMapper @Inject constructor() : Mapper<CoinNetwork, CoinEntity> { | |
| override fun from(e: CoinEntity?): CoinNetwork { | |
| return CoinNetwork( | |
| current_price = e?.current_price, | |
| image = e?.image, | |
| name = e?.name, | |
| price_change_percentage_24h = e?.price_change_percentage_24h, | |
| symbol = e?.symbol |
This file contains hidden or 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 Mapper<T, E> { | |
| fun from(e: E?): T | |
| fun to(t: T?): E | |
| } |
This file contains hidden or 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
| /** | |
| * Service and Endpoints | |
| */ | |
| interface ApiService { | |
| @GET("coins/markets") | |
| suspend fun getAllCoinsFirst100( | |
| @Query("vs_currency") vs_currency: String = "usd", | |
| @Query("order") order: String = "market_cap_desc", | |
| @Query("per_page") perPage: Int = 3, | |
| @Query("page") page: Int = 1, |
This file contains hidden or 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 CoinRepository(private val dataSource: CoinDataSource): ICoinRepository { | |
| override suspend fun getAll(): List<CoinEntity> { | |
| return dataSource.getAll() | |
| } | |
| } |
This file contains hidden or 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 CoinDataSource { | |
| suspend fun getAll(): List<CoinEntity> | |
| } |
NewerOlder