Refactoring: category names

This commit is contained in:
xynngh 2020-06-14 13:14:06 +04:00
parent 8bbf82a7fe
commit 711298c899
6 changed files with 81 additions and 45 deletions

View File

@ -13,6 +13,7 @@ import androidx.recyclerview.widget.RecyclerView;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import dummydomain.yetanothercallblocker.data.SiaNumberCategoryUtils;
import dummydomain.yetanothercallblocker.sia.model.CommunityReview; import dummydomain.yetanothercallblocker.sia.model.CommunityReview;
class CustomListViewAdapter extends RecyclerView.Adapter<CustomListViewAdapter.CommunityReviewViewHolder> { class CustomListViewAdapter extends RecyclerView.Adapter<CustomListViewAdapter.CommunityReviewViewHolder> {
@ -55,7 +56,7 @@ class CustomListViewAdapter extends RecyclerView.Adapter<CustomListViewAdapter.C
} }
public void bind(CommunityReview item) { public void bind(CommunityReview item) {
tvNumberCategory.setText(item.getCategory().getStringId()); tvNumberCategory.setText(SiaNumberCategoryUtils.getNameResId(item.getCategory()));
String title = item.getTitle(); String title = item.getTitle();
if (TextUtils.isEmpty(title)) { if (TextUtils.isEmpty(title)) {
tvTitle.setVisibility(View.GONE); tvTitle.setVisibility(View.GONE);

View File

@ -16,6 +16,7 @@ import org.greenrobot.eventbus.ThreadMode;
import java.util.Date; import java.util.Date;
import dummydomain.yetanothercallblocker.data.DatabaseSingleton; import dummydomain.yetanothercallblocker.data.DatabaseSingleton;
import dummydomain.yetanothercallblocker.data.SiaNumberCategoryUtils;
import dummydomain.yetanothercallblocker.event.SecondaryDbUpdateFinished; import dummydomain.yetanothercallblocker.event.SecondaryDbUpdateFinished;
import dummydomain.yetanothercallblocker.sia.model.NumberCategory; import dummydomain.yetanothercallblocker.sia.model.NumberCategory;
import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabase; import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabase;
@ -86,7 +87,7 @@ public class DebugActivity extends AppCompatActivity {
NumberCategory category = NumberCategory.getById(item.getCategory()); NumberCategory category = NumberCategory.getById(item.getCategory());
if (category != null) { if (category != null) {
string += DebugActivity.this.getString(category.getStringId()); string += SiaNumberCategoryUtils.getName(DebugActivity.this, category);
} else { } else {
string += "category=" + item.getCategory() + "\n"; string += "category=" + item.getCategory() + "\n";
} }

View File

@ -11,6 +11,7 @@ import android.widget.TextView;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import dummydomain.yetanothercallblocker.data.NumberInfo; import dummydomain.yetanothercallblocker.data.NumberInfo;
import dummydomain.yetanothercallblocker.data.SiaNumberCategoryUtils;
import dummydomain.yetanothercallblocker.sia.model.NumberCategory; import dummydomain.yetanothercallblocker.sia.model.NumberCategory;
import static dummydomain.yetanothercallblocker.IntentHelper.clearTop; import static dummydomain.yetanothercallblocker.IntentHelper.clearTop;
@ -32,7 +33,7 @@ public class InfoDialogHelper {
: null; : null;
if (category != null && category != NumberCategory.NONE) { if (category != null && category != NumberCategory.NONE) {
categoryView.setText(NumberCategory.getString(context, category)); categoryView.setText(SiaNumberCategoryUtils.getName(context, category));
} else { } else {
categoryView.setVisibility(View.GONE); categoryView.setVisibility(View.GONE);
} }

View File

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import dummydomain.yetanothercallblocker.data.NumberInfo; import dummydomain.yetanothercallblocker.data.NumberInfo;
import dummydomain.yetanothercallblocker.data.SiaNumberCategoryUtils;
import dummydomain.yetanothercallblocker.sia.model.NumberCategory; import dummydomain.yetanothercallblocker.sia.model.NumberCategory;
import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabaseItem; import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabaseItem;
@ -135,7 +136,7 @@ public class NotificationHelper {
NumberCategory category = NumberCategory.getById(communityItem.getCategory()); NumberCategory category = NumberCategory.getById(communityItem.getCategory());
if (category != null && category != NumberCategory.NONE) { if (category != null && category != NumberCategory.NONE) {
title += " - " + NumberCategory.getString(context, category); title += " - " + SiaNumberCategoryUtils.getName(context, category);
} }
} }

View File

@ -0,0 +1,50 @@
package dummydomain.yetanothercallblocker.data;
import android.content.Context;
import androidx.annotation.StringRes;
import dummydomain.yetanothercallblocker.R;
import dummydomain.yetanothercallblocker.sia.model.NumberCategory;
public class SiaNumberCategoryUtils {
public static String getName(Context context, NumberCategory category) {
return context.getString(getNameResId(category));
}
@StringRes
public static int getNameResId(NumberCategory category) {
if (category == null) {
return R.string.sia_category_none;
}
switch (category) {
case NONE: return R.string.sia_category_none;
case TELEMARKETER: return R.string.sia_category_telemarketer;
case DEPT_COLLECTOR: return R.string.sia_category_dept_collector;
case SILENT_CALL: return R.string.sia_category_silent;
case NUISANCE_CALL: return R.string.sia_category_nuisance;
case UNSOLICITED_CALL: return R.string.sia_category_unsolicited;
case CALL_CENTER: return R.string.sia_category_call_center;
case FAX_MACHINE: return R.string.sia_category_fax;
case NON_PROFIT: return R.string.sia_category_nonprofit;
case POLITICAL: return R.string.sia_category_political;
case SCAM: return R.string.sia_category_scam;
case PRANK: return R.string.sia_category_prank;
case SMS: return R.string.sia_category_sms;
case SURVEY: return R.string.sia_category_survey;
case OTHER: return R.string.sia_category_other;
case FINANCE_SERVICE: return R.string.sia_category_financial_service;
case COMPANY: return R.string.sia_category_company;
case SERVICE: return R.string.sia_category_service;
case ROBOCALL: return R.string.sia_category_robocall;
// TODO: check: these are probably not present in the db
case SAFE_PERSONAL: return R.string.sia_category_safe_personal;
case SAFE_COMPANY: return R.string.sia_category_safe_company;
case SAFE_NONPROFIT: return R.string.sia_category_safe_nonprofit;
default: throw new RuntimeException("Category not implemented: " + category);
}
}
}

View File

@ -1,52 +1,41 @@
package dummydomain.yetanothercallblocker.sia.model; package dummydomain.yetanothercallblocker.sia.model;
import android.content.Context;
import androidx.annotation.StringRes;
import dummydomain.yetanothercallblocker.R;
public enum NumberCategory { public enum NumberCategory {
NONE(0, R.string.sia_category_none), NONE(0),
TELEMARKETER(1, R.string.sia_category_telemarketer), TELEMARKETER(1),
DEPT_COLLECTOR(2, R.string.sia_category_dept_collector), DEPT_COLLECTOR(2),
SILENT_CALL(3, R.string.sia_category_silent), SILENT_CALL(3),
NUISANCE_CALL(4, R.string.sia_category_nuisance), NUISANCE_CALL(4),
UNSOLICITED_CALL(5, R.string.sia_category_unsolicited), UNSOLICITED_CALL(5),
CALL_CENTER(6, R.string.sia_category_call_center), CALL_CENTER(6),
FAX_MACHINE(7, R.string.sia_category_fax), FAX_MACHINE(7),
NON_PROFIT(8, R.string.sia_category_nonprofit), NON_PROFIT(8),
POLITICAL(9, R.string.sia_category_political), POLITICAL(9),
SCAM(10, R.string.sia_category_scam), SCAM(10),
PRANK(11, R.string.sia_category_prank), PRANK(11),
SMS(12, R.string.sia_category_sms), SMS(12),
SURVEY(13, R.string.sia_category_survey), SURVEY(13),
OTHER(14, R.string.sia_category_other), OTHER(14),
FINANCE_SERVICE(15, R.string.sia_category_financial_service), FINANCE_SERVICE(15),
COMPANY(16, R.string.sia_category_company), COMPANY(16),
SERVICE(17, R.string.sia_category_service), SERVICE(17),
ROBOCALL(18, R.string.sia_category_robocall), ROBOCALL(18),
// TODO: check: these are probably not present in the db // TODO: check: these are probably not present in the db
SAFE_PERSONAL(100, R.string.sia_category_safe_personal), SAFE_PERSONAL(100),
SAFE_COMPANY(101, R.string.sia_category_safe_company), SAFE_COMPANY(101),
SAFE_NONPROFIT(102, R.string.sia_category_safe_nonprofit); SAFE_NONPROFIT(102);
private int id; private int id;
private @StringRes int stringId;
NumberCategory(int id, int stringId) { NumberCategory(int id) {
this.id = id; this.id = id;
this.stringId = stringId;
} }
public int getId() { public int getId() {
return id; return id;
} }
public @StringRes int getStringId() {
return stringId;
}
public static NumberCategory getById(int id) { public static NumberCategory getById(int id) {
for (NumberCategory category : values()) { for (NumberCategory category : values()) {
if (category.getId() == id) return category; if (category.getId() == id) return category;
@ -54,11 +43,4 @@ public enum NumberCategory {
return null; return null;
} }
public static String getString(Context context, NumberCategory category) {
@StringRes int stringId = category != null
? category.getStringId() : R.string.sia_category_none;
return context.getString(stringId);
}
} }