Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Prev Previous commit
Next Next commit
#100[30m]. Remove categories from autocomplete.
  • Loading branch information
Evgenii Kanivets committed Aug 16, 2016
commit e5b5aac21c764a3be3c2c587543200eee835e48d
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 @@ -4,9 +4,7 @@
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;

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

/**
* Util class to encapsulate category autocomplete logic.
Expand All @@ -16,13 +14,13 @@
*/
public class CategoryAutoCompleter {
private List<String> categoryList;
private Set<String> filterSet;
private CategoryController categoryController;

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

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

public void removeFromAutoComplete(String category) {
filterSet.add(category);
categoryList.remove(category);
categoryController.disableCategory(category);
}
}