Skip to content

Commit 99c6f5f

Browse files
Shift contributions to use Room DB (commons-app#3324)
* Part of commons-app#3127 * Added Room Dependency * Shifted ContributionsDao to use RoomDB * Save and Fetch contributions via RoomDAO * Bugfixes, fixed test cases, injected schedulers for ContributionsPresenter * removed stetho * Fixed ReviewHelperTest cases * Fixed test cases in DeleteHelperTest * Fetch all contributions [TODO add pagination to use this, maybe later in a seperate PR] * Update Schema false in AppDatabase * removed parameter from fetchControbutions * Added logs for fetch contributions * Fixed test case ContributionsPresenter * Added an autogenerate primary key, submit save contributions on executor * fixed getItemAtPosition * MainActivity Config changes +=orientation * BugFixes * Make AppDataBase Singleton * Set _id as autogenerate primary key [replacing the previously used filename, seems like they are not unique] * Replace Execxutor Utils with Subscribers on Singles in UploadService * BugFix, Upload Progress * Remove un-nescessary null check on contributions in ContributionsListAdapter * removed ContributionsListFragment [not-implemeted] * Review suggested changes * removed un-nescessary null checks * provide ContributionsDao * Minor bug fixes * wip * delete existing contributions table (from the existing db) on upgrade * remove un-nescessary null checks in test classes * shifted media to be a local variable in ReviewHelperTest * removed captured folder * Dispose composite disposables in UploadService * replaced size check with isEmpty ContributionsPresenter * transform saveContributions to a Completable * Addressed comments in review * Typo in Contributions * ReasonBuilderTest (create media object instead of mocking) * Use global Gson object instead of creating a new one in Converters * Provide Gson to Converters from the CommonsApplicationComponent * use static method instead of field instead of static field to provide GSON in Converters * Modified gitignore to exclude captures/*
1 parent 642ed51 commit 99c6f5f

35 files changed

+547
-1286
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ app/src/main/jniLibs
3838
#Below removes all the HTML files related to OpenCV documentation. The documentation can be otherwise found at:
3939
#https://docs.opencv.org/3.3.0/
4040
/libraries/opencv/javadoc/
41+
captures/*

app/build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ dependencies {
102102

103103
//swipe_layout
104104
implementation 'com.daimajia.swipelayout:library:1.2.0@aar'
105+
106+
//Room
107+
def room_version= '2.2.3'
108+
implementation "androidx.room:room-runtime:$room_version"
109+
kapt "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
105110
implementation 'com.squareup.retrofit2:retrofit:2.7.1'
111+
implementation "androidx.room:room-rxjava2:$room_version"
112+
testImplementation "androidx.arch.core:core-testing:2.1.0"
106113
}
107114

108115
android {

app/src/main/AndroidManifest.xml

+1-19
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
android:name=".contributions.MainActivity"
8686
android:icon="@mipmap/ic_launcher"
8787
android:label="@string/app_name"
88-
android:configChanges="screenSize|keyboard" />
88+
android:configChanges="screenSize|keyboard|orientation" />
8989
<activity
9090
android:name=".settings.SettingsActivity"
9191
android:label="@string/title_activity_settings" />
@@ -157,18 +157,6 @@
157157
android:name="android.accounts.AccountAuthenticator"
158158
android:resource="@xml/authenticator" />
159159
</service>
160-
<service
161-
android:name=".contributions.ContributionsSyncService"
162-
android:exported="true"
163-
android:process=":sync">
164-
<intent-filter>
165-
<action android:name="android.content.SyncAdapter" />
166-
</intent-filter>
167-
168-
<meta-data
169-
android:name="android.content.SyncAdapter"
170-
android:resource="@xml/contributions_sync_adapter" />
171-
</service>
172160

173161
<service
174162
android:name="org.acra.sender.SenderService"
@@ -184,12 +172,6 @@
184172
android:name="android.support.FILE_PROVIDER_PATHS"
185173
android:resource="@xml/provider_paths" />
186174
</provider>
187-
<provider
188-
android:name=".contributions.ContributionsContentProvider"
189-
android:authorities="${applicationId}.contributions.contentprovider"
190-
android:exported="false"
191-
android:label="@string/provider_contributions"
192-
android:syncable="true" />
193175

194176
<provider
195177
android:name=".category.CategoryContentProvider"

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.app.NotificationManager;
77
import android.content.Context;
88
import android.database.sqlite.SQLiteDatabase;
9+
import android.database.sqlite.SQLiteException;
910
import android.os.Build;
1011
import android.os.Process;
1112
import android.util.Log;
@@ -44,8 +45,8 @@
4445
import fr.free.nrw.commons.category.CategoryDao;
4546
import fr.free.nrw.commons.concurrency.BackgroundPoolExceptionHandler;
4647
import fr.free.nrw.commons.concurrency.ThreadPoolService;
47-
import fr.free.nrw.commons.contributions.ContributionDao;
4848
import fr.free.nrw.commons.data.DBOpenHelper;
49+
import fr.free.nrw.commons.db.AppDatabase;
4950
import fr.free.nrw.commons.di.ApplicationlessInjection;
5051
import fr.free.nrw.commons.kvstore.JsonKvStore;
5152
import fr.free.nrw.commons.logging.FileLoggingTree;
@@ -60,6 +61,7 @@
6061
import okhttp3.OkHttpClient;
6162
import timber.log.Timber;
6263

64+
import static fr.free.nrw.commons.data.DBOpenHelper.CONTRIBUTIONS_TABLE;
6365
import static org.acra.ReportField.ANDROID_VERSION;
6466
import static org.acra.ReportField.APP_VERSION_CODE;
6567
import static org.acra.ReportField.APP_VERSION_NAME;
@@ -126,6 +128,9 @@ public AppLanguageLookUpTable getLanguageLookUpTable() {
126128
return languageLookUpTable;
127129
}
128130

131+
@Inject
132+
AppDatabase appDatabase;
133+
129134
/**
130135
* Used to declare and initialize various components and dependencies
131136
*/
@@ -306,11 +311,13 @@ private void updateAllDatabases() {
306311
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
307312

308313
CategoryDao.Table.onDelete(db);
309-
ContributionDao.Table.onDelete(db);
314+
dbOpenHelper.deleteTable(db,CONTRIBUTIONS_TABLE);//Delete the contributions table in the existing db on older versions
315+
appDatabase.getContributionDao().deleteAll();
310316
BookmarkPicturesDao.Table.onDelete(db);
311317
BookmarkLocationsDao.Table.onDelete(db);
312318
}
313319

320+
314321
/**
315322
* Interface used to get log-out events
316323
*/

app/src/main/java/fr/free/nrw/commons/Media.java

+24-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import androidx.annotation.NonNull;
88
import androidx.annotation.Nullable;
9+
import androidx.room.Entity;
10+
import androidx.room.PrimaryKey;
911

1012
import org.apache.commons.lang3.StringUtils;
1113
import org.wikipedia.dataclient.mwapi.MwQueryPage;
@@ -26,6 +28,7 @@
2628
import fr.free.nrw.commons.utils.CommonsDateUtil;
2729
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
2830

31+
@Entity
2932
public class Media implements Parcelable {
3033

3134
public static final Media EMPTY = new Media("");
@@ -42,25 +45,25 @@ public Media[] newArray(int i) {
4245
};
4346

4447
// Primary metadata fields
45-
protected Uri localUri;
46-
private String thumbUrl;
47-
protected String imageUrl;
48-
protected String filename;
49-
protected String description; // monolingual description on input...
50-
protected String discussion;
51-
protected long dataLength;
52-
protected Date dateCreated;
53-
protected @Nullable Date dateUploaded;
54-
protected int width;
55-
protected int height;
56-
protected String license;
57-
protected String licenseUrl;
58-
protected String creator;
59-
protected ArrayList<String> categories; // as loaded at runtime?
60-
protected boolean requestedDeletion;
61-
private Map<String, String> descriptions; // multilingual descriptions as loaded
62-
private HashMap<String, Object> tags = new HashMap<>();
63-
private @Nullable LatLng coordinates;
48+
public Uri localUri;
49+
public String thumbUrl;
50+
public String imageUrl;
51+
public String filename;
52+
public String description; // monolingual description on input...
53+
public String discussion;
54+
long dataLength;
55+
public Date dateCreated;
56+
@Nullable public Date dateUploaded;
57+
public int width;
58+
public int height;
59+
public String license;
60+
public String licenseUrl;
61+
public String creator;
62+
public ArrayList<String> categories; // as loaded at runtime?
63+
public boolean requestedDeletion;
64+
public HashMap<String, String> descriptions; // multilingual descriptions as loaded
65+
public HashMap<String, String> tags = new HashMap<>();
66+
@Nullable public LatLng coordinates;
6467

6568
/**
6669
* Provides local constructor
@@ -118,7 +121,7 @@ public Media(Parcel in) {
118121
dateCreated = (Date) in.readSerializable();
119122
dateUploaded = (Date) in.readSerializable();
120123
creator = in.readString();
121-
tags = (HashMap<String, Object>) in.readSerializable();
124+
tags = (HashMap<String, String>) in.readSerializable();
122125
width = in.readInt();
123126
height = in.readInt();
124127
license = in.readString();
@@ -218,7 +221,7 @@ public Object getTag(String key) {
218221
* @param key Media key
219222
* @param value Media value
220223
*/
221-
public void setTag(String key, Object value) {
224+
public void setTag(String key, String value) {
222225
tags.put(key, value);
223226
}
224227

app/src/main/java/fr/free/nrw/commons/contributions/Contribution.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import androidx.annotation.NonNull;
88
import androidx.annotation.StringDef;
9+
import androidx.room.Entity;
10+
import androidx.room.PrimaryKey;
911

1012
import org.apache.commons.lang3.StringUtils;
1113

@@ -21,6 +23,7 @@
2123

2224
import static java.lang.annotation.RetentionPolicy.SOURCE;
2325

26+
@Entity(tableName = "contribution")
2427
public class Contribution extends Media {
2528

2629
//{{According to Exif data|2009-01-09}}
@@ -54,17 +57,19 @@ public Contribution[] newArray(int i) {
5457
public static final String SOURCE_CAMERA = "camera";
5558
public static final String SOURCE_GALLERY = "gallery";
5659
public static final String SOURCE_EXTERNAL = "external";
57-
58-
private Uri contentUri;
59-
private String source;
60-
private String editSummary;
61-
private int state;
62-
private long transferred;
63-
private String decimalCoords;
64-
private boolean isMultiple;
65-
private String wikiDataEntityId;
66-
private Uri contentProviderUri;
67-
private String dateCreatedSource;
60+
@PrimaryKey (autoGenerate = true)
61+
@NonNull
62+
public long _id;
63+
public Uri contentUri;
64+
public String source;
65+
public String editSummary;
66+
public int state;
67+
public long transferred;
68+
public String decimalCoords;
69+
public boolean isMultiple;
70+
public String wikiDataEntityId;
71+
public Uri contentProviderUri;
72+
public String dateCreatedSource;
6873

6974
public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date dateCreated,
7075
int state, long dataLength, Date dateUploaded, long transferred,

0 commit comments

Comments
 (0)