纯kotlin编写的各种实用库
kotlin一些通用的工具
- RecyclerView适配器
- Activity、Fragment基类封装
- 一些常用的工具扩展类
- ViewModel相关扩展,ViewModel -> UI 事件发送
- 常用的自定义控件
Kotlin中低能耗蓝牙的使用api,支持最低版本Android4.4
Kable的纯android版[https://github.com/JuulLabs/kable]
Kotlin中使用原子操作的api
Gson的kotlin扩展
OkHttp的kotlin封装
初始化
//不是必需操作
OkFaker.setGlobalConfig(
OkConfig.Builder()
.baseUrl("https://www.baidu.com")
.client(OkHttpClient())
.build()
)
GET请求
httpCallFlow<String> {
url("/s")
queryParameters {
"wd" += "hello word"
}
}.onStart {
Log.e("OkFaker", "onStart")
}.onEach {
Log.e("OkFaker", "onEach: $it")
}.onCompletion {
Log.e("OkFaker", "onCompletion")
}.catch {
Log.e("OkFaker", "onError: $it")
}.launchIn(lifecycleScope)
POST请求
httpCallFlow<String> {
method(OkRequestMethod.POST)
url("/s")
queryParameters {
"wd" += "hello word"
}
}.onStart {
Log.e("OkFaker", "onStart")
}.onEach {
Log.e("OkFaker", "onEach: $it")
}.onCompletion {
Log.e("OkFaker", "onCompletion")
}.catch {
Log.e("OkFaker", "onError: $it")
}.launchIn(lifecycleScope)
转换成任意数据格式
httpCallFlow<String> {
method(OkRequestMethod.POST)
url("/s")
queryParameters {
"wd" += "hello word"
}
mapResponse { response ->
//转换成任意格式
JSONObject(response.body!!.string())
}
}
下载
httpCallFlow<File> {
client (
//注意 HttpLoggingInterceptor.Level < BODY
OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.build()
)
url("http://www.baidu.com")
extension(DefaultOkDownloadExtension("../path.html", true) { readBytes, totalBytes ->
//Main Thread
})
}.collect { file ->
}
SQLite的kotlin封装
object TestTable : Table("test") {
val id = LongColumn("id").primaryKey()
val name = StringColumn("name", "jack")
val age = IntColumn("age")
val flag = BooleanColumn("flag")
val number = IntColumn("number").default(10)
}
object DB : ManagedSQLiteOpenHelper(
SupportSQLiteOpenHelper.Configuration.builder(applicationContext)
.name("test_db.db")
.callback(Callback())
.build()
) {
private class Callback : SupportSQLiteOpenHelper.Callback(1) {
override fun onCreate(db: SupportSQLiteDatabase) {
//创建表
offer(TestTable).create(db.statementExecutor) {
it.id + it.name + it.age + it.flag + it.number + index(
it.id,
it.name,
name = "indexName"
)
}
}
override fun onUpgrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) {
//添加列 或者 索引
offer(TestTable).alter(db.statementExecutor) {
it.number + index(it.id, it.name)
}
}
}
}
offer(TestTable).drop()
如果要删除索引 (不支持删除列)
offer(TestTable).drop {
index(it.id, it.name).ifExists()
//or
index(name = "indexName").ifExists()
}
offer(TestTable).insert {
it.id(1) + it.name("jack") + it.age(20) + it.flag(true)
}
批量插入
offer(TestTable).batchInsert(statementExecutor, Conflict.Replace) {
for (bean in beans) {
assignments {
it.id(bean.id) + it.name(bean.name) + it.age(bean.age) +
it.flag(bean.flag) + it.number(bean.number)
}
}
}
offer(TestTable).where {
it.id eq 1
}.delete()
offer(TestTable).where {
(it.name like "%jack%") and (it.id gt 100)
}.select {
it.id + it.name + it.age + it.flag
}
//or
data class TestBean @ClassParserConstructor constructor(
val id: Long,
val name: String,
val age: Int,
val flag: Boolean
)
DB.use {
val cursor = offer(TestTable)
.where { it.flag eq true }
.groupBy { it.id + it.name }
.having { it.age gt 20 }
.orderBy { desc(it.id) }
.limit { 10 }
.offset { 10 }
.select(statementExecutor) {
it.id + it.name + it.age + it.flag
}
//Sequence<Map<String, ColumnValue>>
for (row in cursor.asMapSequence()) {
//do something
}
//直接解析为bean
for (bean in cursor.parseList(classParser<TestBean>())) {
//do something
}
}
offer(TestTable).where {
it.name eq "jack"
}.update {
it.id(100) + it.name("tom") + it.age(20) + it.flag(false)
}