Commit fb22f59f authored by zhaopengxiang's avatar zhaopengxiang

优化本地关键数据存储

parent ccb1d111
...@@ -20,6 +20,7 @@ import com.miya.fastcashier.utils.CenterToasty ...@@ -20,6 +20,7 @@ import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.DateSelectHelper import com.miya.fastcashier.utils.DateSelectHelper
import com.miya.fastcashier.utils.clickWithTrigger import com.miya.fastcashier.utils.clickWithTrigger
import com.miya.fastcashier.utils.manage.AccountPasswordManageKit import com.miya.fastcashier.utils.manage.AccountPasswordManageKit
import com.miya.fastcashier.utils.manage.LocalKeyDataMKManageKit
import com.miya.fastcashier.utils.manage.OrderRecordManageKit import com.miya.fastcashier.utils.manage.OrderRecordManageKit
import com.miya.fastcashier.viewmodel.MainViewModel import com.miya.fastcashier.viewmodel.MainViewModel
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
...@@ -83,7 +84,7 @@ class MainActivity : BaseActivity() { ...@@ -83,7 +84,7 @@ class MainActivity : BaseActivity() {
binding.llBalance.clickWithTrigger { binding.llBalance.clickWithTrigger {
//上次记录的时间,或者当天0点 //上次记录的时间,或者当天0点
val timeMillis = MMKV.defaultMMKV().getLong("balanceDate", 0L) val timeMillis = LocalKeyDataMKManageKit.getBalanceDate()
if (timeMillis > 0L && timeMillis <= System.currentTimeMillis()) { if (timeMillis > 0L && timeMillis <= System.currentTimeMillis()) {
viewModel.orderStatistics(Date(timeMillis), Date(), true) viewModel.orderStatistics(Date(timeMillis), Date(), true)
} else { } else {
...@@ -131,7 +132,7 @@ class MainActivity : BaseActivity() { ...@@ -131,7 +132,7 @@ class MainActivity : BaseActivity() {
viewModel.balanceLiveData.observe(this, { result -> viewModel.balanceLiveData.observe(this, { result ->
result.onSuccess { result.onSuccess {
MMKV.defaultMMKV().putLong("balanceDate", it) LocalKeyDataMKManageKit.putBalanceDate(it)
} }
result.onFailure { e -> result.onFailure { e ->
CenterToasty.error(this@MainActivity, e.message ?: "").show() CenterToasty.error(this@MainActivity, e.message ?: "").show()
......
...@@ -26,6 +26,7 @@ import com.miya.fastcashier.databinding.ItemSearchOrderListBinding ...@@ -26,6 +26,7 @@ import com.miya.fastcashier.databinding.ItemSearchOrderListBinding
import com.miya.fastcashier.service.AccountService import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.service.PrintService import com.miya.fastcashier.service.PrintService
import com.miya.fastcashier.utils.* import com.miya.fastcashier.utils.*
import com.miya.fastcashier.utils.manage.LocalKeyDataMKManageKit
import com.miya.fastcashier.viewmodel.SearchOrderViewModel import com.miya.fastcashier.viewmodel.SearchOrderViewModel
import com.miya.fastcashier.widget.Divider import com.miya.fastcashier.widget.Divider
import com.tencent.mmkv.MMKV import com.tencent.mmkv.MMKV
...@@ -95,7 +96,7 @@ class SearchOrderFragment(val payType: MiYaPayPlantformPayWayEnum.MiyaPayType) : ...@@ -95,7 +96,7 @@ class SearchOrderFragment(val payType: MiYaPayPlantformPayWayEnum.MiyaPayType) :
} }
isEnd = false isEnd = false
// onLoading = false // onLoading = false
balanceDate = MMKV.defaultMMKV().getLong("balanceDate", 0L) balanceDate = LocalKeyDataMKManageKit.getBalanceDate()
val act = requireActivity() val act = requireActivity()
binding?.run { binding?.run {
adapter = SearchOrderListAdapter(act, arrayListOf()) adapter = SearchOrderListAdapter(act, arrayListOf())
......
package com.miya.fastcashier.utils; package com.miya.fastcashier.utils;
import com.blankj.utilcode.util.StringUtils; import com.blankj.utilcode.util.StringUtils;
import java.text.ParseException; import java.text.ParseException;
...@@ -153,6 +152,20 @@ public class DateUtils { ...@@ -153,6 +152,20 @@ public class DateUtils {
return today; return today;
} }
/**
* 获取某天的n天以前的0点
*/
public static long getDaysAgo(Date time, int daysAgoNum) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.add(calendar.DATE, -daysAgoNum);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime().getTime();
}
/** /**
* 得到前一天 * 得到前一天
*/ */
...@@ -438,7 +451,7 @@ public class DateUtils { ...@@ -438,7 +451,7 @@ public class DateUtils {
try { try {
SimpleDateFormat format = new SimpleDateFormat("HH:mm"); SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date now = format.parse(format(new Date(),DF_HHMM)); Date now = format.parse(format(new Date(), DF_HHMM));
Date start = format.parse(startDate); Date start = format.parse(startDate);
Date end = format.parse(endDate); Date end = format.parse(endDate);
long nowTime = now.getTime(); long nowTime = now.getTime();
......
package com.miya.fastcashier.utils.manage
import com.blankj.utilcode.util.LogUtils
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.utils.DateUtils
import com.tencent.mmkv.MMKV
import java.util.*
/**
* 类描述:本地关键数据MMKV统一管理
* 概述:涉及到的关键数据:balanceDate(结算时间)
* 创建人:zpxiang
* 创建时间:2022/4/24
* 修改人:
* 修改时间:
*/
class LocalKeyDataMKManageKit {
companion object {
private val MMKV_ID = "fastcashier_key_data"
private val BALANCEDATE_ID = "balanceDate"
/**
* 存储结算时间
*/
fun putBalanceDate(time: Long) {
getKeyDataMK().putLong(BALANCEDATE_ID, time)
LogUtils.e(BALANCEDATE_ID, "putBalanceDate${getKeyDataMK().getLong(BALANCEDATE_ID, 0)}")
}
/**
* 获取结算时间
*/
fun getBalanceDate(): Long {
LogUtils.e(BALANCEDATE_ID, "getBalanceDate${getKeyDataMK().getLong(BALANCEDATE_ID, 0)}")
return getKeyDataMK().getLong(BALANCEDATE_ID, 0)
}
/**
* 结算时间合规化处理,适用于登录时
* 策略:
* ①当获取的结算时间=0(证明本地未存储过,第一次登陆)存储为当前时间
* ②当获取的结算时间小于三天前的当前时间,则将结算时间重置为两天前的零点
*/
fun handleCompliantBalanceDate() {
LogUtils.e(BALANCEDATE_ID, "handleCompliantBalanceDate")
var balanceTime = getBalanceDate()
if (balanceTime <= 0L) {
//todo 如果返回有服务器时间建议改成服务器时间
putBalanceDate(System.currentTimeMillis())
return
}
var minimumTime: Long = System.currentTimeMillis() - 3 * 24 * 3600 * 1000
if (balanceTime <= minimumTime){
val daysAgoMillions = DateUtils.getDaysAgo(Date(), 2)
putBalanceDate(daysAgoMillions)
LogUtils.e(BALANCEDATE_ID, "daysAgoMillions:${DateUtils.format18(Date(daysAgoMillions))}")
}
}
/**
* 清除关键数据
*/
fun clear() {
getKeyDataMK().clearAll()
}
private fun getKeyDataMK(): MMKV {
val MMKV_ID_WITH_USER = MMKV_ID.plus("_${AccountService.getUserName()}")
return MMKV.mmkvWithID(MMKV_ID_WITH_USER)
}
}
}
\ No newline at end of file
package com.miya.fastcashier.viewmodel package com.miya.fastcashier.viewmodel
import android.util.Patterns
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.miya.fastcashier.R import com.miya.fastcashier.R
import com.miya.fastcashier.beans.LoginFormState
import com.miya.fastcashier.beans.LoginRequest import com.miya.fastcashier.beans.LoginRequest
import com.miya.fastcashier.beans.SelfCashierAccountInfo import com.miya.fastcashier.beans.SelfCashierAccountInfo
import com.miya.fastcashier.net.BaseResult import com.miya.fastcashier.net.BaseResult
import com.miya.fastcashier.service.LoginService import com.miya.fastcashier.service.LoginService
import com.miya.fastcashier.beans.LoginFormState import com.miya.fastcashier.utils.manage.LocalKeyDataMKManageKit
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.lang.RuntimeException
class LoginViewModel : ViewModel() { class LoginViewModel : ViewModel() {
...@@ -30,14 +28,14 @@ class LoginViewModel : ViewModel() { ...@@ -30,14 +28,14 @@ class LoginViewModel : ViewModel() {
errorHandle(LoginService.getApi().login(LoginRequest(userName, passWord, ""))) errorHandle(LoginService.getApi().login(LoginRequest(userName, passWord, "")))
) )
//MMKV记录结算时间 // val balanceDate = LocalKeyDataMKManageKit.getBalanceDate()
val balanceDate = // if (balanceDate <= 0L) {
MMKV.defaultMMKV().getLong("balanceDate", 0L) // //todo 改成服务器时间
if (balanceDate <= 0L) { // LocalKeyDataMKManageKit.putBalanceDate(System.currentTimeMillis())
//todo 改成服务器时间 // }
MMKV.defaultMMKV().putLong("balanceDate", System.currentTimeMillis())
}
loginLiveData.postValue(result) loginLiveData.postValue(result)
//根据本地存储时间,MMKV初始化合理的结算时间
LocalKeyDataMKManageKit.handleCompliantBalanceDate()
} catch (e: Exception) { } catch (e: Exception) {
loginLiveData.postValue(Result.failure(e)) loginLiveData.postValue(Result.failure(e))
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment