Skip to content

Instantly share code, notes, and snippets.

Avatar
🎉
Mostly Android stuff now, also FP

Jorge Castillo JorgeCastilloPrz

🎉
Mostly Android stuff now, also FP
View GitHub Profile
@JorgeCastilloPrz
JorgeCastilloPrz / gist:a13f129743fd7a4d6b60
Last active Aug 29, 2015
Medium - Dependency injection - 1
View gist:a13f129743fd7a4d6b60
public interface BookRepository {
public List<Book> getAllBooks();
public List<Book> getAllBooksFromUser(int userId);
public List<Book> getAllBooksByCategory(BookCategory category);
public Book getBook(int bookId);
public Book getBookByName(String bookName);
public void removeBook(int bookId);
public void markAsCompleted(int bookId);
}
View gist:1f242d36cbbec66ac72f
public void BookController {
private static BookController instance = null;
private BookRepository bookRepository = new PersistentBookRepository();
protected BookController() {}
public static BookController getInstance() {
if(instance == null) {
instance = new BookController();
View gist:9107b6c3d2e04cfa927e
public class ApplicationClass extends Application {
private ObjectGraph objectGraph;
@Override
public void onCreate() {
super.onCreate();
objectGraph = ObjectGraph.create(getModules().toArray());
objectGraph.inject(this);
}
View gist:ed2f472f549ca8a5cf20
@Module(
injects = {
ApplicationClass.class,
AbstractFontFactory.class,
RobotoFontFactory.class,
DataController.class,
LocationController.class,
RestAnimalProvider.class,
MainActivity.class,
TutorialActivity.class,
View gist:9cb01c518be2fde5b301
/**
* A module for more generic or external dependencies wich doesnt require a {@link android.content.Context} or
* {@link android.app.Application} to create.
*/
@Module(
library = true
)
public class AppModule {
@Provides
View gist:615a3ce6956eec47d2ca
/**
* A module for dependencies which require a {@link android.content.Context} or
* {@link android.app.Application} to create.
*/
@Module(
library = true
)
public class ContextModule {
private Context appContext;
View gist:564e2f3ddab9673ea127
public class LocationController implements LocationListener {
private static final int TWO_MINUTES = 1000 * 60 * 2;
private Context context;
private LocationManager locationManager;
private String provider;
private Location bestLocationUntilNow;
@Inject
View gist:94c4b3196a075a7bdba1
/**
* Created by jorge on 3/17/14.
*/
public class MainActivity extends InjectedActivity {
@Inject
GoogleApiClient mGoogleApiClient;
@Inject
LocationController mLocationController;
@Inject
View gist:2c30822b44f444b987be
/**
* Created by jorge on 5/3/14.
*/
public class InjectedActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((ApplicationClass) getApplicationContext()).inject(this);
}
View gist:fcd339741c7c5a923915
class GridingCoffeeMaker {
@Inject Lazy<Grinder> lazyGrinder;
public void brew() {
while (needsGrinding()) {
// Grinder created once on first call to .get() and cached.
lazyGrinder.get().grind();
}
}
}
You can’t perform that action at this time.