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 QuoteRepository { | |
private QuoteAPI quoteAPI; | |
QuoteRepository(){} | |
// … | |
LiveData<Quote> getQuote(String category, int count) { | |
//live data with setValue method exposed | |
final MutableLiveData<Quote> data = new MutableLiveData<>(); |
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 Quote{ | |
@PrimaryKey | |
private int id=0; | |
private String quote; | |
private String author; | |
private String category; |
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 QuoteDAO { | |
@Insert(onConflict = REPLACE) | |
void save(Quote quote); | |
@Update | |
void update(Quote quote); | |
@Query("SELECT * FROM quote") | |
LiveData<Quote> getQuote(); |
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 = {Quote.class}, version = 1) | |
public abstract class MyQuoteDB extends RoomDatabase { | |
public abstract QuoteDAO quoteDAO(); | |
} |
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 QuoteRepository { | |
private QuoteAPI quoteAPI; | |
private QuoteDAO quoteDAO; | |
private Executor executor; | |
QuoteRepository(){ | |
this.executor = Executors.newSingleThreadExecutor(); | |
MyQuoteDB db = Room.databaseBuilder(context, | |
MyQuoteDB.class, "database-name").build(); | |
quoteDAO=db.quoteDAO(); |
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 QuoteAPI { | |
//I insist you put your own key here fellas | |
@Headers({ | |
"X-Mashape-Key: your_Api_key", | |
"Content-Type: application/x-www-form-urlencoded", | |
"Accept: application/json" | |
}) | |
@GET("/") | |
Call <Quote> getQuote(@Query("cat") String category, | |
@Query("count") int count); |
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 Quote{ | |
@PrimaryKey(autoGenerate = true) | |
private int id=0; | |
private String quote; | |
private String author; | |
private String category; | |
public String getQuote() { |
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 AdapterQuotes extends RecyclerView.Adapter<AdapterQuotes.MyViewHolder>{ | |
private LayoutInflater inflater; | |
private List<Quote> quoteItems; | |
private Context context; | |
//private Typeface type; | |
public AdapterQuotes(Context context){ | |
this.quoteItems=new ArrayList<>(); | |
inflater= LayoutInflater.from(context); | |
this.context = 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
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp"> | |
<TextView | |
android:id="@+id/quote" |
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 FragmentQuote extends LifecycleFragment{ | |
private QuoteViewModel quoteViewModel; //data model | |
private SwipeRefreshLayout swipeRefreshLayout; //swipe to refresh data | |
private RecyclerView recyclerView; | |
private AdapterQuotes adapterQuotes; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |