From 461dd40ac36933480b5efc4d9f2298f30652d5aa Mon Sep 17 00:00:00 2001 From: yizeliang Date: Fri, 8 May 2020 17:11:20 +0800 Subject: [PATCH] 0.0.1 --- .gitignore | 93 ++++++++++ README.md | 67 +++++++ app/.gitignore | 1 + app/build.gradle | 36 ++++ app/proguard-rules.pro | 21 +++ app/src/main/AndroidManifest.xml | 22 +++ .../cn/yzl/saved/delegate/simple/Fragment.kt | 10 + .../saved/delegate/simple/Main2Activity.kt | 12 ++ .../yzl/saved/delegate/simple/MainActivity.kt | 58 ++++++ .../drawable-v24/ic_launcher_foreground.xml | 30 +++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++++++ app/src/main/res/layout/activity_main.xml | 19 ++ app/src/main/res/layout/activity_main2.xml | 9 + .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3593 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5339 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2636 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 3388 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4926 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7472 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7909 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 11873 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10652 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 16570 bytes app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 ++ build.gradle | 17 ++ gradle.properties | 21 +++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 ++++++++++++++++++ gradlew.bat | 84 +++++++++ maven.gradle | 17 ++ savedelegate/.gitignore | 1 + savedelegate/build.gradle | 39 ++++ savedelegate/consumer-rules.pro | 0 savedelegate/proguard-rules.pro | 21 +++ savedelegate/src/main/AndroidManifest.xml | 2 + .../cn/yzl/saved/delegate/BundleWriter.kt | 153 ++++++++++++++++ .../cn/yzl/saved/delegate/SavedDelegate.kt | 110 +++++++++++ .../yzl/saved/delegate/SavedDelegateHelper.kt | 142 +++++++++++++++ .../saved/delegate/SavedDelegateProvider.kt | 40 ++++ screen/screen.gif | Bin 0 -> 1236444 bytes settings.gradle | 3 + upload.gradle | 37 ++++ 47 files changed, 1443 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/cn/yzl/saved/delegate/simple/Fragment.kt create mode 100644 app/src/main/java/cn/yzl/saved/delegate/simple/Main2Activity.kt create mode 100644 app/src/main/java/cn/yzl/saved/delegate/simple/MainActivity.kt create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/activity_main2.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 maven.gradle create mode 100644 savedelegate/.gitignore create mode 100644 savedelegate/build.gradle create mode 100644 savedelegate/consumer-rules.pro create mode 100644 savedelegate/proguard-rules.pro create mode 100644 savedelegate/src/main/AndroidManifest.xml create mode 100644 savedelegate/src/main/java/cn/yzl/saved/delegate/BundleWriter.kt create mode 100644 savedelegate/src/main/java/cn/yzl/saved/delegate/SavedDelegate.kt create mode 100644 savedelegate/src/main/java/cn/yzl/saved/delegate/SavedDelegateHelper.kt create mode 100644 savedelegate/src/main/java/cn/yzl/saved/delegate/SavedDelegateProvider.kt create mode 100644 screen/screen.gif create mode 100644 settings.gradle create mode 100644 upload.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1a044e --- /dev/null +++ b/.gitignore @@ -0,0 +1,93 @@ +*.iml +.gradle +/local.properties +.idea +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +### Android template +# Built application files +*.apk +*.ap_ +*.aab + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +release/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +# Android Studio 3 in .gitignore file. +.idea/caches +.idea/modules.xml +# Comment next line if keeping position of elements in Navigation Editor is relevant for you +.idea/navEditor.xml + +# Keystore files +# Uncomment the following lines if you do not want to check your keystore files in. +#*.jks +#*.keystore + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +# google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md + +# Version control +vcs.xml + +# lint +lint/intermediates/ +lint/generated/ +lint/outputs/ +lint/tmp/ +# lint/reports/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..eac0a3e --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +## SavedDelegate + +## 项目描述 + +还在傻傻的写 `onSaveInstanceState` 和 `onRestoreInstanceState` 方法么? + +还在为项目中 大量的 未保存和恢复处理的变量 引起的错误 头疼? + +**Just do it!!! you can fly!!!** + +## 使用方法 + +```kotlin + +//使用 delegate +var a: Int? by SavedDelegate { 10 } + +var b: IntArray by SavedDelegateNotNull { + IntArray(10) { it } +} + +//在Activity 合适的方法中(onCreate?,onResume?, 总之在 页面关闭之前调用即可) +SavedDelegateHelper.registerSimple(activity) +//或 +SavedDelegateHelper.registerSimple(fragment) + +//或 +SavedDelegateHelper.registerSavedProvider(savedStateRegistry,activity/fragment) +//或 +SavedDelegateHelper.registerWithLifecycle(lifecycleOwner,savedStateRegistry,activity/fragment) +``` + +然后,你的这些变量,在页面被销毁时,就会被自动恢复了!! + +## 效果 + +打开手机 开发者模式中的 不保留活动 + +![screen](./screen/screen.gif) + + +## 项目依赖 + +- androidx 和kotlin ,所以一定要是 kotlin 开发的项目,并且迁移了androidx库 +- 真正保存和恢复数据的是savestate库,这个库在 androidx 中默认被导入,所以可以直接使用 + +## TODO + +- 没有测试效率问题,因为保存数据采用了反射 +- 保存数据时会检查数据是否能够写入Bundle,主要是类型判断,这样存在隐患,需要触发了才能报错,想办法提前,检验 +- 还没有进行比较全面的测试,主要是数据类型方面 + + + +## 更新日志 + +### 0.0.1 + +项目初步完成,还没有进行比较全面的测试(数据类型方面) + +## 实现思路 + +1. 熟悉 savestate 库,强烈建议大家去看看 +2. 使用属性委托在get方法中,查询 savedStateRegistry 中保存的数据 +3. 为savedStateRegistry 注册 SavedStateRegistry.SavedStateProvider +4. 在 SavedStateRegistry.SavedStateProvider 中,使用反射,读取 activity/fragment 中使用了委托对象的属性 +5. 获取到对象属性后,获取值和类型,调用Bundle 相应的方法写入即可 diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..7a4dad9 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,36 @@ +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.3" + + defaultConfig { + applicationId "cn.yzl.saved.delegate.simple" + minSdkVersion 21 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" + implementation project(path: ':savedelegate') +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..16bc84d --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/cn/yzl/saved/delegate/simple/Fragment.kt b/app/src/main/java/cn/yzl/saved/delegate/simple/Fragment.kt new file mode 100644 index 0000000..443fe53 --- /dev/null +++ b/app/src/main/java/cn/yzl/saved/delegate/simple/Fragment.kt @@ -0,0 +1,10 @@ +package cn.yzl.saved.delegate.simple + +import androidx.fragment.app.Fragment +import cn.yzl.saved.delegate.SavedDelegateHelper + +class Fragment: Fragment() { + fun a(){ + SavedDelegateHelper.registerSimple(this) + } +} \ No newline at end of file diff --git a/app/src/main/java/cn/yzl/saved/delegate/simple/Main2Activity.kt b/app/src/main/java/cn/yzl/saved/delegate/simple/Main2Activity.kt new file mode 100644 index 0000000..3222f29 --- /dev/null +++ b/app/src/main/java/cn/yzl/saved/delegate/simple/Main2Activity.kt @@ -0,0 +1,12 @@ +package cn.yzl.saved.delegate.simple + +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle + +class Main2Activity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main2) + } +} diff --git a/app/src/main/java/cn/yzl/saved/delegate/simple/MainActivity.kt b/app/src/main/java/cn/yzl/saved/delegate/simple/MainActivity.kt new file mode 100644 index 0000000..fb4d94a --- /dev/null +++ b/app/src/main/java/cn/yzl/saved/delegate/simple/MainActivity.kt @@ -0,0 +1,58 @@ +package cn.yzl.saved.delegate.simple + +import android.content.Intent +import android.os.Bundle +import android.os.PersistableBundle +import android.util.Log +import androidx.appcompat.app.AppCompatActivity +import cn.yzl.saved.delegate.SavedDelegate +import cn.yzl.saved.delegate.SavedDelegateHelper +import cn.yzl.saved.delegate.SavedDelegateNotNull +import kotlinx.android.synthetic.main.activity_main.* + +class MainActivity : AppCompatActivity() { + + var a: Int? by SavedDelegate() { 10 } + + var b: String? by SavedDelegate() { "hahha" } + + var c: Boolean? by SavedDelegate { false } + + var d: IntArray by SavedDelegateNotNull { + IntArray(10) { it } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + second.setOnClickListener { + a = (a ?: 0) + 1 + b = null + c = true + d.forEachIndexed { index, i -> + d[index] *= 10 + } + startActivity(Intent(this, Main2Activity::class.java)) + } + SavedDelegateHelper.registerSimple(this) + } + + override fun onResume() { + super.onResume() + log("----") + log(a?.toString()) + log(b?.toString()) + log(c?.toString()) + log(d.joinToString(separator = ",")) +// log(e?.toString()) +// log(a?.toString()) + + log("----") + } + + + private fun log(msg: String?) { + Log.e("hahaha", msg ?: "NULL") + } + +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..d235c83 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,19 @@ + + + +