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="Remove" />
</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" />
</component>
<component name="ProjectType">

View File

@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<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$/packwithme.iml" filepath="$PROJECT_DIR$/packwithme.iml" />
</modules>
</component>
</project>

View File

@ -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'

View File

@ -30,27 +30,33 @@ public class ItemModel implements Model.Item {
private List<Category> sortedCategories;
private List<Item> rawItems;
private DAO dao;
private int currentSet;
private Category currentCategory;
private List<Category> categoriesCache;
private Hashtable<Category, List<Item>> items;
private Hashtable<Integer, Hashtable<String, List<Item>>> cache;
private Hashtable<Integer, Hashtable<Category, List<Item>>> 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<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();
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<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);
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

View File

@ -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;

View File

@ -348,8 +348,8 @@ public class DAO {
}
return ret;
}
private Hashtable<Integer, String> readCategories() {
Hashtable<Integer, String> ret = new Hashtable<>(20, 0.9f);
private Hashtable<Integer, Category> readCategories() {
Hashtable<Integer, Category> 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<Integer, String> categories = readCategories();
Hashtable<String, List<Item>> result = new Hashtable<>(20, 0.9f);
String categoryName;
Hashtable<Integer, Category> categories = readCategories();
Hashtable<Category, List<Item>> 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<Item> innerList = new ArrayList<>(20);
innerList.add(item);
result.put(categoryName, innerList);
result.put(category, innerList);
}
}
message.what = Event.SET_ITEMS_LOAD_COMPLETED;

View File

@ -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" />
android:textStyle="bold"
android:height="30dp"
/>

View File

@ -1,25 +1,49 @@
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear">
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:singleLine="true"
android:textAllCaps="true"
android:background="@android:color/transparent"
android:textSize="16sp"
android:id="@+id/section_text"
android:textStyle="bold" />
>
<android.support.v7.widget.RecyclerView
android:id="@+id/category_inner_recycler"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:orientation="horizontal">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:singleLine="true"
android:textAllCaps="true"
android:background="@android:color/transparent"
android:textSize="16sp"
android:id="@+id/section_text"
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:id="@+id/category_inner_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>