First baby steps into the world of dependency injection using Dagger.

This commit is contained in:
Paul Hawke 2017-08-25 00:57:17 -05:00
parent 04f676c320
commit 8fe2816ca9
32 changed files with 351 additions and 115 deletions

View file

@ -24,13 +24,17 @@ import android.widget.AdapterView;
import java.util.ArrayList;
import javax.inject.Inject;
import butterknife.ButterKnife;
import dagger.android.AndroidInjection;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.HandlerService;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.upload.UploadService;
import io.reactivex.android.schedulers.AndroidSchedulers;
@ -46,6 +50,9 @@ public class ContributionsActivity
FragmentManager.OnBackStackChangedListener,
ContributionsListFragment.SourceRefresher {
@Inject CommonsApplication application;
@Inject MediaWikiApi mediaWikiApi;
private Cursor allContributions;
private ContributionsListFragment contributionsList;
private MediaDetailPagerFragment mediaDetails;
@ -108,7 +115,7 @@ public class ContributionsActivity
@Override
protected void onAuthCookieAcquired(String authCookie) {
// Do a sync everytime we get here!
ContentResolver.requestSync(CommonsApplication.getInstance().getCurrentAccount(), ContributionsContentProvider.AUTHORITY, new Bundle());
ContentResolver.requestSync(application.getCurrentAccount(), ContributionsContentProvider.AUTHORITY, new Bundle());
Intent uploadServiceIntent = new Intent(this, UploadService.class);
uploadServiceIntent.setAction(UploadService.ACTION_START_SERVICE);
startService(uploadServiceIntent);
@ -263,10 +270,8 @@ public class ContributionsActivity
}
private void setUploadCount() {
CommonsApplication application = CommonsApplication.getInstance();
compositeDisposable.add(
CommonsApplication.getInstance().getMWApi()
mediaWikiApi
.getUploadCount(application.getCurrentAccount().name)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())

View file

@ -10,6 +10,9 @@ import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import javax.inject.Inject;
import dagger.android.AndroidInjection;
import fr.free.nrw.commons.CommonsApplication;
import timber.log.Timber;
@ -33,9 +36,12 @@ public class ContributionsContentProvider extends ContentProvider{
return Uri.parse(BASE_URI.toString() + "/" + id);
}
@Inject CommonsApplication application;
@Override
public boolean onCreate() {
return false;
AndroidInjection.inject(this);
return true;
}
@Override
@ -45,7 +51,7 @@ public class ContributionsContentProvider extends ContentProvider{
int uriType = uriMatcher.match(uri);
SQLiteDatabase db = CommonsApplication.getInstance().getDBOpenHelper().getReadableDatabase();
SQLiteDatabase db = application.getDBOpenHelper().getReadableDatabase();
Cursor cursor;
switch(uriType) {
@ -79,7 +85,7 @@ public class ContributionsContentProvider extends ContentProvider{
@Override
public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = CommonsApplication.getInstance().getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
long id = 0;
switch (uriType) {
case CONTRIBUTIONS:
@ -97,7 +103,7 @@ public class ContributionsContentProvider extends ContentProvider{
int rows = 0;
int uriType = uriMatcher.match(uri);
SQLiteDatabase db = CommonsApplication.getInstance().getDBOpenHelper().getReadableDatabase();
SQLiteDatabase db = application.getDBOpenHelper().getReadableDatabase();
switch(uriType) {
case CONTRIBUTIONS_ID:
@ -118,7 +124,7 @@ public class ContributionsContentProvider extends ContentProvider{
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
Timber.d("Hello, bulk insert! (ContributionsContentProvider)");
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = CommonsApplication.getInstance().getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
sqlDB.beginTransaction();
switch (uriType) {
case CONTRIBUTIONS:
@ -146,7 +152,7 @@ public class ContributionsContentProvider extends ContentProvider{
In here, the only concat created argument is for id. It is cast to an int, and will error out otherwise.
*/
int uriType = uriMatcher.match(uri);
SQLiteDatabase sqlDB = CommonsApplication.getInstance().getDBOpenHelper().getWritableDatabase();
SQLiteDatabase sqlDB = application.getDBOpenHelper().getWritableDatabase();
int rowsUpdated = 0;
switch (uriType) {
case CONTRIBUTIONS:

View file

@ -17,6 +17,8 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.mwapi.LogEventResult;
@ -25,6 +27,9 @@ import timber.log.Timber;
public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
private static int COMMIT_THRESHOLD = 10;
@Inject CommonsApplication application;
public ContributionsSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@ -59,10 +64,12 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
@Override
public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
((CommonsApplication)getContext().getApplicationContext()).injector().inject(this);
// This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
String user = account.name;
MediaWikiApi api = CommonsApplication.getInstance().getMWApi();
SharedPreferences prefs = this.getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
MediaWikiApi api = application.getMWApi();
SharedPreferences prefs = getContext().getSharedPreferences("prefs", Context.MODE_PRIVATE);
String lastModified = prefs.getString("lastSyncTimestamp", "");
Date curTime = new Date();
LogEventResult result;