From feee5f20e939a3bf177b20288cc0c03234f3046a Mon Sep 17 00:00:00 2001 From: Tariel Hlontsi Date: Mon, 2 May 2016 14:17:47 +0300 Subject: [PATCH] Refactoring --- .idea/dictionaries/tariel.xml | 7 +++ .idea/misc.xml | 2 +- .idea/modules.xml | 2 +- app/build.gradle | 3 +- .../hikapro/com/backpack/model/ItemModel.java | 47 +++++++++------ .../hikapro/com/backpack/model/SetModel.java | 3 + .../com/backpack/model/database/DAO.java | 20 +++---- app/src/main/res/layout/item.xml | 6 +- app/src/main/res/layout/section.xml | 58 +++++++++++++------ 9 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 .idea/dictionaries/tariel.xml diff --git a/.idea/dictionaries/tariel.xml b/.idea/dictionaries/tariel.xml new file mode 100644 index 0000000..4da906f --- /dev/null +++ b/.idea/dictionaries/tariel.xml @@ -0,0 +1,7 @@ + + + + tariel + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 5d19981..fbb6828 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 53118bf..ea59753 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,8 @@ + - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 0ae0a65..f7c9d6b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,13 +28,12 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - - compile 'com.android.support:support-v4:23.3.0' compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp:logging-interceptor:2.7.0' + compile 'com.android.support:support-v4:23.3.0' compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:design:23.3.0' diff --git a/app/src/main/java/hikapro/com/backpack/model/ItemModel.java b/app/src/main/java/hikapro/com/backpack/model/ItemModel.java index 268be0e..679ab15 100644 --- a/app/src/main/java/hikapro/com/backpack/model/ItemModel.java +++ b/app/src/main/java/hikapro/com/backpack/model/ItemModel.java @@ -30,27 +30,33 @@ public class ItemModel implements Model.Item { private List sortedCategories; private List rawItems; private DAO dao; + private int currentSet; + private Category currentCategory; + private List categoriesCache; private Hashtable> items; - private Hashtable>> cache; + private Hashtable>> cache; public ItemModel() { this.rawCategories = new ArrayList<>(); this.rawItems = new ArrayList<>(); this.sortedCategories = new ArrayList<>(); - this.cache = new Hashtable<>(16, 0.9f); + this.categoriesCache = new ArrayList<>(20); + this.cache = new Hashtable<>(12, 0.9f); this.dao = DAO.getInstance(); + dao.registerObserver(this); } // categories --> @Override public Category getCategoryByPosition(int position) { - return sortedCategories.get(position); + currentCategory = categoriesCache.get(position); + return currentCategory; } @Override public int getCategoriesCount() { - return sortedCategories.size(); + return categoriesCache.size(); } // categories <-- @@ -78,20 +84,12 @@ public class ItemModel implements Model.Item { } @Override public Item getItemByPosition(int categoryId, int position) { - Item ret = null; - Category category = findSortedCategory(categoryId); - if (category != null) { - ret = items.get(category).get(position); - } + Item ret = cache.get(currentSet).get(currentCategory).get(position); return ret; } @Override public int getItemsCount(int categoryId) { - int ret = 0; - Category category = findSortedCategory(categoryId); - if (category != null) { - ret = items.get(category).size(); - } + int ret = cache.get(currentSet).get(currentCategory).size(); return ret; } // items <-- @@ -119,10 +117,18 @@ public class ItemModel implements Model.Item { @Override public void executeQuery() { - Message command = Message.obtain(); - command.what = Command.SET_GET_ITEMS; - command.arg1 = presenter.getCurrentSet().getId(); - dao.executeCommand(command); + + if (cache.contains(currentSet)) { + Hashtable> buff = cache.get(currentSet); + Category[] array = buff.keySet().toArray(new Category[buff.keySet().size()]); + categoriesCache = Arrays.asList(array); + notifyDataSetChanged(); + } else { + Message command = Message.obtain(); + command.what = Command.SET_GET_ITEMS; + command.arg1 = presenter.getCurrentSet().getId(); + dao.executeCommand(command); + } } @Override public void onEvent(Message event) { @@ -141,8 +147,10 @@ public class ItemModel implements Model.Item { case Event.ITEM_INSERT_ERROR : break; case Event.SET_ITEMS_LOAD_COMPLETED : - Hashtable> res = (Hashtable>) event.obj; + Hashtable> res = (Hashtable>) event.obj; cache.put(event.arg1, res); + Category[] array = res.keySet().toArray(new Category[res.keySet().size()]); + categoriesCache = Arrays.asList(array); notifyDataSetChanged(); break; case Event.ITEM_FROM_SET_DELETED : @@ -166,6 +174,7 @@ public class ItemModel implements Model.Item { @Override public void setPresenter(Presenter.ItemList presenter) { this.presenter = presenter; + this.currentSet = presenter.getCurrentSet().getId(); } @Override diff --git a/app/src/main/java/hikapro/com/backpack/model/SetModel.java b/app/src/main/java/hikapro/com/backpack/model/SetModel.java index 1757da9..d07cadb 100644 --- a/app/src/main/java/hikapro/com/backpack/model/SetModel.java +++ b/app/src/main/java/hikapro/com/backpack/model/SetModel.java @@ -107,6 +107,9 @@ public class SetModel implements Model.Set { @Override public void setsReorderNotify() { + for (int i = 0; i < cache.size(); ++i) { + cache.get(i).setLineNumber(i); + } Message command = Message.obtain(); command.what = Command.SET_REORDER; command.obj = cache; diff --git a/app/src/main/java/hikapro/com/backpack/model/database/DAO.java b/app/src/main/java/hikapro/com/backpack/model/database/DAO.java index d5eb553..8aa5054 100644 --- a/app/src/main/java/hikapro/com/backpack/model/database/DAO.java +++ b/app/src/main/java/hikapro/com/backpack/model/database/DAO.java @@ -348,8 +348,8 @@ public class DAO { } return ret; } - private Hashtable readCategories() { - Hashtable ret = new Hashtable<>(20, 0.9f); + private Hashtable readCategories() { + Hashtable ret = new Hashtable<>(20, 0.9f); Cursor cursor = null; SQLiteDatabase db = null; Category category; @@ -361,7 +361,7 @@ public class DAO { null,null,null,null,null); while (cursor.moveToNext()) { category = Db.CategoriesTable.parseCursor(cursor); - ret.put(category.getId(), category.getName()); + ret.put(category.getId(), category); } } catch (SQLiteException e) { //TODO write to log here @@ -630,18 +630,18 @@ public class DAO { message.what = Event.SET_ITEMS_LOAD_ERROR; else { Collections.sort(items); - Hashtable categories = readCategories(); - Hashtable> result = new Hashtable<>(20, 0.9f); - String categoryName; + Hashtable categories = readCategories(); + Hashtable> result = new Hashtable<>(20, 0.9f); + Category category; for (Item item : items) { - categoryName = categories.get(item.getCategory()); - if (result.containsKey(categoryName)) { - result.get(categoryName).add(item); + category = categories.get(item.getCategory()); + if (result.containsKey(category)) { + result.get(category).add(item); } else { List innerList = new ArrayList<>(20); innerList.add(item); - result.put(categoryName, innerList); + result.put(category, innerList); } } message.what = Event.SET_ITEMS_LOAD_COMPLETED; diff --git a/app/src/main/res/layout/item.xml b/app/src/main/res/layout/item.xml index 773bfbf..f10833f 100644 --- a/app/src/main/res/layout/item.xml +++ b/app/src/main/res/layout/item.xml @@ -5,6 +5,8 @@ android:layout_height="wrap_content" android:singleLine="true" android:background="@android:color/transparent" - android:textSize="10sp" + android:textSize="16sp" android:id="@+id/item_text" - android:textStyle="bold" /> \ No newline at end of file + android:textStyle="bold" + android:height="30dp" + /> \ No newline at end of file diff --git a/app/src/main/res/layout/section.xml b/app/src/main/res/layout/section.xml index b722a57..973fa15 100644 --- a/app/src/main/res/layout/section.xml +++ b/app/src/main/res/layout/section.xml @@ -1,25 +1,49 @@ - - + > - + android:orientation="horizontal"> - \ No newline at end of file + + + + + + + + + + + + + + + + + + + \ No newline at end of file