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 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) |
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 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) { |
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
@Module | |
class AppModule(val app: Application) { | |
//...rest of the code | |
@Provides | |
@Singleton | |
fun provideUtils(): Utils = Utils(app) | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/cryptocurrency_card" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="@dimen/default_4dp" | |
app:cardElevation="@dimen/cardview_default_elevation" |
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 CryptocurrenciesAdapter( | |
cryptocurrencies: List<Cryptocurrency>?) : RecyclerView.Adapter<CryptocurrencieViewHolder>() { | |
private var cryptocurrenciesList = ArrayList<Cryptocurrency>() | |
init { | |
this.cryptocurrenciesList = cryptocurrencies as ArrayList<Cryptocurrency> | |
} | |
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CryptocurrencieViewHolder { |
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_margin="8dp" | |
tools:context="co.cdmunoz.cryptocurrencyapp.ui.list.CryptocurrenciesActivity" | |
> |
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 InfiniteScrollListener( | |
val func: () -> Unit, | |
val layoutManager: LinearLayoutManager) : RecyclerView.OnScrollListener() { | |
private var previousTotal = 0 | |
private var loading = true | |
private var visibleThreshold = 2 | |
private var firstVisibleItem = 0 | |
private var visibleItemCount = 0 | |
private var totalItemCount = 0 |
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 InfiniteScrollListener( | |
val func: () -> Unit, | |
val layoutManager: LinearLayoutManager) : RecyclerView.OnScrollListener() { | |
private var previousTotal = 0 | |
private var loading = true | |
private var visibleThreshold = 2 | |
private var firstVisibleItem = 0 | |
private var visibleItemCount = 0 | |
private var totalItemCount = 0 |
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 CryptocurrenciesActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(layout.activity_cryptocurrencies) | |
initializeRecycler() | |
progressBar.visibility = View.VISIBLE | |
loadData() |
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 Constants { | |
companion object { | |
const val OFFSET = 12 | |
const val LIMIT = 12 | |
const val START_ZERO_VALUE = "0" | |
const val DATABASE_NAME = "cryptocurrencies_db" | |
const val LIST_SCROLLING = 12 | |
} | |
} |