167 lines
5.9 KiB
Kotlin
167 lines
5.9 KiB
Kotlin
package com.shuwei.intelligent.shelves
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.os.Bundle
|
|
import android.provider.Settings
|
|
import android.util.Log
|
|
import androidx.activity.viewModels
|
|
import androidx.lifecycle.Lifecycle
|
|
import androidx.lifecycle.ViewModelProvider
|
|
import androidx.lifecycle.lifecycleScope
|
|
import androidx.lifecycle.repeatOnLifecycle
|
|
import com.shuwei.intelligent.shelves.base.BaseActivity
|
|
import com.shuwei.intelligent.shelves.databinding.ActivityInitBinding
|
|
import com.shuwei.intelligent.shelves.model.DeviceConfigInfo
|
|
import com.shuwei.intelligent.shelves.net.NetViewModel
|
|
import com.shuwei.intelligent.shelves.net.UiState
|
|
import com.shuwei.intelligent.shelves.net.UrlConfig
|
|
import com.shuwei.intelligent.shelves.utils.AppUtil
|
|
import com.shuwei.intelligent.shelves.utils.QRCodeUtil
|
|
import com.shuwei.intelligent.shelves.utils.SpTool
|
|
import com.shuwei.intelligent.shelves.utils.ext.dp
|
|
import com.shuwei.intelligent.shelves.utils.ext.invisible
|
|
import com.shuwei.intelligent.shelves.utils.ext.startActivity
|
|
import com.shuwei.intelligent.shelves.utils.ext.toJsonString
|
|
import com.shuwei.intelligent.shelves.utils.ext.toObject
|
|
import com.shuwei.intelligent.shelves.utils.ext.toast
|
|
import com.shuwei.intelligent.shelves.utils.ext.visible
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.flow.FlowCollector
|
|
import kotlinx.coroutines.flow.flow
|
|
import kotlinx.coroutines.launch
|
|
import kotlin.collections.forEach
|
|
import kotlin.getValue
|
|
import kotlin.jvm.java
|
|
import kotlin.ranges.downTo
|
|
import kotlin.text.isBlank
|
|
import kotlin.to
|
|
|
|
class InitActivity : BaseActivity() {
|
|
companion object {
|
|
const val TAG = "InitActivity"
|
|
}
|
|
|
|
private lateinit var binding: ActivityInitBinding
|
|
|
|
private val viewModel: NetViewModel by viewModels()
|
|
|
|
@SuppressLint("HardwareIds")
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
binding = ActivityInitBinding.inflate(layoutInflater)
|
|
setContentView(binding.root)
|
|
|
|
|
|
App.deviceId = AppUtil.getUDID( this)
|
|
SpTool.put(SpTool.DEVICE_ID, App.deviceId)
|
|
App.configUrl = UrlConfig.BASE_URL
|
|
App.canteenId = "0"
|
|
|
|
// var deviceId = AppUtil.getUDID(this)
|
|
// Log.d(TAG, "onCreate: deviceId=$deviceId")
|
|
//// deviceId = "39a7abdd06b3c7ab"
|
|
//// deviceId = "3ea47dc0-3cf0-3c2f-909c-265a9a65572e"
|
|
// App.deviceId = deviceId
|
|
//
|
|
// SpTool.put(SpTool.DEVICE_ID, deviceId)
|
|
// val deviceConfigCache = SpTool.getString(SpTool.DEVICE_CONFIG_CACHE)
|
|
// val checkResult = checkConfigData(deviceConfigCache)
|
|
// if (checkResult.not()) {
|
|
// binding.ivQrCode.visible()
|
|
// binding.btnInit.visible()
|
|
// //进行初始化操作
|
|
// initConfig()
|
|
// return
|
|
// }
|
|
binding.ivQrCode.invisible()
|
|
binding.btnInit.invisible()
|
|
|
|
countDown()
|
|
}
|
|
|
|
private fun countDown() {
|
|
lifecycleScope.launch {
|
|
flow {
|
|
(2 downTo 1).forEach {
|
|
delay(1000)
|
|
emit(it)
|
|
}
|
|
}.collect {
|
|
// 倒计时结束执行跳转
|
|
if (it == 1) {
|
|
startActivity<HomeActivity>()
|
|
finish()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun initConfig() {
|
|
binding.ivQrCode.setImageBitmap(
|
|
QRCodeUtil.generateQRCode(
|
|
content = App.deviceId,
|
|
size = 200.dp
|
|
)
|
|
)
|
|
binding.btnInit.setOnClickListener {
|
|
viewModel.getDeviceToken(App.deviceId)
|
|
}
|
|
lifecycleScope.launch {
|
|
repeatOnLifecycle(Lifecycle.State.CREATED) {
|
|
launch {
|
|
viewModel.getDeviceTokenUiState.collect { state ->
|
|
when (state) {
|
|
is UiState.Loading -> {}
|
|
is UiState.Success<*> -> {
|
|
state.data.data?.let { deviceToken ->
|
|
Log.d(TAG, "initConfig: $deviceToken")
|
|
viewModel.getDeviceConfig(
|
|
deviceId = App.deviceId,
|
|
deviceToken = deviceToken.toString()
|
|
)
|
|
}
|
|
}
|
|
is UiState.Error -> toast(state.msg)
|
|
else -> {}
|
|
}
|
|
}
|
|
}
|
|
launch {
|
|
viewModel.getDeviceConfigUiState.collect { state ->
|
|
when (state) {
|
|
is UiState.Loading -> {}
|
|
is UiState.Success<*> -> {
|
|
state.data.data?.let {
|
|
if (it is DeviceConfigInfo) {
|
|
SpTool.put(SpTool.DEVICE_CONFIG_CACHE, it.toJsonString())
|
|
App.configUrl = it.appPackageUrl?:""
|
|
App.canteenId = it.canteenId?:""
|
|
|
|
startActivity<HomeActivity>()
|
|
finish()
|
|
}
|
|
}
|
|
}
|
|
is UiState.Error -> toast(message = state.msg)
|
|
else -> {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun checkConfigData(data: String): Boolean {
|
|
if (data.isBlank()) {
|
|
return false
|
|
}
|
|
val config = data.toObject<DeviceConfigInfo?>()
|
|
if (config == null) {
|
|
return false
|
|
}
|
|
App.configUrl = config.appPackageUrl?:""
|
|
App.canteenId = config.canteenId?:""
|
|
return true
|
|
}
|
|
|
|
} |