Skip to content

Instantly share code, notes, and snippets.

View GauravChaddha1996's full-sized avatar
🙂
Giving my best!

Gaurav Chaddha GauravChaddha1996

🙂
Giving my best!
View GitHub Profile
@GauravChaddha1996
GauravChaddha1996 / RecyclerViewEmptyExtdener.java
Last active August 30, 2016 04:33
Extended Recycler View to show empty view when the list gets empty.
package com.pokemonify.pokemonify.recyclerviewcomponents;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
@GauravChaddha1996
GauravChaddha1996 / CustomTypeFaceTextView.java
Created November 16, 2016 12:28
Custom Typeface TextView
/*
* Copyright (c) 2016 Gaurav Chaddha
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
@GauravChaddha1996
GauravChaddha1996 / Java Design Patterns.md
Last active December 27, 2016 11:49
Small description and implementation details of java design patterns.

Information is taken from : [Journal Dev Java Design Pattern Example Tutorial] (http://www.journaldev.com/1827/java-design-patterns-example-tutorial)

#Creational Design Patterns ###Patterns to use when an object is being instantiated

  1. Singleton Pattern : Used when we want only one instance of a class. Implement using a static inner class with a private final outer class object for confirmed single instance.

  2. Factory Pattern : Used when parent class has many sub class and we want an object of those classes. Implement it using a factory class which uses if elseif code for returning sub class object based on some condition and cast it to parent class (Dynamic Run time overriding)

  3. Abstract Factory Pattern : Usage same as factory to remove if checks. Implement it by making abstract factory class and then its impl of base class, and all subclasses. The base class abstract impl should accept values from sub class abstract impl.

@GauravChaddha1996
GauravChaddha1996 / JobOfModel.java
Last active June 25, 2017 16:27
The following gist shows the job of a model in MVP via an example.
class Model {
UserModelHelper userModel;
RecipeModelHelper recipeModel;
NetworkManager networkManager;
/*
Make the constructor here and initialize the model helpers
like UserModel and other managers
*/
/* Checks if the account is premium or not. Throws an
class RecipeView {
// Lifecycle and other instantiation events.
/*
updateUI function is called with RecipeData object
holding the list to be shown
*/
void updateUI(RecipeData data) {
if (data.getError()) {
// show upgrade dialog.
class RecipePresenter {
Model model;
View view;
List<Recipe> list;
RecipeData data;
// Make the constructor here and initialize the model and view
/* First and second role of presenter- asking model for data
on button click. Caching the recipes in 'data' variable and
then sending the results back */
public class MyActivity extends AppCompatActivity implements MyViewInterface {
private MyPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
presenter = new MyPresenter(DataManager.getDataManager());
}
@Override
protected void onStart() {
class MyPresenter {
private MyViewInterface viewInterface;
private DataManager dataManager;
private MyState state;
MyPresenter(DataManager dataManager) {
this.dataManager = dataManager;
/* Initiates state with a default value for the initial UI
of this view */
state = new MyState("This is the template data");
public class DataManager {
private static DataManager dataManager;
private final Context context;
private SharedPreferencesHelper sharedPreferencesHelper;
private DataManager(Context context) {
this.context = context;
/* Create your delegation managers here like
NetworkManager, SharedPreferencesHelper etc. */
sharedPreferencesHelper = new SharedPreferencesHelper();
interface MyViewInterface {
/* MyState class is a class which contains all the data needed
to display the UI of the said activity. */
void updateUI(MyState state);
}