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
@Entity
public class User {
@PrimaryKey
private int uId;
private String uName;
private ArrayList<String> uPets = new ArrayList<>();
public User() {}
public User(int id, String name, List<String> pets){
@Dao
public interface QuoteDAO {
@Insert(onConflict = REPLACE)
void cacheOnDB(List<Quote> quote);
@Query("SELECT * FROM quote")
LiveData<List<Quote>>getCachedQuotes();
//for saved quotes (starred quotes)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_to_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
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) {
<?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 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;
@Entity
public class Quote{
@PrimaryKey(autoGenerate = true)
private int id=0;
private String quote;
private String author;
private String category;
public String getQuote() {
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);
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();
@Database(entities = {Quote.class}, version = 1)
public abstract class MyQuoteDB extends RoomDatabase {
public abstract QuoteDAO quoteDAO();
}