Skip to content

Commit fe347c2

Browse files
authored
Migrated recentlanguages and repository module from Java to Kotlin (#5948)
* Rename .java to .kt * Migrated recentlanguages module to Kotlin * Rename .java to .kt * Migrated repository module to Kotlin
1 parent 088dd24 commit fe347c2

13 files changed

+825
-798
lines changed

app/src/main/java/fr/free/nrw/commons/recentlanguages/RecentLanguagesContentProvider.java

-122
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package fr.free.nrw.commons.recentlanguages
2+
3+
4+
import android.content.ContentValues
5+
import android.database.Cursor
6+
import android.database.sqlite.SQLiteDatabase
7+
import android.database.sqlite.SQLiteQueryBuilder
8+
import android.net.Uri
9+
import android.text.TextUtils
10+
import fr.free.nrw.commons.BuildConfig
11+
import fr.free.nrw.commons.data.DBOpenHelper
12+
import fr.free.nrw.commons.di.CommonsDaggerContentProvider
13+
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao.Table.COLUMN_NAME
14+
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao.Table.TABLE_NAME
15+
import javax.inject.Inject
16+
import timber.log.Timber
17+
18+
19+
/**
20+
* Content provider of recently used languages
21+
*/
22+
class RecentLanguagesContentProvider : CommonsDaggerContentProvider() {
23+
24+
companion object {
25+
private const val BASE_PATH = "recent_languages"
26+
val BASE_URI: Uri =
27+
Uri.parse(
28+
"content://${BuildConfig.RECENT_LANGUAGE_AUTHORITY}/$BASE_PATH"
29+
)
30+
31+
/**
32+
* Append language code to the base URI
33+
* @param languageCode Code of a language
34+
*/
35+
@JvmStatic
36+
fun uriForCode(languageCode: String): Uri {
37+
return Uri.parse("$BASE_URI/$languageCode")
38+
}
39+
}
40+
41+
@Inject
42+
lateinit var dbOpenHelper: DBOpenHelper
43+
44+
override fun getType(uri: Uri): String? {
45+
return null
46+
}
47+
48+
/**
49+
* Queries the SQLite database for the recently used languages
50+
* @param uri : contains the URI for recently used languages
51+
* @param projection : contains all fields of the table
52+
* @param selection : handles WHERE
53+
* @param selectionArgs : the condition of WHERE clause
54+
* @param sortOrder : ascending or descending
55+
*/
56+
override fun query(
57+
uri: Uri,
58+
projection: Array<String>?,
59+
selection: String?,
60+
selectionArgs: Array<String>?,
61+
sortOrder: String?
62+
): Cursor? {
63+
val queryBuilder = SQLiteQueryBuilder()
64+
queryBuilder.tables = TABLE_NAME
65+
val db = dbOpenHelper.readableDatabase
66+
val cursor = queryBuilder.query(
67+
db,
68+
projection,
69+
selection,
70+
selectionArgs,
71+
null,
72+
null,
73+
sortOrder
74+
)
75+
cursor.setNotificationUri(context?.contentResolver, uri)
76+
return cursor
77+
}
78+
79+
/**
80+
* Handles the update query of local SQLite Database
81+
* @param uri : contains the URI for recently used languages
82+
* @param contentValues : new values to be entered to the database
83+
* @param selection : handles WHERE
84+
* @param selectionArgs : the condition of WHERE clause
85+
*/
86+
override fun update(
87+
uri: Uri,
88+
contentValues: ContentValues?,
89+
selection: String?,
90+
selectionArgs: Array<String>?
91+
): Int {
92+
val sqlDB = dbOpenHelper.writableDatabase
93+
val rowsUpdated: Int
94+
if (selection.isNullOrEmpty()) {
95+
val id = uri.lastPathSegment?.toInt()
96+
?: throw IllegalArgumentException("Invalid URI: $uri")
97+
rowsUpdated = sqlDB.update(
98+
TABLE_NAME,
99+
contentValues,
100+
"$COLUMN_NAME = ?",
101+
arrayOf(id.toString())
102+
)
103+
} else {
104+
throw IllegalArgumentException("Parameter `selection` should be empty when updating an ID")
105+
}
106+
107+
context?.contentResolver?.notifyChange(uri, null)
108+
return rowsUpdated
109+
}
110+
111+
/**
112+
* Handles the insertion of new recently used languages record to local SQLite Database
113+
* @param uri : contains the URI for recently used languages
114+
* @param contentValues : new values to be entered to the database
115+
*/
116+
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
117+
val sqlDB = dbOpenHelper.writableDatabase
118+
val id = sqlDB.insert(
119+
TABLE_NAME,
120+
null,
121+
contentValues
122+
)
123+
context?.contentResolver?.notifyChange(uri, null)
124+
return Uri.parse("$BASE_URI/$id")
125+
}
126+
127+
/**
128+
* Handles the deletion of a recently used languages record from local SQLite Database
129+
* @param uri : contains the URI for recently used languages
130+
*/
131+
override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int {
132+
val db = dbOpenHelper.readableDatabase
133+
Timber.d("Deleting recently used language %s", uri.lastPathSegment)
134+
val rows = db.delete(
135+
TABLE_NAME,
136+
"language_code = ?",
137+
arrayOf(uri.lastPathSegment)
138+
)
139+
context?.contentResolver?.notifyChange(uri, null)
140+
return rows
141+
}
142+
}

0 commit comments

Comments
 (0)