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 */
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");
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);
}
class MainLoader extends Loader<MainPresenter> {
private MainPresenter presenter;
/* When the loader is being created, we instantiate the
presenter also and keep it's object as private */
MainLoader(Context context, MainPresenter presenter) {
super(context);
this.presenter = presenter;
}
public class MainActivity extends AppCompatActivity implements MainViewInterface,
LoaderManager.LoaderCallbacks<MainPresenter> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// start the loader to load the presenter
/* 1001 is a unique loader id through which system manages