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
data class User(var id: Int = 0, var age: Int = 0, var score: Int = 0, var name: String? = null) |
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
viewModel.getGenres().observe(this, listResource -> { | |
refreshLayout.setRefreshing(listResource.status == Status.LOADING); | |
genresAdapter.setGenres(listResource.data); | |
}); |
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
public class GenresViewModel extends ViewModel { | |
private LiveData<Resource<List<GenreEntity>>> genreLiveData; | |
@Inject | |
public GenresViewModel(RadioRepository radioRepository) { | |
genreLiveData = LiveDataReactiveStreams.fromPublisher(radioRepository.getGenres() | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread())); | |
} |
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
public class RadioRepository { | |
//... | |
public Flowable<Resource<List<GenreEntity>>> getGenres() { | |
return Flowable.create(emitter -> new NetworkBoundSource<List<GenreEntity>, List<Genre>>(emitter) { | |
@Override | |
public Single<List<Genre>> getRemote() { | |
return firebaseService.getGenres(); | |
} |
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
public abstract class NetworkBoundSource<LocalType, RemoteType> { | |
public NetworkBoundSource(FlowableEmitter<Resource<LocalType>> emitter) { | |
Disposable firstDataDisposable = getLocal() | |
.map(Resource::loading) | |
.subscribe(emitter::onNext); | |
getRemote().map(mapper()) | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(Schedulers.newThread()) |
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
public class FirebaseService { | |
// ... | |
public Single<List<Genre>> getGenres() { | |
return Single.create(e -> | |
firebaseDatabase.getReference().child("genres") | |
.addListenerForSingleValueEvent(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
List<Genre> genreList = new ArrayList<>(); | |
for (DataSnapshot child : dataSnapshot.getChildren()) { |
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
@Dao | |
public abstract class GenresDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
public abstract void insert(List<GenreEntity> genreEntities); | |
@Query("SELECT * FROM genreentity") | |
public abstract Flowable<List<GenreEntity>> getAll(); | |
} |
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
//Room | |
archComponentVersion = "1.0.0-alpha3" | |
archRoom = "android.arch.persistence.room:runtime:$archComponentVersion" | |
archRoomCompiler = "android.arch.persistence.room:compiler:$archComponentVersion" | |
archRoomRx = "android.arch.persistence.room:rxjava2:$archComponentVersion" | |
archLifeCycleReactiveStreams = "android.arch.lifecycle:reactivestreams:$archComponentVersion" | |
//RxJava & RxAndroid |
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 | |
public abstract class ActivityBuilder { | |
@Binds | |
@IntoMap | |
@ActivityKey(MainActivity.class) | |
abstract AndroidInjector.Factory<? extends Activity> bindMainActivity(MainActivityComponent.Builder builder); | |
} |
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
public class DetailActivity extends AppCompatActivity implements HasSupportFragmentInjector { | |
@Inject | |
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector; | |
//simplified | |
@Override | |
public AndroidInjector<Fragment> supportFragmentInjector() { | |
return fragmentDispatchingAndroidInjector; |