Skip to content

Commit

Permalink
refactor callback to lambda and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerDarren committed Jan 13, 2020
1 parent da2dbc7 commit bf8eefe
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import io.github.skywalkerdarren.simpleaccounting.model.dao.TypeDao
import io.github.skywalkerdarren.simpleaccounting.model.database.AppDatabase
import io.github.skywalkerdarren.simpleaccounting.model.database.AppDatabase.Companion.getInstance
import io.github.skywalkerdarren.simpleaccounting.model.datasource.AppDataSource
import io.github.skywalkerdarren.simpleaccounting.model.datasource.StatsDataSource.*
import io.github.skywalkerdarren.simpleaccounting.model.datasource.TypeDataSource.LoadTypeCallBack
import io.github.skywalkerdarren.simpleaccounting.model.datasource.TypeDataSource.LoadTypesCallBack
import io.github.skywalkerdarren.simpleaccounting.model.entity.*
Expand Down Expand Up @@ -298,7 +297,7 @@ class AppRepository private constructor(val executors: AppExecutors, val databas
return billStats
}

override fun getBillsAnnualStats(year: Int, callBack: LoadBillsStatsCallBack) {
override fun getBillsAnnualStats(year: Int, callBack: (List<BillStats>?) -> Unit) {
execute(object : LoadData {
override fun load() {
Log.d(TAG, "getBillsAnnualStats: in " + Thread.currentThread().name)
Expand All @@ -321,12 +320,12 @@ class AppRepository private constructor(val executors: AppExecutors, val databas
start = start.plusMonths(1)
end = end.plusMonths(1)
}
executors.mainThread().execute { callBack.onBillStatsLoaded(billStatsList) }
executors.mainThread().execute { callBack(billStatsList) }
}
})
}

override fun getBillStats(start: DateTime, end: DateTime, callBack: LoadBillStatsCallBack) {
override fun getBillStats(start: DateTime, end: DateTime, callBack: (BillStats?) -> Unit) {
execute(object : LoadData {
override fun load() {
Log.d(TAG, "getBillStats: in " + Thread.currentThread().name)
Expand All @@ -341,42 +340,42 @@ class AppRepository private constructor(val executors: AppExecutors, val databas
}
}
}
executors.mainThread().execute { callBack.onBillStatsLoaded(billStats) }
executors.mainThread().execute { callBack(billStats) }
}
})
}

override fun getTypesStats(start: DateTime, end: DateTime, isExpense: Boolean, callBack: LoadTypesStatsCallBack) {
override fun getTypesStats(start: DateTime, end: DateTime, isExpense: Boolean, callBack: (List<TypeStats>?) -> Unit) {
execute(object : LoadData {
override fun load() {
Log.d(TAG, "getTypesStats: in " + Thread.currentThread().name)
val typesStats = statsDao.getTypesStats(start, end, isExpense)
executors.mainThread().execute { callBack.onTypesStatsLoaded(typesStats) }
executors.mainThread().execute { callBack(typesStats) }
}
})
}

override fun getTypeStats(start: DateTime, end: DateTime, typeId: UUID, callBack: LoadTypeStatsCallBack) {
override fun getTypeStats(start: DateTime, end: DateTime, typeId: UUID, callBack: (TypeStats?) -> Unit) {
execute(object : LoadData {
override fun load() {
Log.d(TAG, "getTypeStats: in " + Thread.currentThread().name)
val typeStats = statsDao.getTypeStats(start, end, typeId)
executors.mainThread().execute { callBack.onTypeStatsLoaded(typeStats) }
executors.mainThread().execute { callBack(typeStats) }
}
})
}

override fun getTypeAverage(start: DateTime, end: DateTime, typeId: UUID, callBack: LoadTypeStatsCallBack) {
override fun getTypeAverage(start: DateTime, end: DateTime, typeId: UUID, callBack: (TypeStats?) -> Unit) {
execute(object : LoadData {
override fun load() {
Log.d(TAG, "getTypeAverage: in " + Thread.currentThread().name)
val typeStats = statsDao.getTypeAverageStats(start, end, typeId)
executors.mainThread().execute { callBack.onTypeStatsLoaded(typeStats) }
executors.mainThread().execute { callBack(typeStats) }
}
})
}

override fun getAccountStats(accountId: UUID, start: DateTime, end: DateTime, callBack: LoadAccountStatsCallBack) {
override fun getAccountStats(accountId: UUID, start: DateTime, end: DateTime, callBack: (AccountStats?) -> Unit) {
execute(object : LoadData {
override fun load() {
Log.d(TAG, "getAccountStats: in " + Thread.currentThread().name)
Expand All @@ -389,7 +388,7 @@ class AppRepository private constructor(val executors: AppExecutors, val databas
accountStats.income = balance
}
}
executors.mainThread().execute { callBack.onAccountStatsLoaded(accountStats) }
executors.mainThread().execute { callBack(accountStats) }
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,10 @@ import org.joda.time.DateTime
import java.util.*

interface StatsDataSource {
fun getBillsAnnualStats(year: Int, callBack: LoadBillsStatsCallBack)
fun getBillStats(start: DateTime, end: DateTime, callBack: LoadBillStatsCallBack)
fun getTypesStats(start: DateTime, end: DateTime, isExpense: Boolean, callBack: LoadTypesStatsCallBack)
fun getTypeStats(start: DateTime, end: DateTime, typeId: UUID, callBack: LoadTypeStatsCallBack)
fun getTypeAverage(start: DateTime, end: DateTime, typeId: UUID, callBack: LoadTypeStatsCallBack)
fun getAccountStats(accountId: UUID, start: DateTime, end: DateTime, callBack: LoadAccountStatsCallBack)
interface LoadBillStatsCallBack {
fun onBillStatsLoaded(billStats: BillStats?)
}

interface LoadBillsStatsCallBack {
fun onBillStatsLoaded(billsStats: List<BillStats>?)
}

interface LoadTypeStatsCallBack {
fun onTypeStatsLoaded(typeStats: TypeStats?)
}

interface LoadTypesStatsCallBack {
fun onTypesStatsLoaded(typesStats: List<TypeStats>?)
}

interface LoadAccountStatsCallBack {
fun onAccountStatsLoaded(accountStats: AccountStats?)
}
fun getBillsAnnualStats(year: Int, callBack: (List<BillStats>?) -> Unit)
fun getBillStats(start: DateTime, end: DateTime, callBack: (BillStats?) -> Unit)
fun getTypesStats(start: DateTime, end: DateTime, isExpense: Boolean, callBack: (List<TypeStats>?) -> Unit)
fun getTypeStats(start: DateTime, end: DateTime, typeId: UUID, callBack: (TypeStats?) -> Unit)
fun getTypeAverage(start: DateTime, end: DateTime, typeId: UUID, callBack: (TypeStats?) -> Unit)
fun getAccountStats(accountId: UUID, start: DateTime, end: DateTime, callBack: (AccountStats?) -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@

import org.joda.time.DateTime;

import java.util.List;

import io.github.skywalkerdarren.simpleaccounting.R;
import io.github.skywalkerdarren.simpleaccounting.model.AppRepository;
import io.github.skywalkerdarren.simpleaccounting.model.datasource.StatsDataSource;
import io.github.skywalkerdarren.simpleaccounting.model.entity.BillStats;
import io.github.skywalkerdarren.simpleaccounting.ui.activity.BillEditActivity;
import io.github.skywalkerdarren.simpleaccounting.ui.activity.MainActivity;
import io.github.skywalkerdarren.simpleaccounting.util.AppExecutors;
import io.github.skywalkerdarren.simpleaccounting.util.view.FormatUtil;
import kotlin.Unit;

/**
* 桌面小部件
Expand All @@ -28,7 +26,7 @@
* @date 2018/3/16
*/

public class DesktopWidget extends AppWidgetProvider implements StatsDataSource.LoadBillsStatsCallBack {
public class DesktopWidget extends AppWidgetProvider {
public static final String EXTRA_ACTION_UP = "android.appwidget.action.APPWIDGET_UPDATE";
private static final int REQUEST_BILL = 0;
private Context mContext;
Expand Down Expand Up @@ -67,18 +65,16 @@ private void refreshData(Context context) {
mRemoteViews.setOnClickPendingIntent(R.id.add_bill_text_view, pendingAddIntent);
mRemoteViews.setOnClickPendingIntent(R.id.icon, pendingIconIntent);

repository.getBillsAnnualStats(DateTime.now().getYear(), this);

ComponentName name = new ComponentName(mContext, DesktopWidget.class);
AppWidgetManager.getInstance(mContext).updateAppWidget(name, mRemoteViews);
}
repository.getBillsAnnualStats(DateTime.now().getYear(), billsStats -> {
BillStats billStats = billsStats.get(DateTime.now().getMonthOfYear() - 1);
mRemoteViews.setTextViewText(R.id.income_text_view, FormatUtil.getNumeric(billStats.getIncome()));
mRemoteViews.setTextViewText(R.id.expense_text_view, FormatUtil.getNumeric(billStats.getExpense()));
mRemoteViews.setTextViewText(R.id.total_text_view, FormatUtil.getNumeric(billStats.getSum()));
ComponentName name = new ComponentName(mContext, DesktopWidget.class);
AppWidgetManager.getInstance(mContext).updateAppWidget(name, mRemoteViews);
return Unit.INSTANCE;
});

@Override
public void onBillStatsLoaded(List<BillStats> billsStats) {
BillStats billStats = billsStats.get(DateTime.now().getMonthOfYear() - 1);
mRemoteViews.setTextViewText(R.id.income_text_view, FormatUtil.getNumeric(billStats.getIncome()));
mRemoteViews.setTextViewText(R.id.expense_text_view, FormatUtil.getNumeric(billStats.getExpense()));
mRemoteViews.setTextViewText(R.id.total_text_view, FormatUtil.getNumeric(billStats.getSum()));
ComponentName name = new ComponentName(mContext, DesktopWidget.class);
AppWidgetManager.getInstance(mContext).updateAppWidget(name, mRemoteViews);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import io.github.skywalkerdarren.simpleaccounting.R;
import io.github.skywalkerdarren.simpleaccounting.base.BaseAppBarStateChangeListener;
import io.github.skywalkerdarren.simpleaccounting.base.BaseFragment;
import io.github.skywalkerdarren.simpleaccounting.databinding.FragmentBillDetailBinding;
import io.github.skywalkerdarren.simpleaccounting.model.entity.Bill;
import io.github.skywalkerdarren.simpleaccounting.ui.activity.BillEditActivity;
Expand All @@ -53,8 +52,7 @@
* @author darren
* @date 2018/2/21
*/
public class BillDetailFragment extends BaseFragment {
private static final String TAG = "BillDetailFragment";
public class BillDetailFragment extends Fragment {

private static final String ARG_BILL = "bill";
private static final String ARG_CX = "cx";
Expand Down Expand Up @@ -266,7 +264,8 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
}

@Override
protected void updateUI() {
public void onResume() {
super.onResume();
mViewModel.start(mBill);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import io.github.skywalkerdarren.simpleaccounting.model.AppRepository
import io.github.skywalkerdarren.simpleaccounting.model.datasource.StatsDataSource.LoadBillStatsCallBack
import io.github.skywalkerdarren.simpleaccounting.model.entity.Account
import io.github.skywalkerdarren.simpleaccounting.model.entity.BillStats
import io.github.skywalkerdarren.simpleaccounting.util.view.FormatUtil
import org.joda.time.DateTime

Expand Down Expand Up @@ -40,13 +38,11 @@ class AccountViewModel(private val mRepository: AppRepository) : ViewModel() {
mRepository.getAccounts { accounts: List<Account>? ->
this@AccountViewModel.accounts.value = accounts
}
mRepository.getBillStats(DateTime(0), DateTime.now(), object : LoadBillStatsCallBack {
override fun onBillStatsLoaded(billStats: BillStats?) {
nav.value = FormatUtil.getNumeric(billStats?.sum)
liability.value = FormatUtil.getNumeric(billStats?.expense)
totalAssets.value = FormatUtil.getNumeric(billStats?.income)
}
})
mRepository.getBillStats(DateTime(0), DateTime.now()) { billStats ->
nav.value = FormatUtil.getNumeric(billStats?.sum)
liability.value = FormatUtil.getNumeric(billStats?.expense)
totalAssets.value = FormatUtil.getNumeric(billStats?.income)
}
}

fun changePosition(a: Account, b: Account) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.github.skywalkerdarren.simpleaccounting.R
import io.github.skywalkerdarren.simpleaccounting.model.AppRepository
import io.github.skywalkerdarren.simpleaccounting.model.datasource.StatsDataSource.*
import io.github.skywalkerdarren.simpleaccounting.model.datasource.TypeDataSource.LoadTypeCallBack
import io.github.skywalkerdarren.simpleaccounting.model.entity.*
import io.github.skywalkerdarren.simpleaccounting.model.entity.Bill
import io.github.skywalkerdarren.simpleaccounting.model.entity.Type
import io.github.skywalkerdarren.simpleaccounting.util.view.FormatUtil
import org.joda.time.DateTime
import java.math.BigDecimal
Expand Down Expand Up @@ -71,48 +71,41 @@ class BillDetailViewModel(private val mRepository: AppRepository) : ViewModel()
typeName.value = t.name
balanceColor.value = if (t.isExpense) R.color.deeporange800 else R.color.lightgreen700
expensePercentHint.value = if (t.isExpense) R.string.expense_percent else R.string.income_percent
mRepository.getAccountStats(bill.accountId, start, end, object : LoadAccountStatsCallBack {
override fun onAccountStatsLoaded(accountStats: AccountStats?) {
accountStats?.let {
accountPercent.value = if (t.isExpense) getPercent(bill, it.expense) else getPercent(bill, it.income)
}
mRepository.getAccountStats(bill.accountId, start, end) { accountStats ->
accountStats?.let {
accountPercent.value = if (t.isExpense) getPercent(bill, it.expense) else getPercent(bill, it.income)
}
})
mRepository.getBillStats(start, end, object : LoadBillStatsCallBack {
override fun onBillStatsLoaded(billStats: BillStats?) {
billStats?.let {
expensePercent.value = if (t.isExpense) getPercent(bill, it.expense) else getPercent(bill, it.income)
}
}
mRepository.getBillStats(start, end) { billStats ->
billStats?.let {
expensePercent.value = if (t.isExpense) getPercent(bill, it.expense) else getPercent(bill, it.income)
}
})
}
}
}
})
mRepository.getTypeStats(start, end, bill.typeId, object : LoadTypeStatsCallBack {
override fun onTypeStatsLoaded(typeStats: TypeStats?) {
typeStats?.let {
typePercent.value = getPercent(bill, it.balance)
}
mRepository.getTypeStats(start, end, bill.typeId) { typeStats ->
typeStats?.let {
typePercent.value = getPercent(bill, it.balance)
}
})
mRepository.getTypeAverage(start, end, bill.typeId, object : LoadTypeStatsCallBack {
override fun onTypeStatsLoaded(typeStats: TypeStats?) {
typeStats?.let {
val sub = bill.balance?.subtract(it.balance)?.abs()
try {
thanAverage.value = sub?.multiply(BigDecimal.valueOf(100))
?.divide(it.balance, 2, BigDecimal.ROUND_HALF_UP).toString() + "%"
} catch (e: ArithmeticException) {
thanAverage.value = "ERROR"
}
typeAverage.value = FormatUtil.getNumeric(it.balance)
thanAverageHint.value = if (bill.balance ?: return >= it.balance) R.string.higher_than_average else R.string.less_than_average
}
mRepository.getTypeAverage(start, end, bill.typeId) { typeStats ->
typeStats?.let {
val sub = bill.balance?.subtract(it.balance)?.abs()
try {
thanAverage.value = sub?.multiply(BigDecimal.valueOf(100))
?.divide(it.balance, 2, BigDecimal.ROUND_HALF_UP).toString() + "%"
} catch (e: ArithmeticException) {
thanAverage.value = "ERROR"
}
typeAverage.value = FormatUtil.getNumeric(it.balance)
thanAverageHint.value = if (bill.balance ?: return@let >= it.balance) R.string.higher_than_average else R.string.less_than_average
}
})
balance.value = FormatUtil.getNumeric(bill.balance)
time.value = bill.date.toString("yyyy-MM-dd hh:mm")
remark.value = bill.remark

balance.value = FormatUtil.getNumeric(bill.balance)
time.value = bill.date.toString("yyyy-MM-dd hh:mm")
remark.value = bill.remark
}
}

/**
Expand Down Expand Up @@ -176,4 +169,4 @@ class BillDetailViewModel(private val mRepository: AppRepository) : ViewModel()
fun delBill(billId: UUID) {
mRepository.delBill(billId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.github.skywalkerdarren.simpleaccounting.model.AppRepository
import io.github.skywalkerdarren.simpleaccounting.model.datasource.StatsDataSource.LoadBillStatsCallBack
import io.github.skywalkerdarren.simpleaccounting.model.entity.Bill
import io.github.skywalkerdarren.simpleaccounting.model.entity.BillInfo
import io.github.skywalkerdarren.simpleaccounting.model.entity.BillStats
import io.github.skywalkerdarren.simpleaccounting.ui.activity.StatsActivity
import io.github.skywalkerdarren.simpleaccounting.util.view.FormatUtil
import org.joda.time.DateTime
Expand Down Expand Up @@ -39,16 +37,12 @@ class BillListViewModel(private val mRepository: AppRepository) : ViewModel() {
mDateTime.value = date
val month = DateTime(date.year, date.monthOfYear,
1, 0, 0)
mRepository.getBillStats(month, month.plusMonths(1), object : LoadBillStatsCallBack {
override fun onBillStatsLoaded(billStats: BillStats?) {
income.value = FormatUtil.getNumeric(billStats?.income)
}
})
mRepository.getBillStats(month, month.plusMonths(1), object : LoadBillStatsCallBack {
override fun onBillStatsLoaded(billStats: BillStats?) {
expense.value = FormatUtil.getNumeric(billStats?.expense)
}
})
mRepository.getBillStats(month, month.plusMonths(1)) {
income.value = FormatUtil.getNumeric(it?.income)
}
mRepository.getBillStats(month, month.plusMonths(1)) {
expense.value = FormatUtil.getNumeric(it?.expense)
}
mRepository.getBillInfoList(date.year, date.monthOfYear) { billInfoList.value = it }
this.month.value = month.monthOfYear.toString()
}
Expand Down
Loading

0 comments on commit bf8eefe

Please sign in to comment.