Skip to content

Instantly share code, notes, and snippets.

View JorgeCastilloPrz's full-sized 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 August 29, 2015 14:08
Medium - Dependency injection - 1
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);
}
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();
public class ApplicationClass extends Application {
private ObjectGraph objectGraph;
@Override
public void onCreate() {
super.onCreate();
objectGraph = ObjectGraph.create(getModules().toArray());
objectGraph.inject(this);
}
@Module(
injects = {
ApplicationClass.class,
AbstractFontFactory.class,
RobotoFontFactory.class,
DataController.class,
LocationController.class,
RestAnimalProvider.class,
MainActivity.class,
TutorialActivity.class,
/**
* 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
@JorgeCastilloPrz
JorgeCastilloPrz / gist:564e2f3ddab9673ea127
Created December 7, 2014 15:35
LocationController Injection
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
@JorgeCastilloPrz
JorgeCastilloPrz / gist:94c4b3196a075a7bdba1
Created December 7, 2014 15:59
MainActivity injection
/**
* Created by jorge on 3/17/14.
*/
public class MainActivity extends InjectedActivity {
@Inject
GoogleApiClient mGoogleApiClient;
@Inject
LocationController mLocationController;
@Inject
/**
* 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);
}
class GridingCoffeeMaker {
@Inject Lazy<Grinder> lazyGrinder;
public void brew() {
while (needsGrinding()) {
// Grinder created once on first call to .get() and cached.
lazyGrinder.get().grind();
}
}
}
@JorgeCastilloPrz
JorgeCastilloPrz / gist:615a3ce6956eec47d2ca
Created December 7, 2014 14:14
Dagger ContextModule
/**
* 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;