Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c18d21a7
authored
Jul 08, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、增加Kotlin及协程库,并开始重构部分viewmodel
parent
a41816e3
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
124 additions
and
8 deletions
+124
-8
app/build.gradle
+12
-5
app/src/main/java/com/bgycc/smartcanteen/executor/DefaultTaskExecutorKt.kt
+7
-0
app/src/main/java/com/bgycc/smartcanteen/repository/CommandRepository.java
+5
-0
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.kt
+86
-0
build.gradle
+2
-0
daemon/build.gradle
+12
-3
No files found.
app/build.gradle
View file @
c18d21a7
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"
...
...
app/src/main/java/com/bgycc/smartcanteen/executor/DefaultTaskExecutorKt.kt
0 → 100644
View file @
c18d21a7
package
com.bgycc.smartcanteen.executor
import
kotlinx.coroutines.asCoroutineDispatcher
val
coroutinesDispatcher
=
SCTaskExecutor
.
getIOThreadExecutor
().
asCoroutineDispatcher
()
\ No newline at end of file
app/src/main/java/com/bgycc/smartcanteen/repository/CommandRepository.java
View file @
c18d21a7
...
...
@@ -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
);
}
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/CommandViewModel.kt
0 → 100644
View file @
c18d21a7
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
build.gradle
View file @
c18d21a7
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"
}
}
...
...
daemon/build.gradle
View file @
c18d21a7
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
()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment