Skip to content

Instantly share code, notes, and snippets.

View cdmunoz's full-sized avatar

Carlos Daniel cdmunoz

View GitHub Profile
@Module
class AppModule(val app: Application) {
@Provides
@Singleton
fun provideApplication(): Application = app
@Provides
@Singleton
fun provideCryptocurrenciesDatabase(app: Application): Database = Room.databaseBuilder(app,
dependencies {
//android libs
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.android.support.constraint:constraint-layout:${rootProject.ext.constraintLayoutVersion}"
//moshi
implementation "com.squareup.moshi:moshi-kotlin:${rootProject.ext.moshiKotlinVersion}"
implementation "com.squareup.moshi:moshi-adapters:${rootProject.ext.moshiKotlinVersion}"
//dagger2
class ApiClient {
companion object {
private const val BASE_URL = "https://api.coinmarketcap.com/v1/"
fun getClient(): Retrofit {
val okHttpClient = OkHttpClient.Builder().build()
val moshi = Moshi.Builder().build()
interface ApiInterface {
@GET("ticker/")
fun getCryptocurrencies(@Query("start") start: String): Observable<List<Cryptocurrency>>
}
class CryptocurrenciesActivity : AppCompatActivity() {
val compositeDisposable = CompositeDisposable()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showCryptocurrencies()
}
@Module
class NetModule(private val baseUrl: String) {
@Provides
@Singleton
fun providesOkHttpClient(): OkHttpClient = OkHttpClient.Builder().build()
@Provides
@Singleton
fun providesMoshi(): Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
@Singleton
@Component(
modules = arrayOf(AndroidInjectionModule::class, BuildersModule::class, AppModule::class,
NetModule::class)
)
interface AppComponent {
fun inject(app: CryptocurrencyApplication)
}
class CryptocurrencyApplication: Application(), HasActivityInjector {
@Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity>
override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder()
.appModule(AppModule(this))
.netModule(NetModule(BuildConfig.URL))
class CryptocurrencyRepository @Inject constructor(val apiInterface: ApiInterface,
val cryptocurrenciesDao: CryptocurrenciesDao) {
fun getCryptocurrencies(): Observable<List<Cryptocurrency>> {
val observableFromApi = getCryptocurrenciesFromApi()
val observableFromDb = getCryptocurrenciesFromDb()
return Observable.concatArrayEager(observableFromApi, observableFromDb)
}
fun getCryptocurrenciesFromApi(): Observable<List<Cryptocurrency>> {
class CryptocurrenciesViewModel @Inject constructor(
private val cryptocurrencyRepository: CryptocurrencyRepository) : ViewModel() {
var cryptocurrenciesResult: MutableLiveData<List<Cryptocurrency>> = MutableLiveData()
var cryptocurrenciesError: MutableLiveData<String> = MutableLiveData()
lateinit var disposableObserver: DisposableObserver<List<Cryptocurrency>>
fun cryptocurrenciesResult(): LiveData<List<Cryptocurrency>> {
return cryptocurrenciesResult
}