Properly notify changes to dictionary list

Julien LepillerSun May 31 17:36:32+0200 2020

bc4238e

Properly notify changes to dictionary list

app/src/main/java/eu/lepiller/nani/DictionaryActivity.java

2929
public class DictionaryActivity extends AppCompatActivity {
3030
    static final int DICO_REQUEST = 1;
3131
    DictionariesAdapter adapter;
32+
    ArrayList<Dictionary> dictionaries;
3233
3334
    @Override
3435
    protected void onCreate(Bundle savedInstanceState) {

4142
        final ListView list_view = findViewById(R.id.dictionary_view);
4243
        final SwipeRefreshLayout refresher = findViewById(R.id.dictionary_refresh_layout);
4344
44-
        final ArrayList<Dictionary> dictionaries = DictionaryFactory.getDictionnaries(getApplicationContext());
45+
        dictionaries = DictionaryFactory.getDictionnaries(getApplicationContext());
4546
        adapter = new DictionariesAdapter(getApplicationContext(), dictionaries);
4647
        list_view.setAdapter(adapter);
4748

113114
            super.onPostExecute(i);
114115
            refresher.setRefreshing(false);
115116
            DictionaryFactory.updatePackageList();
117+
            dictionaries.clear();
118+
            dictionaries.addAll(DictionaryFactory.getDictionnaries(getApplicationContext()));
116119
            adapter.notifyDataSetChanged();
117120
            if(i == null)
118121
                return;

app/src/main/java/eu/lepiller/nani/dictionary/DictionariesAdapter.java

5454
        description_view.setText(dictionary.getDescription());
5555
        if(!dictionary.isUpToDate())
5656
            convertView.setBackgroundColor(context.getResources().getColor(R.color.colorUpdate));
57+
        else
58+
            convertView.setBackgroundColor(0);
5759
5860
        // Return the completed view to render on screen
5961
        return convertView;

app/src/main/java/eu/lepiller/nani/dictionary/DictionaryFactory.java

8484
                int current = LIST_PARSER_NONE;
8585
                String currentLanguage = "";
8686
87+
                Dictionary d;
8788
                while((line = br.readLine()) != null) {
8889
                    if(line.isEmpty()) {
8990
                        // create dictionary
9091
                        if(type.compareTo("radk") == 0) {
91-
                            dictionaries.add(new RadicalDict(name,
92+
                            d = new RadicalDict(name,
9293
                                    chooseLanguage(synopsis),
9394
                                    chooseLanguage(description),
9495
                                    cacheDir,
9596
                                    url,
9697
                                    size,
9798
                                    entries,
98-
                                    sha256));
99+
                                    sha256);
99100
                        } else if (type.compareTo("jmdict") == 0) {
100-
                            dictionaries.add(new JMDict(name,
101+
                            d = new JMDict(name,
101102
                                    chooseLanguage(synopsis),
102103
                                    chooseLanguage(description),
103104
                                    cacheDir,
104105
                                    url,
105106
                                    size,
106107
                                    entries,
107-
                                    sha256));
108+
                                    sha256);
109+
                        } else {
110+
                            continue;
111+
                        }
112+
113+
                        if(d.isDownloaded()) {
114+
                            dictionaries.add(0, d);
115+
                        } else {
116+
                            dictionaries.add(d);
108117
                        }
109118
                    }
110119