Search in sources :

Example 6 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationManagerPresenter method mergeWithServerTranslations.

private List<TranslationItem> mergeWithServerTranslations(List<Translation> serverTranslations) {
    List<TranslationItem> results = new ArrayList<>(serverTranslations.size());
    SparseArray<LocalTranslation> localTranslations = translationsDBAdapter.getTranslationsHash();
    String databaseDir = quranFileUtils.getQuranDatabaseDirectory(appContext);
    List<TranslationItem> updates = new ArrayList<>();
    for (int i = 0, count = serverTranslations.size(); i < count; i++) {
        Translation translation = serverTranslations.get(i);
        LocalTranslation local = localTranslations.get(translation.getId());
        File dbFile = new File(databaseDir, translation.getFileName());
        boolean exists = dbFile.exists();
        TranslationItem item;
        if (exists) {
            int version = local == null ? getVersionFromDatabase(translation.getFileName()) : local.version;
            item = new TranslationItem(translation, version);
        } else {
            item = new TranslationItem(translation);
        }
        if (exists && !item.exists()) {
            // delete the file, it has been corrupted
            if (dbFile.delete()) {
                exists = false;
            }
        }
        if ((local == null && exists) || (local != null && !exists)) {
            updates.add(item);
        } else if (local != null && local.languageCode == null) {
            // older items don't have a language code
            updates.add(item);
        }
        results.add(item);
    }
    if (!updates.isEmpty()) {
        translationsDBAdapter.writeTranslationUpdates(updates);
    }
    return results;
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) Translation(com.quran.labs.androidquran.dao.translation.Translation) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) LocalTranslation(com.quran.labs.androidquran.common.LocalTranslation) ArrayList(java.util.ArrayList) File(java.io.File)

Example 7 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationsAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(TranslationViewHolder holder, int position) {
    TranslationRowData rowItem = translations.get(position);
    switch(holder.getItemViewType()) {
        case R.layout.translation_row:
            TranslationItem item = (TranslationItem) rowItem;
            holder.getTranslationTitle().setText(item.name());
            if (TextUtils.isEmpty(item.translation.getTranslatorNameLocalized())) {
                holder.getTranslationInfo().setText(item.translation.getTranslator());
            } else {
                holder.getTranslationInfo().setText(item.translation.getTranslatorNameLocalized());
            }
            ImageView leftImage = holder.getLeftImage();
            ImageView rightImage = holder.getRightImage();
            if (item.exists()) {
                if (item.needsUpgrade()) {
                    leftImage.setImageResource(R.drawable.ic_download);
                    leftImage.setVisibility(View.VISIBLE);
                    holder.getTranslationInfo().setText(R.string.update_available);
                } else {
                    leftImage.setVisibility(View.GONE);
                }
                rightImage.setImageResource(R.drawable.ic_cancel);
                rightImage.setVisibility(View.VISIBLE);
                rightImage.setContentDescription(context.getString(R.string.remove_button));
            } else {
                leftImage.setVisibility(View.GONE);
                rightImage.setImageResource(R.drawable.ic_download);
                rightImage.setVisibility(View.VISIBLE);
                rightImage.setOnClickListener(null);
                rightImage.setClickable(false);
                rightImage.setContentDescription(null);
            }
            break;
        case R.layout.translation_sep:
            holder.getSeparatorText().setText(rowItem.name());
            break;
    }
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) TranslationRowData(com.quran.labs.androidquran.dao.translation.TranslationRowData) ImageView(android.widget.ImageView)

Example 8 with TranslationItem

use of com.quran.labs.androidquran.dao.translation.TranslationItem in project quran_android by quran.

the class TranslationManagerActivity method generateListItems.

private void generateListItems() {
    if (allItems == null) {
        return;
    }
    List<TranslationItem> downloaded = new ArrayList<>();
    List<TranslationItem> notDownloaded = new ArrayList<>();
    for (int i = 0, mAllItemsSize = allItems.size(); i < mAllItemsSize; i++) {
        TranslationItem item = allItems.get(i);
        if (item.exists()) {
            downloaded.add(item);
        } else {
            notDownloaded.add(item);
        }
    }
    List<TranslationRowData> result = new ArrayList<>();
    if (downloaded.size() > 0) {
        TranslationHeader hdr = new TranslationHeader(getString(R.string.downloaded_translations));
        result.add(hdr);
        boolean needsUpgrade = false;
        for (TranslationItem item : downloaded) {
            result.add(item);
            needsUpgrade = needsUpgrade || item.needsUpgrade();
        }
        if (!needsUpgrade) {
            quranSettings.setHaveUpdatedTranslations(false);
        }
    }
    result.add(new TranslationHeader(getString(R.string.available_translations)));
    result.addAll(notDownloaded);
    adapter.setTranslations(result);
    adapter.notifyDataSetChanged();
}
Also used : TranslationItem(com.quran.labs.androidquran.dao.translation.TranslationItem) TranslationHeader(com.quran.labs.androidquran.dao.translation.TranslationHeader) ArrayList(java.util.ArrayList) TranslationRowData(com.quran.labs.androidquran.dao.translation.TranslationRowData)

Aggregations

TranslationItem (com.quran.labs.androidquran.dao.translation.TranslationItem)8 File (java.io.File)4 TranslationRowData (com.quran.labs.androidquran.dao.translation.TranslationRowData)3 ArrayList (java.util.ArrayList)3 Intent (android.content.Intent)2 IntentFilter (android.content.IntentFilter)2 SparseIntArray (android.util.SparseIntArray)2 TranslationHeader (com.quran.labs.androidquran.dao.translation.TranslationHeader)2 DefaultDownloadReceiver (com.quran.labs.androidquran.service.util.DefaultDownloadReceiver)2 QuranSettings (com.quran.labs.androidquran.util.QuranSettings)2 ContentValues (android.content.ContentValues)1 Bundle (android.os.Bundle)1 Snackbar (android.support.design.widget.Snackbar)1 LocalBroadcastManager (android.support.v4.content.LocalBroadcastManager)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 ActionBar (android.support.v7.app.ActionBar)1 AlertDialog (android.support.v7.app.AlertDialog)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 MenuItem (android.view.MenuItem)1