Skip to content

Instantly share code, notes, and snippets.

View amit-bhandari's full-sized avatar
💻
Writing code, duhhh

Amit Bhandari amit-bhandari

💻
Writing code, duhhh
View GitHub Profile
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<>();
@Entity
public class Quote{
@PrimaryKey
private int id=0;
private String quote;
private String author;
private String category;
@Dao
public interface QuoteDAO {
@Insert(onConflict = REPLACE)
void save(Quote quote);
@Update
void update(Quote quote);
@Query("SELECT * FROM quote")
LiveData<Quote> getQuote();
@Database(entities = {Quote.class}, version = 1)
public abstract class MyQuoteDB extends RoomDatabase {
public abstract QuoteDAO quoteDAO();
}
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();
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);
@Entity
public class Quote{
@PrimaryKey(autoGenerate = true)
private int id=0;
private String quote;
private String author;
private String category;
public String getQuote() {
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;
<?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"
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) {