|
@@ -9,7 +9,6 @@ import androidx.activity.viewModels
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
import androidx.databinding.DataBindingUtil
|
|
|
-import androidx.fragment.app.Fragment
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
@@ -23,19 +22,28 @@ import com.doverfuelingsolutions.issp.fusion.callback.OnFusionStatus
|
|
|
import com.doverfuelingsolutions.issp.utils.DFSToastUtil
|
|
|
import com.doverfuelingsolutions.issp.utils.WindowUtil
|
|
|
import com.doverfuelingsolutions.issp.utils.log.DFSLog
|
|
|
+import com.doverfuelingsolutions.issp.utils.sp.SPUtil
|
|
|
import com.doverfuelingsolutions.issp.view.fragment.FragmentReconnect
|
|
|
+import com.doverfuelingsolutions.issp.view.fragment.router.FragmentRouter
|
|
|
import com.doverfuelingsolutions.issp.view.fragment.FragmentSelect
|
|
|
+import com.doverfuelingsolutions.issp.view.fragment.router.OnRouteChangeListener
|
|
|
import com.doverfuelingsolutions.issp.view.util.LoadingDialogBuilder
|
|
|
+import com.doverfuelingsolutions.issp.view.util.LoginTokenRefresher
|
|
|
import com.wayne.www.waynelib.fdc.FdcClient
|
|
|
import com.youth.banner.adapter.BannerImageAdapter
|
|
|
import com.youth.banner.holder.BannerImageHolder
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.GlobalScope
|
|
|
+import kotlinx.coroutines.async
|
|
|
import kotlinx.coroutines.launch
|
|
|
import java.io.File
|
|
|
import java.util.*
|
|
|
|
|
|
class MainActivity : AppCompatActivity(),
|
|
|
View.OnClickListener,
|
|
|
- OnFusionStatus {
|
|
|
+ View.OnLongClickListener,
|
|
|
+ OnFusionStatus,
|
|
|
+ OnRouteChangeListener {
|
|
|
|
|
|
companion object {
|
|
|
fun start(context: Context) {
|
|
@@ -44,22 +52,23 @@ class MainActivity : AppCompatActivity(),
|
|
|
}
|
|
|
|
|
|
private val binding: ActivityMainBinding by lazy {
|
|
|
- DataBindingUtil.setContentView(
|
|
|
- this,
|
|
|
- R.layout.activity_main
|
|
|
- )
|
|
|
+ DataBindingUtil.setContentView(this, R.layout.activity_main)
|
|
|
}
|
|
|
private val mainViewModel: MainViewModel by viewModels()
|
|
|
+ val fragmentRouter: FragmentRouter by lazy { FragmentRouter(this, R.id.fragmentBox) }
|
|
|
|
|
|
private var dialogFusionLinking: AlertDialog? = null
|
|
|
-
|
|
|
private var isBlockBackPress = false
|
|
|
|
|
|
+ private val mLoginTokenRefresher = LoginTokenRefresher()
|
|
|
+
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
binding.lifecycleOwner = this
|
|
|
binding.mainViewModel = mainViewModel
|
|
|
binding.handler = this
|
|
|
+ binding.clock.setOnLongClickListener(this)
|
|
|
+
|
|
|
|
|
|
supportActionBar?.hide()
|
|
|
initView()
|
|
@@ -67,6 +76,10 @@ class MainActivity : AppCompatActivity(),
|
|
|
|
|
|
FusionManager.onFusionStatus = this
|
|
|
lifecycle.addObserver(FusionManager)
|
|
|
+
|
|
|
+ fragmentRouter.setOnRouteChangeListener(this)
|
|
|
+
|
|
|
+ mLoginTokenRefresher.start(this)
|
|
|
}
|
|
|
|
|
|
override fun onResume() {
|
|
@@ -81,6 +94,7 @@ class MainActivity : AppCompatActivity(),
|
|
|
super.onDestroy()
|
|
|
|
|
|
FusionManager.onFusionStatus = null
|
|
|
+ mLoginTokenRefresher.stop()
|
|
|
}
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
@@ -145,6 +159,16 @@ class MainActivity : AppCompatActivity(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ override fun onLongClick(v: View?): Boolean {
|
|
|
+ return when (v) {
|
|
|
+ binding.clock -> {
|
|
|
+ PreferenceActivity.startForResult(this)
|
|
|
+ true
|
|
|
+ }
|
|
|
+ else -> false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
override fun onFusionStatus(status: FusionStatus) {
|
|
|
DFSLog.i("Fusion output status: ${status.name.toLowerCase(Locale.CHINESE)}")
|
|
|
lifecycleScope.launch {
|
|
@@ -154,15 +178,23 @@ class MainActivity : AppCompatActivity(),
|
|
|
setFusionLinkingLoading()
|
|
|
}
|
|
|
FusionStatus.Connected -> {
|
|
|
+ // TODO 直接油枪模式
|
|
|
DFSToastUtil.success(R.string.connect_fusion_success)
|
|
|
- setFragment(FragmentSelect())
|
|
|
+ fragmentRouter.push(FragmentSelect())
|
|
|
+ }
|
|
|
+ FusionStatus.ConnectedWithoutInfo -> {
|
|
|
+ DFSToastUtil.fail(R.string.get_fuel_failed)
|
|
|
+ fragmentRouter.push(FragmentReconnect.build {
|
|
|
+ DFSToastUtil.success(R.string.get_fuel_success)
|
|
|
+ fragmentRouter.push(FragmentSelect())
|
|
|
+ })
|
|
|
}
|
|
|
FusionStatus.Disconnected -> {
|
|
|
// Disconnected reason
|
|
|
// 1. Network problem.
|
|
|
// 2. Communication exception and failed retry.
|
|
|
DFSToastUtil.fail(R.string.disconnect_fusion)
|
|
|
- setFragment(FragmentReconnect.build {
|
|
|
+ fragmentRouter.push(FragmentReconnect.build {
|
|
|
setFusionLinkingLoading()
|
|
|
})
|
|
|
}
|
|
@@ -170,6 +202,21 @@ class MainActivity : AppCompatActivity(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ override fun onRouteForward() {
|
|
|
+ mLoginTokenRefresher.stop()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onRouteBackward(isBottom: Boolean) {
|
|
|
+ if (isBottom) {
|
|
|
+ mLoginTokenRefresher.start(this)
|
|
|
+ handleUnsolvedOrder()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun blockBackPress(isBlock: Boolean) {
|
|
|
+ isBlockBackPress = isBlock
|
|
|
+ }
|
|
|
+
|
|
|
private fun initView() {
|
|
|
mainViewModel.stationName.value = GlobalData.businessName.get()
|
|
|
if (GlobalData.stationLogoFileName.get().isNotEmpty()) {
|
|
@@ -209,18 +256,66 @@ class MainActivity : AppCompatActivity(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fun setFragment(fragment: Fragment, back: Boolean = false) {
|
|
|
- supportFragmentManager.beginTransaction().run {
|
|
|
- replace(R.id.fragmentBox, fragment)
|
|
|
- if (back) {
|
|
|
- addToBackStack(fragment::class.java.simpleName)
|
|
|
+ // TODO 当本地未处理订单无法按预期处理时(如本地锁的单,但在服务器上被其他渠道解锁了,造成解锁失败;消单同理;)
|
|
|
+ private fun handleUnsolvedOrder() {
|
|
|
+ GlobalScope.launch(Dispatchers.IO) {
|
|
|
+ val lockList = SPUtil.getLockOrderList()
|
|
|
+ val unclearedList = SPUtil.getUnclearedOrderList()
|
|
|
+ // 解锁订单
|
|
|
+ lockList.let { list ->
|
|
|
+ if (list.isEmpty()) return@let
|
|
|
+
|
|
|
+ val deferredList = list.map { item ->
|
|
|
+ val info = item.split("-")
|
|
|
+ val pumpId = info[0].toInt()
|
|
|
+ val transactionNo = info[1]
|
|
|
+ val token = info[2].toInt()
|
|
|
+ async {
|
|
|
+ FusionManager.lockOrder(pumpId, transactionNo, token, false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val resultList = deferredList.map { item -> item.await() }
|
|
|
+ val remainLockList = resultList.mapIndexed { index, result ->
|
|
|
+ val tip = "auto unlock order ${list[index]}"
|
|
|
+ if (result.success) {
|
|
|
+ DFSLog.d("$tip succeeded")
|
|
|
+ null
|
|
|
+ } else {
|
|
|
+ DFSLog.d("$tip failed")
|
|
|
+ list[index]
|
|
|
+ }
|
|
|
+ }.filterNotNull()
|
|
|
+ SPUtil.replaceLockOrderList(remainLockList)
|
|
|
}
|
|
|
- commit()
|
|
|
- }
|
|
|
- }
|
|
|
+ // 消除订单
|
|
|
+ unclearedList.let { list ->
|
|
|
+ if (list.isEmpty()) return@let
|
|
|
|
|
|
- fun blockBackPress(isBlock: Boolean) {
|
|
|
- isBlockBackPress = isBlock
|
|
|
+ val deferredList = list.map { item ->
|
|
|
+ val info = item.split("-")
|
|
|
+ val pumpId = info[0].toInt()
|
|
|
+ val transactionNo = info[1]
|
|
|
+ val token = info[2].toInt()
|
|
|
+ async {
|
|
|
+ FusionManager.clearOrder(pumpId, transactionNo, token)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val resultList = deferredList.map { item -> item.await() }
|
|
|
+ val remainLockList = resultList.mapIndexed { index, result ->
|
|
|
+ val tip = "auto clear order ${list[index]}"
|
|
|
+ if (result.success) {
|
|
|
+ DFSLog.d("$tip succeeded")
|
|
|
+ null
|
|
|
+ } else {
|
|
|
+ DFSLog.d("$tip failed")
|
|
|
+ list[index]
|
|
|
+ }
|
|
|
+ }.filterNotNull()
|
|
|
+ SPUtil.replaceUnclearedOrderList(remainLockList)
|
|
|
+ }
|
|
|
+ // 删除过期云订单
|
|
|
+ SPUtil.removeExpiredWayneOrder()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
class MainViewModel : ViewModel() {
|