-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFlutterCollectService.kt
More file actions
133 lines (112 loc) · 3.77 KB
/
FlutterCollectService.kt
File metadata and controls
133 lines (112 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package note.jdbc
import cn.hutool.core.date.DateUtil
import cn.hutool.db.Entity
import cn.hutool.db.handler.EntityListHandler
import cn.hutool.db.sql.SqlExecutor
import shop.itbug.fluttercheckversionx.bus.FlutterPluginCollectEvent
import shop.itbug.fluttercheckversionx.bus.FlutterPluginCollectEventType
val defaultAddToCollect = listOf("cupertino_icons", "dd_js_util", "provider", "riverpod", "freezed")
object FlutterCollectService {
/**
* 操作前先检查表是否存在,不存在先创建表再进行下一步操作
*/
private fun check(call: () -> Unit) {
///表不存在,先创建
if (!hasTable()) {
val success = SqliteConnectManager.createFlutterPluginTable()
if (success.not()) {
println("警告⚠️: 表创建失败了.!请检查sql语句是否正确")
return
} else {
///设置默认收藏
defaultAddToCollect.forEach { add(it) }
}
}
call.invoke()
}
/**
* 判断插件收藏表是否存在
* true : 存在
*/
private fun hasTable(): Boolean {
return SqliteConnectManager.isExits(SqliteConnectManager.FlutterPluginTableName)
}
///添加收藏
fun add(pluginName: String): Boolean {
var success = false
check {
success = try {
val result = SqlExecutor.execute(
SqliteConnectManager.connect, """
insert into ${SqliteConnectManager.FlutterPluginTableName} (name,time) values ('${pluginName}','${DateUtil.now()}')
""".trimIndent()
)
result >= 1
} catch (e: Exception) {
println("警告: ⚠️添加收藏失败:$e")
false
}
}
if(success){
FlutterPluginCollectEvent.fire(FlutterPluginCollectEventType.add,pluginName)
}
return success
}
/**
* 判断是否存在
* @return true : 已存在, false: 不存在
*/
fun exits(pluginName: String): Boolean {
var success = false
check {
success = try {
val sql = """
select name from ${SqliteConnectManager.FlutterPluginTableName} where name=?;
""".trimIndent()
val query = SqlExecutor.query(SqliteConnectManager.connect, sql, EntityListHandler(), pluginName)
query.isNotEmpty()
} catch (e: Exception) {
false
}
}
return success
}
/**
* 删除
* @return true : 删除成功
*/
fun remove(pluginName: String): Boolean {
var success = false
check {
success = try {
val sql = """
delete from ${SqliteConnectManager.FlutterPluginTableName} where name='$pluginName'
""".trimIndent()
val execute = SqlExecutor.execute(SqliteConnectManager.connect, sql)
println("删除结果:$execute")
execute > 0
} catch (e: Exception) {
println("警告: ⚠️删除失败:$e")
false
}
}
if(success){
FlutterPluginCollectEvent.fire(FlutterPluginCollectEventType.remove,pluginName)
}
return success
}
/**
* 查询所有已经收藏的插件列表
*/
fun selectAll(): List<Entity> {
var list = emptyList<Entity>()
check {
val sql = """
select * from ${SqliteConnectManager.FlutterPluginTableName}
""".trimIndent()
val query = SqlExecutor.query(SqliteConnectManager.connect, sql, EntityListHandler())
list = query.toList()
}
return list
}
}