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
@Module | |
class AppModule(val app: Application) { | |
@Provides | |
@Singleton | |
fun provideApplication(): Application = app | |
@Provides | |
@Singleton | |
fun provideCryptocurrenciesDatabase(app: Application): Database = Room.databaseBuilder(app, |
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
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 |
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 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() |
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 ApiInterface { | |
@GET("ticker/") | |
fun getCryptocurrencies(@Query("start") start: String): Observable<List<Cryptocurrency>> | |
} |
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 CryptocurrenciesActivity : AppCompatActivity() { | |
val compositeDisposable = CompositeDisposable() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
showCryptocurrencies() | |
} |
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
@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() |
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
@Singleton | |
@Component( | |
modules = arrayOf(AndroidInjectionModule::class, BuildersModule::class, AppModule::class, | |
NetModule::class) | |
) | |
interface AppComponent { | |
fun inject(app: CryptocurrencyApplication) | |
} |
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 CryptocurrencyApplication: Application(), HasActivityInjector { | |
@Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity> | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.builder() | |
.appModule(AppModule(this)) | |
.netModule(NetModule(BuildConfig.URL)) |
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 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>> { |
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 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 | |
} |