Skip to content

Instantly share code, notes, and snippets.

View cdmunoz's full-sized avatar

Carlos Daniel cdmunoz

View GitHub Profile
class CryptocurrenciesViewModelFactory @Inject constructor(
private val cryptocurrenciesViewModel: CryptocurrenciesViewModel) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(CryptocurrenciesViewModel::class.java!!)) {
return cryptocurrenciesViewModel as T
}
throw IllegalArgumentException("Unknown class name")
}
}
class CryptocurrenciesActivity : AppCompatActivity() {
@Inject
lateinit var cryptocurrenciesViewModelFactory: CryptocurrenciesViewModelFactory
lateinit var cryptocurrenciesViewModel: CryptocurrenciesViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout.activity_main)
AndroidInjection.inject(this)
@Module
class AppModule(val app: Application) {
companion object {
val MIGRATION_1_2: Migration = object : Migration(1, 2){
override fun migrate(database: SupportSQLiteDatabase) {
// Change the table name to the correct one
database.execSQL("ALTER TABLE cryptocurrency RENAME TO cryptocurrencies")
}
}
@Entity(
tableName = "cryptocurrencies"
)
data class Cryptocurrency(
@Json(name = "id")
@PrimaryKey
val id: String,
@Json(name = "name")
@Dao
interface CryptocurrenciesDao {
@Query("SELECT * FROM cryptocurrencies ORDER BY rank limit :limit offset :offset")
fun queryCryptocurrencies(limit:Int, offset:Int): Single<List<Cryptocurrency>>
// ... rest of the code
}
class CryptocurrencyRepository @Inject constructor(val apiInterface: ApiInterface,
val cryptocurrenciesDao: CryptocurrenciesDao, val utils: Utils) {
fun getCryptocurrencies(limit: Int, offset: Int): Observable<List<Cryptocurrency>> {
val hasConnection = utils.isConnectedToInternet()
var observableFromApi: Observable<List<Cryptocurrency>>? = null
if (hasConnection){
observableFromApi = getCryptocurrenciesFromApi()
}
val observableFromDb = getCryptocurrenciesFromDb(limit, offset)
class CryptocurrencyRepository @Inject constructor(val apiInterface: ApiInterface,
val cryptocurrenciesDao: CryptocurrenciesDao, val utils: Utils) {
fun getCryptocurrencies(limit: Int, offset: Int): Observable<List<Cryptocurrency>> {
val hasConnection = utils.isConnectedToInternet()
var observableFromApi: Observable<List<Cryptocurrency>>? = null
if (hasConnection){
observableFromApi = getCryptocurrenciesFromApi()
}
val observableFromDb = getCryptocurrenciesFromDb(limit, offset)
class CryptocurrencyRepository @Inject constructor(val apiInterface: ApiInterface,
val cryptocurrenciesDao: CryptocurrenciesDao, val utils: Utils) {
fun getCryptocurrencies(limit: Int, offset: Int): Observable<List<Cryptocurrency>> {
val hasConnection = utils.isConnectedToInternet()
var observableFromApi: Observable<List<Cryptocurrency>>? = null
if (hasConnection){
observableFromApi = getCryptocurrenciesFromApi()
}
val observableFromDb = getCryptocurrenciesFromDb(limit, offset)
class Utils @Inject constructor(private val context: Context) {
fun isConnectedToInternet(): Boolean {
val connectivity = context.getSystemService(
Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (connectivity != null) {
val info = connectivity.allNetworkInfo
if (info != null)
for (i in info.indices)
if (info[i].state == NetworkInfo.State.CONNECTED) {
class Utils @Inject constructor(private val context: Context) {
fun isConnectedToInternet(): Boolean {
val connectivity = context.getSystemService(
Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (connectivity != null) {
val info = connectivity.allNetworkInfo
if (info != null)
for (i in info.indices)
if (info[i].state == NetworkInfo.State.CONNECTED) {