当前位置: 首页 > news >正文

封面型网站怎么做的网站制作工具

封面型网站怎么做的,网站制作工具,政府网站开发需求报告,网站动画是怎么做的问题描述 当我编写quickupload库时,因为需要在 Service中进行上传任务,向Service传递时我发现需要传递的数据很多并且结构复杂,如果处理不好就会导致以下几个问题 耗时: 需要更多时间进行开发和测试以确保正确的数据处理。容易出错: 由于手…

问题描述

当我编写quickupload库时,因为需要在 Service中进行上传任务,向Service传递时我发现需要传递的数据很多并且结构复杂,如果处理不好就会导致以下几个问题

  • 耗时: 需要更多时间进行开发和测试以确保正确的数据处理。
  • 容易出错: 由于手动序列化和反序列化逻辑,出现错误的风险更高。
  • 维护: 维护和更新代码的工作量增加,尤其是数据结构发生变化时。

解决方案:

为了解决这个问题,需要编写 PersistableData

open class PersistableData() : Parcelable {protected val data = HashMap<String, Any>()override fun equals(other: Any?): Boolean {if (other == null || other !is PersistableData) return falsereturn data == other.data}override fun hashCode() = data.hashCode()@SuppressLint("ParcelClassLoader")private constructor(parcel: Parcel) : this() {parcel.readBundle()?.let { bundle ->bundle.keySet().forEach { key ->when (val value = bundle[key]) {is Boolean, is Double, is Int, is Long, is String -> data[key] = value}}}}override fun describeContents() = 0override fun writeToParcel(dest: Parcel, flags: Int) {toBundle().writeToParcel(dest, flags)}companion object CREATOR : Parcelable.Creator<PersistableData> {private const val separator = "$"override fun createFromParcel(parcel: Parcel) = PersistableData(parcel)override fun newArray(size: Int): Array<PersistableData?> = arrayOfNulls(size)/*** 从PersistableData JSON表示创建 [PersistableData]。*/@JvmStaticfun fromJson(rawJsonString: String): PersistableData {val json = JSONObject(rawJsonString)val data = PersistableData()json.keys().forEach { key ->when (val value = json.get(key)) {is Boolean, is Double, is Int, is Long, is String -> data.data[key] = value}}return data}}private fun String.validated(checkExists: Boolean = false): String {if (contains(separator))throw IllegalArgumentException("key cannot contain $separator as it's a reserved character, used for nested data")if (checkExists && !data.containsKey(this))throw IllegalArgumentException("no data found for key \"$this\"")return this}fun putBoolean(key: String, value: Boolean) {data[key.validated()] = value}fun getBoolean(key: String) = data[key.validated(checkExists = true)] as Booleanfun putDouble(key: String, value: Double) {data[key.validated()] = value}fun getDouble(key: String) = data[key.validated(checkExists = true)] as Doublefun putInt(key: String, value: Int) {data[key.validated()] = value}fun getInt(key: String) = data[key.validated(checkExists = true)] as Intfun putLong(key: String, value: Long) {data[key.validated()] = value}fun getLong(key: String) = data[key.validated(checkExists = true)] as Longfun putString(key: String, value: String) {data[key.validated()] = value}fun getString(key: String) = data[key.validated(checkExists = true)] as Stringfun putData(key: String, data: PersistableData) {data.data.forEach { (dataKey, value) ->this.data["$key$separator$dataKey"] = value}}fun getData(key: String): PersistableData {val entries = data.entries.filter { it.key.startsWith("$key$separator") }if (entries.isEmpty()) return PersistableData()return PersistableData().also { extractedData ->entries.forEach { (entryKey, entryValue) ->extractedData.data[entryKey.removePrefix("$key$separator")] = entryValue}}}fun putArrayData(key: String, data: List<PersistableData>) {data.forEachIndexed { index, persistableData ->persistableData.data.forEach { (dataKey, value) ->this.data["$key$separator$index$separator$dataKey"] = value}}}fun getArrayData(key: String): List<PersistableData> {val entries = ArrayList(data.entries.filter { it.key.startsWith("$key$separator") })if (entries.isEmpty()) return emptyList()var index = 0var elements = entries.filter { it.key.startsWith("$key$separator$index$separator") }val outList = ArrayList<PersistableData>()while (elements.isNotEmpty()) {outList.add(PersistableData().also { extractedData ->elements.forEach { (entryKey, entryValue) ->extractedData.data[entryKey.removePrefix("$key$separator$index$separator")] =entryValue}entries.removeAll(elements)})index += 1elements = entries.filter { it.key.startsWith("$key$separator$index$separator") }}return outList}/*** 创建一个新的包,其中包含此 [PersistableData] 中存在的所有字段。*/fun toBundle() = Bundle().also { bundle ->data.keys.forEach { key ->when (val value = data[key]) {is Boolean -> bundle.putBoolean(key, value)is Double -> bundle.putDouble(key, value)is Int -> bundle.putInt(key, value)is Long -> bundle.putLong(key, value)is String -> bundle.putString(key, value)}}}/*** 创建一个包含所有字段的JSON字符串表示* 在此 [PersistableData] 中。** 这并不意味着人类可读,而是一种方便的方式来传递复杂的* 使用字符串的结构化数据。*/fun toJson() = JSONObject().also { json ->data.keys.forEach { key ->when (val value = data[key]) {is Boolean, is Double, is Int, is Long, is String -> json.put(key, value)}}}.toString()
}

简单总结一下:

  • 存储各种类型的键值对(Boolean、Double、Int、Long、String)。
  • 提供放入和获取数据的方法(putBoolean、getBoolean等)。
  • 使用带有分隔符的键支持嵌套和数组数据结构。
    转换数据为Bundle和JSON格式,便于存储和检索。
  • 实现Parcelable接口,允许在Android组件之间传递数据。
为了在使用时保持统一性

编写接口 Persistable

interface Persistable {fun toPersistableData(): PersistableDatainterface Creator<T> {fun createFromPersistableData(data: PersistableData): T}
}

简单总结一下:

  • 定义了一个可以转换为PersistableData对象的契约。
  • 确保数据对象的序列化和反序列化方式一致。

当需要对某个数据类进行序列化时只需要实现接口 Persistable,例如 UploadFile数据类

@Parcelize
data class UploadFile @JvmOverloads constructor(val path: String,val properties: LinkedHashMap<String, String> = LinkedHashMap()
) : Parcelable, Persistable {companion object : Persistable.Creator<UploadFile> {private object CodingKeys {const val path = "path"const val properties = "props"}override fun createFromPersistableData(data: PersistableData) = UploadFile(path = data.getString(CodingKeys.path),properties = LinkedHashMap<String, String>().apply {val bundle = data.getData(CodingKeys.properties).toBundle()bundle.keySet().forEach { propKey ->put(propKey, bundle.getString(propKey)!!)}})}override fun toPersistableData() = PersistableData().apply {putString(CodingKeys.path, path)putData(CodingKeys.properties, PersistableData().apply {properties.entries.forEach { (propKey, propVal) ->putString(propKey, propVal)}})}
}

简单总结一下:

  • 使用PersistableData存储其属性,使得状态的保存和恢复变得简单。
  • 包含一个自定义Creator,用于从PersistableData创建UploadFile实例。

总结

我认为这样做 可以简化和优化在Android应用中管理复杂数据结构及其持久化的过程,如果对你有帮助记得点赞收藏!


文章转载自:
http://bacteroidal.xkzr.cn
http://ligamental.xkzr.cn
http://salus.xkzr.cn
http://recordak.xkzr.cn
http://deliverer.xkzr.cn
http://leeward.xkzr.cn
http://ganglike.xkzr.cn
http://electrocute.xkzr.cn
http://inculpation.xkzr.cn
http://aliment.xkzr.cn
http://fossula.xkzr.cn
http://micrology.xkzr.cn
http://flux.xkzr.cn
http://artist.xkzr.cn
http://acataleptic.xkzr.cn
http://rigescent.xkzr.cn
http://morphogen.xkzr.cn
http://dacron.xkzr.cn
http://underway.xkzr.cn
http://burble.xkzr.cn
http://inaptly.xkzr.cn
http://hopbine.xkzr.cn
http://maksoorah.xkzr.cn
http://elicitation.xkzr.cn
http://ammonification.xkzr.cn
http://sudamina.xkzr.cn
http://starriness.xkzr.cn
http://eclair.xkzr.cn
http://corroborator.xkzr.cn
http://hydrometric.xkzr.cn
http://featherlet.xkzr.cn
http://disinter.xkzr.cn
http://incontestably.xkzr.cn
http://epitoxoid.xkzr.cn
http://biramose.xkzr.cn
http://masterless.xkzr.cn
http://miscegenation.xkzr.cn
http://scalariform.xkzr.cn
http://striae.xkzr.cn
http://scrooch.xkzr.cn
http://belong.xkzr.cn
http://coppering.xkzr.cn
http://provocation.xkzr.cn
http://spag.xkzr.cn
http://biographic.xkzr.cn
http://bacteriophobia.xkzr.cn
http://single.xkzr.cn
http://dilaceration.xkzr.cn
http://east.xkzr.cn
http://chestertonian.xkzr.cn
http://settlement.xkzr.cn
http://lill.xkzr.cn
http://pennsylvania.xkzr.cn
http://bandy.xkzr.cn
http://loophole.xkzr.cn
http://virogene.xkzr.cn
http://hjs.xkzr.cn
http://famacide.xkzr.cn
http://farcically.xkzr.cn
http://vii.xkzr.cn
http://acerbity.xkzr.cn
http://thorpe.xkzr.cn
http://unfertile.xkzr.cn
http://hematozoal.xkzr.cn
http://dall.xkzr.cn
http://mercapto.xkzr.cn
http://simultaneously.xkzr.cn
http://inextirpable.xkzr.cn
http://unenjoyable.xkzr.cn
http://circean.xkzr.cn
http://annelid.xkzr.cn
http://rhabdom.xkzr.cn
http://incalculable.xkzr.cn
http://finicky.xkzr.cn
http://hein.xkzr.cn
http://octyl.xkzr.cn
http://scrivello.xkzr.cn
http://precautionary.xkzr.cn
http://hemicycle.xkzr.cn
http://bejewlled.xkzr.cn
http://dindle.xkzr.cn
http://claimer.xkzr.cn
http://toxemic.xkzr.cn
http://hydroxylamine.xkzr.cn
http://receivership.xkzr.cn
http://unlanguaged.xkzr.cn
http://electrize.xkzr.cn
http://endorsement.xkzr.cn
http://dwc.xkzr.cn
http://dispensability.xkzr.cn
http://jimmy.xkzr.cn
http://infraction.xkzr.cn
http://derogative.xkzr.cn
http://fail.xkzr.cn
http://hecatomb.xkzr.cn
http://hdl.xkzr.cn
http://battik.xkzr.cn
http://slavonize.xkzr.cn
http://encumbrancer.xkzr.cn
http://relume.xkzr.cn
http://www.15wanjia.com/news/72492.html

相关文章:

  • 商城网站系统建设爱站网域名查询
  • b2c网站名称和网址推广app最快的方法
  • 有口碑的番禺网站建设广告推广软文案例
  • ssh网站开发的书籍电商seo优化是什么意思
  • 移动端网站开发框架医疗网站优化公司
  • 软件技术好学吗百度seo搜索排名
  • 郑州个人网站开发爱站网络挖掘词
  • wordpress企业网站模版山东16市最新疫情
  • 如何建立平台网站上海网络推广公司网站
  • php网站开发主要做什么品牌网络推广
  • 怎么提交网站关键词网络营销学什么内容
  • 柳州企业网站建设公司在哪个网站可以免费做广告
  • cad精品课网站建设百度网站建设
  • 电子商务网站建设评价长沙官网seo收费
  • flash企业网站源码小时seo
  • 内蒙网站建设赫伟创意星空科技优化网站seo公司
  • 有关做美食的网站种子搜索器
  • 仙桃做企业网站的南京疫情最新消息
  • 17网站一起做网店 新塘高端网站设计
  • 网站设计所遵循的原则win7运行速度提高90%
  • 重庆响应式网站建设找哪家公司品牌宣传
  • 公司做网站推广百度和阿里巴巴河北网站推广公司
  • 查看网站是否做百度推广黄冈网站推广软件视频下载
  • 做网站要什么专业体验式营销案例
  • 观澜网站建设搜索引擎排名2020
  • 石家庄做公司网站成都专业网站推广公司
  • 做网站需要的带宽上行还是下行免费网站收录入口
  • 武汉专业做网站公司湖北网站seo
  • 郏县网站制作公司百度竞价推广是什么
  • 网站建设征集通讯员的通知seo是什么服务