Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.blogspot.e_kanivets.moneytracker.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.util.CategoryAutoCompleter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
* Custom adapter to autocomplete categories.
* Created on 3/18/16.
Expand All @@ -25,6 +33,31 @@ public CategoryAutoCompleteAdapter(Context context, int resource, CategoryAutoCo
this.autoCompleter = autoCompleter;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.view_category_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();

final String category = resultList.get(position);

viewHolder.tvCategory.setText(category);
viewHolder.ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
autoCompleter.removeFromAutoComplete(category);
resultList.remove(category);
notifyDataSetChanged();
}
});

return convertView;
}

@Override
public int getCount() {
return resultList.size();
Expand Down Expand Up @@ -63,4 +96,15 @@ protected void publishResults(CharSequence constraint, FilterResults results) {
}
};
}

public static class ViewHolder {
@Bind(R.id.tv_category)
TextView tvCategory;
@Bind(R.id.iv_cancel)
View ivCancel;

public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import com.blogspot.e_kanivets.moneytracker.R;

import java.util.HashSet;
import java.util.Set;

/**
* Controller class to encapsulate Shared Preferences handling logic.
* Not deal with {@link com.blogspot.e_kanivets.moneytracker.repo.base.IRepo} instances as others.
Expand All @@ -22,6 +25,7 @@ public class PreferenceController {
private static final String KEY_LAST_TS = "key_last_ts";
private static final String KEY_PERIOD_TYPE = "key_period_type";
private static final String KEY_DROPBOX_ACCESS_TOKEN = "key_dropbox_access_token";
private static final String KEY_FILTERED_CATEGORIES = "key_filtered_categories";

private static final int RATE_PERIOD = 5;

Expand All @@ -34,7 +38,7 @@ public PreferenceController(@NonNull Context context) {

public void addLaunchCount() {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
editor.putInt(LAUNCH_COUNT, preferences.getInt(LAUNCH_COUNT, 0) + 1);
editor.apply();
}
Expand All @@ -50,16 +54,13 @@ public boolean checkRateDialog() {
}

public void appRated() {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();
SharedPreferences.Editor editor = getEditor();
editor.putBoolean(APP_RATED, true);
editor.apply();
}

public void writeFirstTs(long firstTs) {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();

SharedPreferences.Editor editor = getEditor();
editor.putLong(KEY_FIRST_TS, firstTs);
editor.apply();
}
Expand All @@ -73,21 +74,23 @@ public void writeLastTs(long lastTs) {
}

public void writePeriodType(String periodType) {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();

SharedPreferences.Editor editor = getEditor();
editor.putString(KEY_PERIOD_TYPE, periodType);
editor.apply();
}

public void writeDropboxAccessToken(String accessToken) {
SharedPreferences preferences = getDefaultPrefs();
SharedPreferences.Editor editor = preferences.edit();

SharedPreferences.Editor editor = getEditor();
editor.putString(KEY_DROPBOX_ACCESS_TOKEN, accessToken);
editor.apply();
}

public void writeFilteredCategories(@Nullable Set<String> categorySet) {
SharedPreferences.Editor editor = getEditor();
editor.putStringSet(KEY_FILTERED_CATEGORIES, categorySet);
editor.apply();
}

public long readDefaultAccountId() {
String defaultAccountPref = context.getString(R.string.pref_default_account);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Expand Down Expand Up @@ -129,7 +132,18 @@ public String readDropboxAccessToken() {
return getDefaultPrefs().getString(KEY_DROPBOX_ACCESS_TOKEN, null);
}

@NonNull
public Set<String> readFilteredCategories() {
return getDefaultPrefs().getStringSet(KEY_FILTERED_CATEGORIES, new HashSet<String>());
}

@NonNull
private SharedPreferences getDefaultPrefs() {
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
}

@NonNull
private SharedPreferences.Editor getEditor() {
return getDefaultPrefs().edit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
import com.blogspot.e_kanivets.moneytracker.repo.DbHelper;
import com.blogspot.e_kanivets.moneytracker.controller.base.BaseController;
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
import com.blogspot.e_kanivets.moneytracker.repo.base.IRepo;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Controller class to encapsulate category handling logic.
Expand All @@ -20,18 +23,55 @@ public class CategoryController extends BaseController<Category> {
@SuppressWarnings("unused")
private static final String TAG = "CategoryController";

public CategoryController(@NonNull IRepo<Category> categoryRepo) {
@NonNull
private final PreferenceController preferenceController;
@NonNull
private Set<String> filteredCategories;

public CategoryController(@NonNull IRepo<Category> categoryRepo,
@NonNull PreferenceController preferenceController) {
super(categoryRepo);
this.preferenceController = preferenceController;
filteredCategories = preferenceController.readFilteredCategories();
}

public Category readOrCreate(@Nullable String categoryName) {
if (categoryName == null || categoryName.trim().isEmpty()) return null;

enableCategory(categoryName);

String condition = DbHelper.NAME_COLUMN + "=?";
String[] args = {categoryName};
List<Category> categoryList = repo.readWithCondition(condition, args);

if (categoryList.size() >= 1) return categoryList.get(0);
else return repo.create(new Category(categoryName));
}
}

@NonNull
public List<Category> readFiltered() {
List<Category> filteredList = new ArrayList<>();

for (Category category : readAll()) {
if (!filteredCategories.contains(category.getName())) filteredList.add(category);
}

return filteredList;
}

/**
* @param category to disable when request filtered list.
*/
public void disableCategory(@Nullable String category) {
filteredCategories.add(category);
preferenceController.writeFilteredCategories(filteredCategories);
}

/**
* @param category to enable when request filtered list.
*/
public void enableCategory(@Nullable String category) {
filteredCategories.remove(category);
preferenceController.writeFilteredCategories(filteredCategories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public AccountController providesAccountController(IRepo<Account> accountRepo,
@Provides
@NonNull
@Singleton
public CategoryController providesCategoryController(IRepo<Category> categoryRepo) {
return new CategoryController(categoryRepo);
public CategoryController providesCategoryController(IRepo<Category> categoryRepo,
PreferenceController preferenceController) {
return new CategoryController(categoryRepo, preferenceController);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
*/
public class CategoryAutoCompleter {
private List<String> categoryList;
private CategoryController categoryController;

public CategoryAutoCompleter(CategoryController categoryController) {
this.categoryController = categoryController;
categoryList = new ArrayList<>();

for (Category category : categoryController.readAll()) {
for (Category category : categoryController.readFiltered()) {
categoryList.add(category.getName());
}
}
Expand All @@ -32,4 +34,9 @@ public List<String> completeByPart(String part) {

return resultList;
}

public void removeFromAutoComplete(String category) {
categoryList.remove(category);
categoryController.disableCategory(category);
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_cancel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
tools:ignore="VectorRaster"
tools:targetApi="kitkat">
<path
android:fillColor="#FF757575"
android:pathData="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
</vector>
29 changes: 25 additions & 4 deletions app/src/main/res/layout/view_category_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/half_margin"
android:paddingTop="@dimen/half_margin"
android:theme="@style/Text_Body1" />
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingEnd="@dimen/half_margin"
android:paddingStart="@dimen/normal_margin"
tools:ignore="UseCompoundDrawables">

<TextView
android:id="@+id/tv_category"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/half_margin"
android:paddingTop="@dimen/half_margin"
android:theme="@style/Text_Body1" />

<ImageView
android:id="@+id/iv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@android:string/cancel"
android:src="@drawable/ic_cancel" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.blogspot.e_kanivets.moneytracker.controller.PreferenceController;
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;
import com.blogspot.e_kanivets.moneytracker.repo.base.IRepo;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,10 +23,16 @@
* @author Evgenii Kanivets
*/
public class CategoryControllerTest {
private PreferenceController mockPrefs;

@Before
public void setUp() throws Exception {
mockPrefs = Mockito.mock(PreferenceController.class);
}

@Test
public void testReadOrCreate() throws Exception {
CategoryController categoryController = new CategoryController(emptyRepo);
CategoryController categoryController = new CategoryController(emptyRepo, mockPrefs);

assertNull(categoryController.readOrCreate(null));
assertNull(categoryController.readOrCreate(""));
Expand All @@ -34,7 +43,7 @@ public void testReadOrCreate() throws Exception {
result = categoryController.readOrCreate("category 1");
assertEquals("category 1", result.getName());

categoryController = new CategoryController(repo);
categoryController = new CategoryController(repo, mockPrefs);

result = categoryController.readOrCreate("category 1");
assertEquals("category 1", result.getName());
Expand Down