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
dependencies { | |
... | |
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1' | |
retrofit2() | |
rx() | |
lifeCycle() |
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
@Entity | |
public class Movie { | |
@PrimaryKey | |
@NonNull | |
private String imdbID; | |
@SerializedName("Title") | |
private String title; |
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 SearchResult { | |
@SerializedName("Search") | |
private ArrayList<Movie> search; | |
private int totalResults; | |
@SerializedName("Response") | |
private boolean response; | |
... |
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
@Database(entities = Movie.class, version = 1, exportSchema = false) | |
public abstract class AppDatabase extends RoomDatabase { | |
public static final String DATABASE_DB = "database.db"; | |
private static AppDatabase appDatabase; | |
public static AppDatabase getInstance(Context context) { | |
if (appDatabase == null) { | |
appDatabase = Room.databaseBuilder(context, AppDatabase.class, DATABASE_DB).allowMainThreadQueries().build(); | |
} |
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 MovieDatabaseHelper extends BaseDatabaseHelper { | |
private MovieDao movieDao; | |
public MovieDatabaseHelper(Context context) { | |
super(context); | |
movieDao = appDatabase.getMovieDao(); | |
} | |
public LiveData<List<Movie>> 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
public interface MovieApi { | |
@GET("/?s=Batman&page=1&apikey=96099fe3") | |
Single<Response<SearchResult>> getMovieList(); | |
} |
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 MovieRepository extends BaseRepository { | |
private MovieApi movieApi; | |
private MovieDatabaseHelper movieDatabaseHelper; | |
public MovieRepository(Context context) { | |
movieApi = getRetrofitHelper().getService(MovieApi.class); | |
movieDatabaseHelper = new MovieDatabaseHelper(context); | |
} |
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 HomeViewModel extends AndroidViewModel { | |
private MovieRepository movieRepository; | |
HomeViewModel(@NonNull Application application) { | |
super(application); | |
movieRepository = new MovieRepository(application.getApplicationContext()); | |
} | |
LiveData<List<Movie>> getMovies() { |
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
... | |
HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class); | |
homeViewModel.getMovies().observe(HomeActivity.this, movieList -> { | |
if (movieList != null) { | |
ArrayList<Movie> movieArrayList = new ArrayList<>(movieList); | |
homeAdapter = new HomeAdapter(HomeActivity.this, movieArrayList); | |
InfiniteScrollAdapter wrapper = InfiniteScrollAdapter.wrap(homeAdapter); | |
discreteScrollView.setAdapter(wrapper); |
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 interface MovieDao extends BaseDao<Movie> { | |
@Query("select * from Movie") | |
LiveData<List<Movie>> getAll(); | |
} |
OlderNewer