Refactoring

This commit is contained in:
Tariel Hlontsi 2016-05-02 14:17:47 +03:00
parent 03272e0712
commit feee5f20e9
9 changed files with 96 additions and 52 deletions

View File

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="tariel">
<words>
<w>tariel</w>
</words>
</dictionary>
</component>

View File

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -2,8 +2,8 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/BackPack2.iml" filepath="$PROJECT_DIR$/BackPack2.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/packwithme.iml" filepath="$PROJECT_DIR$/packwithme.iml" />
</modules> </modules>
</component> </component>
</project> </project>

View File

@ -28,13 +28,12 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12' 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.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp:logging-interceptor:2.7.0' compile 'com.squareup.okhttp:logging-interceptor:2.7.0'
compile 'com.android.support:support-v4:23.3.0' compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0' compile 'com.android.support:design:23.3.0'

View File

@ -30,27 +30,33 @@ public class ItemModel implements Model.Item {
private List<Category> sortedCategories; private List<Category> sortedCategories;
private List<Item> rawItems; private List<Item> rawItems;
private DAO dao; private DAO dao;
private int currentSet;
private Category currentCategory;
private List<Category> categoriesCache;
private Hashtable<Category, List<Item>> items; private Hashtable<Category, List<Item>> items;
private Hashtable<Integer, Hashtable<String, List<Item>>> cache; private Hashtable<Integer, Hashtable<Category, List<Item>>> cache;
public ItemModel() { public ItemModel() {
this.rawCategories = new ArrayList<>(); this.rawCategories = new ArrayList<>();
this.rawItems = new ArrayList<>(); this.rawItems = new ArrayList<>();
this.sortedCategories = 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(); this.dao = DAO.getInstance();
dao.registerObserver(this);
} }
// categories --> // categories -->
@Override @Override
public Category getCategoryByPosition(int position) { public Category getCategoryByPosition(int position) {
return sortedCategories.get(position); currentCategory = categoriesCache.get(position);
return currentCategory;
} }
@Override @Override
public int getCategoriesCount() { public int getCategoriesCount() {
return sortedCategories.size(); return categoriesCache.size();
} }
// categories <-- // categories <--
@ -78,20 +84,12 @@ public class ItemModel implements Model.Item {
} }
@Override @Override
public Item getItemByPosition(int categoryId, int position) { public Item getItemByPosition(int categoryId, int position) {
Item ret = null; Item ret = cache.get(currentSet).get(currentCategory).get(position);
Category category = findSortedCategory(categoryId);
if (category != null) {
ret = items.get(category).get(position);
}
return ret; return ret;
} }
@Override @Override
public int getItemsCount(int categoryId) { public int getItemsCount(int categoryId) {
int ret = 0; int ret = cache.get(currentSet).get(currentCategory).size();
Category category = findSortedCategory(categoryId);
if (category != null) {
ret = items.get(category).size();
}
return ret; return ret;
} }
// items <-- // items <--
@ -119,11 +117,19 @@ public class ItemModel implements Model.Item {
@Override @Override
public void executeQuery() { public void executeQuery() {
if (cache.contains(currentSet)) {
Hashtable<Category, List<Item>> buff = cache.get(currentSet);
Category[] array = buff.keySet().toArray(new Category[buff.keySet().size()]);
categoriesCache = Arrays.asList(array);
notifyDataSetChanged();
} else {
Message command = Message.obtain(); Message command = Message.obtain();
command.what = Command.SET_GET_ITEMS; command.what = Command.SET_GET_ITEMS;
command.arg1 = presenter.getCurrentSet().getId(); command.arg1 = presenter.getCurrentSet().getId();
dao.executeCommand(command); dao.executeCommand(command);
} }
}
@Override @Override
public void onEvent(Message event) { public void onEvent(Message event) {
@ -141,8 +147,10 @@ public class ItemModel implements Model.Item {
case Event.ITEM_INSERT_ERROR : case Event.ITEM_INSERT_ERROR :
break; break;
case Event.SET_ITEMS_LOAD_COMPLETED : case Event.SET_ITEMS_LOAD_COMPLETED :
Hashtable<String, List<Item>> res = (Hashtable<String, List<Item>>) event.obj; Hashtable<Category, List<Item>> res = (Hashtable<Category, List<Item>>) event.obj;
cache.put(event.arg1, res); cache.put(event.arg1, res);
Category[] array = res.keySet().toArray(new Category[res.keySet().size()]);
categoriesCache = Arrays.asList(array);
notifyDataSetChanged(); notifyDataSetChanged();
break; break;
case Event.ITEM_FROM_SET_DELETED : case Event.ITEM_FROM_SET_DELETED :
@ -166,6 +174,7 @@ public class ItemModel implements Model.Item {
@Override @Override
public void setPresenter(Presenter.ItemList presenter) { public void setPresenter(Presenter.ItemList presenter) {
this.presenter = presenter; this.presenter = presenter;
this.currentSet = presenter.getCurrentSet().getId();
} }
@Override @Override

View File

@ -107,6 +107,9 @@ public class SetModel implements Model.Set {
@Override @Override
public void setsReorderNotify() { public void setsReorderNotify() {
for (int i = 0; i < cache.size(); ++i) {
cache.get(i).setLineNumber(i);
}
Message command = Message.obtain(); Message command = Message.obtain();
command.what = Command.SET_REORDER; command.what = Command.SET_REORDER;
command.obj = cache; command.obj = cache;

View File

@ -348,8 +348,8 @@ public class DAO {
} }
return ret; return ret;
} }
private Hashtable<Integer, String> readCategories() { private Hashtable<Integer, Category> readCategories() {
Hashtable<Integer, String> ret = new Hashtable<>(20, 0.9f); Hashtable<Integer, Category> ret = new Hashtable<>(20, 0.9f);
Cursor cursor = null; Cursor cursor = null;
SQLiteDatabase db = null; SQLiteDatabase db = null;
Category category; Category category;
@ -361,7 +361,7 @@ public class DAO {
null,null,null,null,null); null,null,null,null,null);
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
category = Db.CategoriesTable.parseCursor(cursor); category = Db.CategoriesTable.parseCursor(cursor);
ret.put(category.getId(), category.getName()); ret.put(category.getId(), category);
} }
} catch (SQLiteException e) { } catch (SQLiteException e) {
//TODO write to log here //TODO write to log here
@ -630,18 +630,18 @@ public class DAO {
message.what = Event.SET_ITEMS_LOAD_ERROR; message.what = Event.SET_ITEMS_LOAD_ERROR;
else { else {
Collections.sort(items); Collections.sort(items);
Hashtable<Integer, String> categories = readCategories(); Hashtable<Integer, Category> categories = readCategories();
Hashtable<String, List<Item>> result = new Hashtable<>(20, 0.9f); Hashtable<Category, List<Item>> result = new Hashtable<>(20, 0.9f);
String categoryName; Category category;
for (Item item : items) { for (Item item : items) {
categoryName = categories.get(item.getCategory()); category = categories.get(item.getCategory());
if (result.containsKey(categoryName)) { if (result.containsKey(category)) {
result.get(categoryName).add(item); result.get(category).add(item);
} else { } else {
List<Item> innerList = new ArrayList<>(20); List<Item> innerList = new ArrayList<>(20);
innerList.add(item); innerList.add(item);
result.put(categoryName, innerList); result.put(category, innerList);
} }
} }
message.what = Event.SET_ITEMS_LOAD_COMPLETED; message.what = Event.SET_ITEMS_LOAD_COMPLETED;

View File

@ -5,6 +5,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:singleLine="true"
android:background="@android:color/transparent" android:background="@android:color/transparent"
android:textSize="10sp" android:textSize="16sp"
android:id="@+id/item_text" android:id="@+id/item_text"
android:textStyle="bold" /> android:textStyle="bold"
android:height="30dp"
/>

View File

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/linear"> >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="48dp" android:layout_height="48dp"
@ -16,10 +22,28 @@
android:id="@+id/section_text" android:id="@+id/section_text"
android:textStyle="bold"/> android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linear">
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:id="@+id/category_inner_recycler" android:id="@+id/category_inner_recycler"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="0dp"
/> android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
</LinearLayout> </LinearLayout>
</RelativeLayout>