-
Notifications
You must be signed in to change notification settings - Fork 894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Reordering Of Translations. Fixes #1179 #1398
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.quran.labs.androidquran.common | ||
|
||
class LocalTransationDiplaySort : Comparator<LocalTranslation> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who ate the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and the 'l' too you mean? yea oops correcting spelling... |
||
override fun compare(p0: LocalTranslation?, p1: LocalTranslation?): Int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really expect these to be nullable? because if not, we can simplify things here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, good call |
||
if (p0 == null || p1 == null) return 0; | ||
return p0.displayOrder.compareTo(p1.displayOrder); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.quran.labs.androidquran.dao.translation | ||
|
||
class TranslationItemDiplaySort : Comparator<TranslationItem> { | ||
override fun compare(p0: TranslationItem?, p1: TranslationItem?): Int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly, can these actually be nullable? |
||
if (p0 == null || p1 == null) return 0; | ||
return p0.displayOrder.compareTo(p1.displayOrder); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,10 +82,11 @@ public List<LocalTranslation> getTranslations() { | |
String languageCode = cursor.getString(6); | ||
int version = cursor.getInt(7); | ||
int minimumVersion = cursor.getInt(8); | ||
int displayOrder = cursor.getInt(9); | ||
|
||
if (quranFileUtils.hasTranslation(context, filename)) { | ||
items.add(new LocalTranslation(id, filename, name, translator, | ||
translatorForeign, url, languageCode, version, minimumVersion)); | ||
translatorForeign, url, languageCode, version, minimumVersion, displayOrder)); | ||
} | ||
} | ||
cursor.close(); | ||
|
@@ -105,11 +106,31 @@ public void deleteTranslationByFile(String filename) { | |
public boolean writeTranslationUpdates(List<TranslationItem> updates) { | ||
boolean result = true; | ||
db.beginTransaction(); | ||
|
||
try { | ||
for (int i = 0, updatesSize = updates.size(); i < updatesSize; i++) { | ||
TranslationItem item = updates.get(i); | ||
if (item.exists()) { | ||
int displayOrder = 0; | ||
|
||
final Translation translation = item.getTranslation(); | ||
|
||
if (item.getDisplayOrder() > -1) { | ||
displayOrder = item.getDisplayOrder(); | ||
} else { | ||
// get next highest display order | ||
Cursor cursor = db.query( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, consider closing this cursor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, yes to both cursor comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should do this in a try / finally because if an exception is thrown due to the query, we will leak the cursor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry to nitpick, the try/catch should be before the cursor assignment even - so like: Cursor cursor = null;
try {
cursor = ..
} finally {
DatabasUtils.closeCursor(cursor);
} |
||
TranslationsTable.TABLE_NAME, | ||
new String[] {TranslationsTable.DISPLAY_ORDER}, | ||
null, null, null, null, | ||
TranslationsTable.DISPLAY_ORDER + " DESC", | ||
"1" | ||
); | ||
if (cursor != null && cursor.moveToNext()) { | ||
displayOrder = cursor.getInt(0) + 1; | ||
} | ||
} | ||
|
||
ContentValues values = new ContentValues(); | ||
values.put(TranslationsTable.ID, translation.getId()); | ||
values.put(TranslationsTable.NAME, translation.getDisplayName()); | ||
|
@@ -121,6 +142,7 @@ public boolean writeTranslationUpdates(List<TranslationItem> updates) { | |
values.put(TranslationsTable.LANGUAGE_CODE, translation.getLanguageCode()); | ||
values.put(TranslationsTable.VERSION, item.getLocalVersion()); | ||
values.put(TranslationsTable.MINIMUM_REQUIRED_VERSION, translation.getMinimumVersion()); | ||
values.put(TranslationsTable.DISPLAY_ORDER, displayOrder); | ||
|
||
db.replace(TranslationsTable.TABLE_NAME, null, values); | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package com.quran.labs.androidquran.database; | ||
|
||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
|
||
|
@@ -11,7 +13,7 @@ | |
class TranslationsDBHelper extends SQLiteOpenHelper { | ||
|
||
private static final String DB_NAME = "translations.db"; | ||
private static final int DB_VERSION = 4; | ||
private static final int DB_VERSION = 5; | ||
private static final String CREATE_TRANSLATIONS_TABLE = | ||
"CREATE TABLE " + TranslationsTable.TABLE_NAME + "(" + | ||
TranslationsTable.ID + " integer primary key, " + | ||
|
@@ -22,7 +24,9 @@ class TranslationsDBHelper extends SQLiteOpenHelper { | |
TranslationsTable.URL + " varchar, " + | ||
TranslationsTable.LANGUAGE_CODE + " varchar, " + | ||
TranslationsTable.VERSION + " integer not null default 0," + | ||
TranslationsTable.MINIMUM_REQUIRED_VERSION + " integer not null default 0);"; | ||
TranslationsTable.MINIMUM_REQUIRED_VERSION + " integer not null default 0, " + | ||
TranslationsTable.DISPLAY_ORDER + " integer not null default -1 " + | ||
");"; | ||
|
||
@Inject | ||
TranslationsDBHelper(Context context) { | ||
|
@@ -69,6 +73,34 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.endTransaction(); | ||
} | ||
} | ||
if (oldVersion < 5) { | ||
// Add display order column and add arbitrary order to existing translations | ||
db.beginTransaction(); | ||
try { | ||
db.execSQL("ALTER TABLE " | ||
+ TranslationsTable.TABLE_NAME | ||
+ " ADD COLUMN " | ||
+ TranslationsTable.DISPLAY_ORDER | ||
+ " integer not null default -1" | ||
); | ||
Cursor translations = db.query( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not entirely sure if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 - also, as a hack, can we just use the row id as the value here so we don't have to do an upload loop? UPDATE translations SET display_order = id |
||
TranslationsTable.TABLE_NAME, new String[] { TranslationsTable.ID }, null, null, null, null, null | ||
); | ||
for (int i = 0; i < translations.getCount(); i++) { | ||
ContentValues values = new ContentValues(); | ||
values.put(TranslationsTable.DISPLAY_ORDER, i); | ||
db.update( | ||
TranslationsTable.TABLE_NAME, | ||
values, | ||
TranslationsTable.ID + " = ?", | ||
new String[] { String.valueOf(translations.getInt(0)) } | ||
); | ||
} | ||
db.setTransactionSuccessful(); | ||
} finally { | ||
db.endTransaction(); | ||
} | ||
} | ||
} | ||
|
||
static class TranslationsTable { | ||
|
@@ -82,5 +114,6 @@ static class TranslationsTable { | |
static final String LANGUAGE_CODE = "languageCode"; | ||
static final String VERSION = "version"; | ||
static final String MINIMUM_REQUIRED_VERSION = "minimumRequiredVersion"; | ||
static final String DISPLAY_ORDER = "userDisplayOrder"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert these changes. (Most probably you used them to test an upgrade?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops yes