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
imageLoader.resource { | |
resource { backgroundDrawable } | |
}.loadBackground(actionButton) |
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
// From resource | |
imageLoader.resource { | |
resource { backgroundDrawable } | |
}.loadBackground(actionButton) | |
//From network | |
imageLoader.network { | |
url { FAKE_IMAGE_URL } | |
error { PLACE_HOLDER_ID } |
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
@RunWith(AndroidJUnit4::class) | |
class TestImageLoader : AutoCloseKoinTest() { | |
private val imageLoader: ImageLoader by inject() | |
@Before | |
fun before() { | |
loadKoinModules(testModule) | |
} |
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 ImageLoader(val context: Context, val networkLoader: LoaderImageNetwork) { | |
fun network(init: FromNetwork.() -> Unit): FromNetwork { | |
return FromNetwork(init) | |
} | |
fun resource(init: FromResource.() -> Unit): FromResource { | |
return FromResource(init) | |
} |
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
@Transaction | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
fun insertAll(list: List<CharacterEntity>): Completable | |
@Query(""" | |
SELECT * FROM character ORDER BY name ASC | |
""") | |
fun getAllPaged(): DataSource.Factory<Int, CharacterEntity |
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
private fun initAdapter() { | |
rvCharacters.layoutManager = layoutManager | |
rvCharacters.adapter = adapter | |
rvCharacters.setHasFixedSize(true) | |
viewModel.dataSource | |
.observe(this, Observer { | |
adapter.submitList(it) | |
}) |
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 MainViewModel( | |
private val characterRepository: CharacterRepository | |
) : ViewModel() { | |
private val listing: LiveData<Listing<CharacterModel>> by lazy { | |
liveData(characterRepository.getListable()) | |
} | |
private val boundaryCallback = switchMap(listing) { it.getBoundaryCallback() }!! |
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
override fun getListable(): Listing<CharacterModel> { | |
return object : Listing<CharacterModel> { | |
/** Create the boundary callback **/ | |
val bc: GenericBoundaryCallback<CharacterModel> by lazy { | |
GenericBoundaryCallback( | |
{ dao.deleteAll() }, | |
{ character(it, SIZE_PAGE) }, | |
{ insertCharacters(it) }, | |
SIZE_PAGE |
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
/** | |
* For a clean architecture | |
* encapsulated all methods for paginate in this class | |
* | |
* For more information | |
* @see https://github.com/googlesamples/android-architecture-components/blob/master/PagingWithNetworkSample | |
**/ | |
interface Listing<T> { | |
fun getBoundaryCallback(): LiveData<GenericBoundaryCallback<T>> | |
fun getDataSource(): LiveData<PagedList<CharacterModel>> |
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
override fun characters(offset: Int, sizePage: Int): Single<List<CharacterEntity>> { | |
val ts = apiInf.ts | |
return retrofit.create(NetworkCalls::class.java) | |
.getCharacters(ts, apiInf.publicApiKey, apiInf.getHash(ts), offset, sizePage,"name") | |
.subscribeOn(Schedulers.io()) | |
.map { it.getDataOrError().results } | |
} |