Commit c18d21a7 by pye52

1、增加Kotlin及协程库,并开始重构部分viewmodel

parent a41816e3
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.bgycc.smartcanteen"
minSdkVersion 22
targetSdkVersion 29
targetSdkVersion 30
versionCode 140
versionName "1.4.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
......@@ -103,14 +106,18 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.core:core-ktx:1.3.0"
def room_version = "2.2.5"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
def work_version = "2.3.4"
implementation "androidx.work:work-runtime:$work_version"
implementation "androidx.work:work-runtime-ktx:$work_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
......@@ -121,7 +128,7 @@ dependencies {
def okhttp_version = "4.7.2"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
def retrofit_version = "2.8.1"
def retrofit_version = '2.9.0'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version"
......
package com.bgycc.smartcanteen.executor
import kotlinx.coroutines.asCoroutineDispatcher
val coroutinesDispatcher =
SCTaskExecutor.getIOThreadExecutor().asCoroutineDispatcher()
\ No newline at end of file
......@@ -26,6 +26,11 @@ public class CommandRepository {
return dao.insertCommand(command);
}
public int finishCommand(Command command) {
command.finish();
return updateCommand(command);
}
public int updateCommand(Command command) {
return dao.updateCommand(command);
}
......
package com.bgycc.smartcanteen.viewModel
import androidx.lifecycle.*
import androidx.work.OneTimeWorkRequest
import com.bgycc.smartcanteen.command.CommandHelper
import com.bgycc.smartcanteen.entity.Command
import com.bgycc.smartcanteen.executor.coroutinesDispatcher
import com.bgycc.smartcanteen.repository.CommandRepository
import com.bgycc.smartcanteen.socket.SCWebSocketClient
import com.bgycc.smartcanteen.socket.SCWebSocketListener
import com.bgycc.smartcanteen.socket.SCWebSocketListenerAdapter
import com.bgycc.smartcanteen.utils.SmartCanteenUtils
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import kotlinx.coroutines.launch
class CommandViewModel(
private var commandRepository: CommandRepository,
private var gson: Gson,
private var deviceSN: String
) : ViewModel() {
val commandWorker = MutableLiveData<Command>()
private val dataLiveData: LiveData<List<Command>> = commandRepository.queryUndoneCommand()
private val dataObserver = Observer<List<Command>> { commands: List<Command>? ->
if (commands == null || commands.isEmpty()) {
return@Observer
}
val first = commands[0]
commandWorker.postValue(first)
}
init {
// 监听数据库的变动,并执行未完成的指令
dataLiveData.observeForever(dataObserver)
}
fun initialize() {
SCWebSocketClient.getInstance().addListener(listener)
}
override fun onCleared() {
super.onCleared()
dataLiveData.removeObserver(dataObserver)
}
fun getCommandWorker(command: Command?): OneTimeWorkRequest? {
return CommandHelper.createWorker(gson, command, deviceSN)
}
fun commandFinish(command: Command) {
viewModelScope.launch(coroutinesDispatcher) {
commandRepository.finishCommand(command)
LogUtils.d(SmartCanteenUtils.TAG, "指令执行完毕: $command")
}
}
private val listener: SCWebSocketListener = object : SCWebSocketListenerAdapter() {
override fun onMessage(
action: String,
obj: JsonObject,
original: String
) {
if (!CommandHelper.isCommand(action)) return
LogUtils.d(SmartCanteenUtils.TAG, "设备下发指令: $original")
viewModelScope.launch(coroutinesDispatcher) {
// 指令插入到数据库,则会触发dataObserver
val command = Command(original, action)
// 该action同一时间是否只能在数据库存在一条待执行的指令
if (CommandHelper.oneAtTime(command)) {
val count: Int = commandRepository.queryUndoneCommandCountByAction(action)
if (count > 0) {
LogUtils.d(SmartCanteenUtils.TAG, "该指令已在数据库中记录并未执行完毕,不再插入重复指令")
return@launch
}
}
val lastInsertId: Long = commandRepository.insertCommand(command)
if (lastInsertId == -1L) {
LogUtils.w(SmartCanteenUtils.TAG, "指令插入到数据库失败: $command")
}
}
}
}
}
\ No newline at end of file
buildscript {
ext.kotlin_version = '1.3.72'
ext {
daemon_verson_code = 11
daemon_verson_name = "1.1"
......@@ -10,6 +11,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
......
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
def signed = "Daemon.apk"
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.bgycc.smartcanteen.daemon"
minSdkVersion 22
targetSdkVersion 29
targetSdkVersion 30
versionCode rootProject.ext.daemon_verson_code
versionName rootProject.ext.daemon_verson_name
......@@ -96,9 +97,17 @@ private String findAppAssetsDir() {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.blankj:utilcodex:1.29.0'
implementation "androidx.core:core-ktx:+"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
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 sign in to comment