Skip to content

创建一个APP

storytellerF edited this page Nov 5, 2023 · 2 revisions

build.gradle.kts

只需要导入com.storyteller_f.version_manager, 无需手动添加依赖,设置JDK Level,编译参数等信息。

import common_ui_list_structure_preset.*

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("kotlin-android")
    id("kotlin-parcelize")
    id("com.storyteller_f.version_manager")
}

android {
    defaultConfig {
        applicationId = "com.storyteller_f.**"
    }
    namespace = "com.storyteller_f.**"
}

dependencies {
    //根据需要添加
    networkDependency()
    fileSystemDependency()
    workerDependency()
}
//必须添加
baseApp()
//推荐添加
setupGeneric()
setupDataBinding()
setupDipToPx()

定义数据结构

@Entity(tableName = "repos")
data class Repo(
    @PrimaryKey @field:SerializedName("id") val id: Long,
    @field:SerializedName("name") val name: String,
) : Datum<RepoRemoteKey> {
    override fun commonDatumId() = id.toString()
    override fun produceRemoteKey(prevKey: Int?, nextKey: Int?) =
        RepoRemoteKey(commonDatumId(), prevKey, nextKey)

    override fun remoteKeyId(): String = commonDatumId()
}

@Entity(tableName = "repo_remote_keys")
class RepoRemoteKey(
    itemId: String,
    prevKey: Int?,
    nextKey: Int?
) : RemoteKey(itemId, prevKey, nextKey)

如果是本地的数据,不必继承自 Datum,可以直接继承自 Model。Model 不包含remoteKey。

使用数据

//添加一个Adapter
val adapter = SimpleSourceAdapter<FileItemHolder, FileViewHolder>()
//绑定数据到RecycleView
listWithState.sourceUp(adapter, owner, session.selected, flash = ListWithState.Companion::remote)

//定义ViewHolder
class FileItemHolder(
    val file: FileModel,
    val selected: MutableLiveData<MutableList<Pair<DataItemHolder, Int>>>
) : DataItemHolder {
    override fun areItemsTheSame(other: DataItemHolder): Booloan

    override fun areContentsTheSame(other: DataItemHolder): Boolean 
}
@BindItemHolder(FileItemHolder::class)
class FileViewHolder(private val binding: ViewHolderFileBinding) : AdapterViewHolder<FileItemHolder>(binding) {
    override fun bindData(itemHolder: FileItemHolder) = binding.fileName.text = itemHolder.file.name
}

sourceUp 最后传入的ListWithState.companion.remote 是一个函数,作用是根据ViewModel 的加载状态返回UIState

UIState 用于标识需要显示UI 上的那一部分的View,比如“加载中”,“加载错误”,“加载完成”。

Clone this wiki locally