这是一个问题而不是问题。我有两个带有名为Type的String字段的实体。两者都有20多种类型:“票据”,“运输”,“奖励”等。我想将它们全部放入图表中,这产生了一点问题,因为它总共创建了60((!)60个,因为我想要按日期获取数据(例如,日,月等)在ViewModel构造函数中的实例,在片段中包含60个以上的观察者。所以我的问题是: 存在一些问题,使其在代码(井井有条),应用程序性能方面更加性感,还是我不关心它?
一个实体(它们几乎相同):
@Entity(tableName = "expense_table")
public class Expense {
@PrimaryKey(autoGenerate = true)
private int expenseId;
private String note;
private Double value;
private String type;
private Long dateLong = System.currentTimeMillis();
private String date = new SimpleDateFormat("MM/yyyy").format(new Date(dateLong));
private static Calendar cal = Calendar.getInstance();
private int month = cal.get(Calendar.MONTH) + 1;
private int day = cal.get(Calendar.DAY_OF_MONTH);
private int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
private String weekDay = new DateFormatSymbols().getWeekdays()[dayOfWeek];
在道中查询:
@Query("SELECT SUM(value) FROM expense_table WHERE type = :type")
MutableLiveData<List<Double>> getTotalType(String type);
其中一个存储库(如您所见,现在有很多实例,共同编码会更加复杂):
public class ExpenseRepository {
private ExpenseDao expenseDao;
private LiveData<List<Expense>> allExpensesDay;
private LiveData<List<Expense>> allExpensesMonth;
private LiveData<List<Expense>> allExpenses;
private LiveData<Double> totalValue;
private LiveData<Double> totalValueDay;
private LiveData<Double> totalValueMonth;
private MutableLiveData<List<Double>> totalType;
private String type = "Bills";
public ExpenseRepository(Application application) {
ExpenseIncomeDatabase database = ExpenseIncomeDatabase.getInstance(application);
expenseDao = database.expenseDao();
allExpenses = expenseDao.getAllExpenses();
allExpensesDay = expenseDao.getExpensesDay();
allExpensesMonth = expenseDao.getExpensesMonth();
totalValueDay = expenseDao.getTotalValueDay();
totalValueMonth = expenseDao.getTotalValueMonth();
totalValue = expenseDao.getTotalValue();
totalType = expenseDao.getTotalType(type);
存储库之一:
private LiveData<Double> totalExpenseValue;
private LiveData<Double> totalIncomeValue;
private MutableLiveData<List<Double>> totalType;
public TotalStatsViewModel(@NonNull Application application) {
super(application);
ExpenseRepository expenseRepository = new ExpenseRepository(application);
ExpenseRepository expenseChartsRepository = new ExpenseRepository(application);
IncomeRepository incomeRepository = new IncomeRepository(application);
totalExpenseValue = expenseChartsRepository.getTotalValue();
totalIncomeValue = incomeRepository.getTotalValue();
totalType = expenseRepository.getTotalType();
}
public LiveData<Double> getTotalExpenseValue() {
return totalExpenseValue;
}
public LiveData<Double> getTotalIncomeValue() {
return totalIncomeValue;
}
public LiveData<List<Double>> getTotalType() {
return totalType;
}
如您所见,现在有很多代码。将来会更多。也许这是一个愚蠢的问题,但是我只关心顺序和性能。