From bf09ae53902f32ae84b5e289ec32b61b1b784032 Mon Sep 17 00:00:00 2001 From: Sky233ml Date: Tue, 9 Jan 2024 22:30:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E7=9F=A5=E9=81=93=E6=9C=89=E4=BB=80?= =?UTF-8?q?=E4=B9=88=E4=BA=86,=E8=BF=98=E6=B2=A1=E5=85=BB=E6=88=90?= =?UTF-8?q?=E5=86=99commit=E7=9A=84=E4=B9=A0=E6=83=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/gradle.xml | 8 +- .idea/kotlinc.xml | 2 +- app/build.gradle.kts | 2 +- build.gradle.kts | 4 +- {kndroidx => core-databinding}/.gitignore | 0 core-databinding/build.gradle.kts | 75 +++++++++++++++++++ core-databinding/consumer-rules.pro | 0 .../proguard-rules.pro | 0 core-databinding/src/main/AndroidManifest.xml | 4 + .../java/kndroidx/activity/ViewActivityX.kt | 21 ++++++ .../java/kndroidx/fragment/ViewFragmentX.kt | 28 +++++++ core/.gitignore | 1 + {kndroidx => core}/build.gradle.kts | 13 +--- core/proguard-rules.pro | 21 ++++++ .../src/main/AndroidManifest.xml | 0 core/src/main/java/kndroidx/KndroidX.kt | 20 +++++ .../java/kndroidx/activity/BaseActivityX.kt | 44 +++++++++++ .../java/kndroidx/extension/LogExtraFun.kt | 4 +- .../kndroidx/extension/RunnableExtraFun.kt | 7 ++ .../java/kndroidx/extension/TextExtraFun.kt | 6 +- .../java/kndroidx/extension/ViewExtraFun.kt | 2 +- .../java/kndroidx/fragment/BaseFragmentX.kt | 53 +++++++++++++ .../java/kndroidx/preference/PreferenceX.kt | 72 ++++++++++++++++++ .../kndroidx/preference/SharedPreferencesX.kt | 2 +- .../main/java/kndroidx/activity/ActivityX.kt | 18 ----- .../kndroidx/activity/BaseViewActivity.kt | 49 ------------ .../java/kndroidx/application/ApplicationX.kt | 22 ------ .../kndroidx/extension/RunnableExtraFun.kt | 9 --- .../kndroidx/fragment/BaseViewFragment.kt | 58 -------------- .../main/java/kndroidx/fragment/FragmentX.kt | 18 ----- settings.gradle.kts | 3 +- 31 files changed, 370 insertions(+), 196 deletions(-) rename {kndroidx => core-databinding}/.gitignore (100%) create mode 100644 core-databinding/build.gradle.kts create mode 100644 core-databinding/consumer-rules.pro rename {kndroidx => core-databinding}/proguard-rules.pro (100%) create mode 100644 core-databinding/src/main/AndroidManifest.xml create mode 100644 core-databinding/src/main/java/kndroidx/activity/ViewActivityX.kt create mode 100644 core-databinding/src/main/java/kndroidx/fragment/ViewFragmentX.kt create mode 100644 core/.gitignore rename {kndroidx => core}/build.gradle.kts (78%) create mode 100644 core/proguard-rules.pro rename {kndroidx => core}/src/main/AndroidManifest.xml (100%) create mode 100644 core/src/main/java/kndroidx/KndroidX.kt create mode 100644 core/src/main/java/kndroidx/activity/BaseActivityX.kt rename {kndroidx => core}/src/main/java/kndroidx/extension/LogExtraFun.kt (86%) create mode 100644 core/src/main/java/kndroidx/extension/RunnableExtraFun.kt rename {kndroidx => core}/src/main/java/kndroidx/extension/TextExtraFun.kt (89%) rename {kndroidx => core}/src/main/java/kndroidx/extension/ViewExtraFun.kt (88%) create mode 100644 core/src/main/java/kndroidx/fragment/BaseFragmentX.kt create mode 100644 core/src/main/java/kndroidx/preference/PreferenceX.kt rename {kndroidx => core}/src/main/java/kndroidx/preference/SharedPreferencesX.kt (98%) delete mode 100644 kndroidx/src/main/java/kndroidx/activity/ActivityX.kt delete mode 100644 kndroidx/src/main/java/kndroidx/activity/BaseViewActivity.kt delete mode 100644 kndroidx/src/main/java/kndroidx/application/ApplicationX.kt delete mode 100644 kndroidx/src/main/java/kndroidx/extension/RunnableExtraFun.kt delete mode 100644 kndroidx/src/main/java/kndroidx/fragment/BaseViewFragment.kt delete mode 100644 kndroidx/src/main/java/kndroidx/fragment/FragmentX.kt diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 2f15805..4948474 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -4,17 +4,17 @@ diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index 2b8a50f..f8467b4 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 3b1eea0..09a4488 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -40,7 +40,7 @@ dependencies { implementation("androidx.core:core-ktx:1.9.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") - implementation(project(mapOf("path" to ":kndroidx"))) + implementation(project(mapOf("path" to ":core"))) testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") diff --git a/build.gradle.kts b/build.gradle.kts index 904f170..5192b5f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,9 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id("com.android.application") version "8.1.2" apply false + id("com.android.application") version "8.2.0" apply false id("org.jetbrains.kotlin.android") version "1.8.0" apply false id("maven-publish") + id("com.android.library") version "8.2.0" apply false + kotlin("kapt") version "1.9.10" } diff --git a/kndroidx/.gitignore b/core-databinding/.gitignore similarity index 100% rename from kndroidx/.gitignore rename to core-databinding/.gitignore diff --git a/core-databinding/build.gradle.kts b/core-databinding/build.gradle.kts new file mode 100644 index 0000000..6f2df1c --- /dev/null +++ b/core-databinding/build.gradle.kts @@ -0,0 +1,75 @@ +plugins { + id("com.android.library") + id("org.jetbrains.kotlin.android") + id("maven-publish") + kotlin("kapt") +} + +val GROUP_ID = "com.github.kndroidx" +val ARTIFACT_ID = "core-databinding" +val VERSION = latestGitTag().ifEmpty { "0.1.1-alpha" } + +fun latestGitTag(): String { + val process = ProcessBuilder("git", "describe", "--tags", "--abbrev=0").start() + return process.inputStream.bufferedReader().use { bufferedReader -> + bufferedReader.readText().trim() + } +} + + +publishing { // 发布配置 + publications { // 发布的内容 + register("release") { // 注册一个名字为 release 的发布内容 + groupId = GROUP_ID + artifactId = ARTIFACT_ID + version = VERSION + + afterEvaluate { // 在所有的配置都完成之后执行 + // 从当前 module 的 release 包中发布 + from(components["release"]) + } + } + } + repositories { + mavenLocal() + } +} + +android { + namespace = "com.github.kndroidx" + compileSdk = 34 + + defaultConfig { + minSdk = 23 + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles("consumer-rules.pro") + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = "1.8" + } + buildFeatures { + dataBinding = true + } +} + +dependencies { + implementation("androidx.core:core-ktx:1.12.0") + implementation("androidx.core:core-ktx:1.12.0") + implementation(project(":core")) + implementation("androidx.appcompat:appcompat:1.6.1") +} \ No newline at end of file diff --git a/core-databinding/consumer-rules.pro b/core-databinding/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/kndroidx/proguard-rules.pro b/core-databinding/proguard-rules.pro similarity index 100% rename from kndroidx/proguard-rules.pro rename to core-databinding/proguard-rules.pro diff --git a/core-databinding/src/main/AndroidManifest.xml b/core-databinding/src/main/AndroidManifest.xml new file mode 100644 index 0000000..a5918e6 --- /dev/null +++ b/core-databinding/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/core-databinding/src/main/java/kndroidx/activity/ViewActivityX.kt b/core-databinding/src/main/java/kndroidx/activity/ViewActivityX.kt new file mode 100644 index 0000000..29cf37c --- /dev/null +++ b/core-databinding/src/main/java/kndroidx/activity/ViewActivityX.kt @@ -0,0 +1,21 @@ +package kndroidx.activity + +import android.view.View +import androidx.databinding.ViewDataBinding +import androidx.lifecycle.ViewModel +import androidx.viewbinding.ViewBinding + +abstract class ViewActivityX : BaseActivityX() { + + open val binding by lazy { createViewBinding(layoutInflater) } + private var _viewModel: VM? = null + val viewModel get() = _viewModel!! + + override fun onCreateView(): View = binding.apply { + if (binding is ViewDataBinding) { + (binding as ViewDataBinding).lifecycleOwner = this@ViewActivityX + } + _viewModel = createViewModel() + }.root + +} \ No newline at end of file diff --git a/core-databinding/src/main/java/kndroidx/fragment/ViewFragmentX.kt b/core-databinding/src/main/java/kndroidx/fragment/ViewFragmentX.kt new file mode 100644 index 0000000..d96c851 --- /dev/null +++ b/core-databinding/src/main/java/kndroidx/fragment/ViewFragmentX.kt @@ -0,0 +1,28 @@ +package kndroidx.fragment + +import android.view.LayoutInflater +import android.view.View +import androidx.databinding.ViewDataBinding +import androidx.lifecycle.ViewModel +import androidx.viewbinding.ViewBinding + +class ViewFragmentX : BaseFragmentX() { + private var _binding: VB? = null + val binding get() = _binding!! + private var _viewModel: VM? = null + val viewModel get() = _viewModel!! + + override fun onCreateView(inflater: LayoutInflater): View { + _binding = createViewBinding(inflater) + if (_binding is ViewDataBinding) { + (_binding as ViewDataBinding).lifecycleOwner = viewLifecycleOwner + } + _viewModel = createViewModel() + return binding.root + } + + override fun onDestroy() { + super.onDestroy() + _binding = null + } +} \ No newline at end of file diff --git a/core/.gitignore b/core/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/core/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/kndroidx/build.gradle.kts b/core/build.gradle.kts similarity index 78% rename from kndroidx/build.gradle.kts rename to core/build.gradle.kts index 86b0451..2fa7926 100644 --- a/kndroidx/build.gradle.kts +++ b/core/build.gradle.kts @@ -4,9 +4,9 @@ plugins { id("maven-publish") } -val GROUP_ID = "com.github.KndroidX" -val ARTIFACT_ID = "KndroidX" -val VERSION = latestGitTag().ifEmpty { "0.1.0-alpha" } +val GROUP_ID = "com.github.kndroidx" +val ARTIFACT_ID = "core" +val VERSION = latestGitTag().ifEmpty { "0.1.1-alpha" } fun latestGitTag(): String { val process = ProcessBuilder("git", "describe", "--tags", "--abbrev=0").start() @@ -65,11 +65,6 @@ android { } dependencies { - - implementation("androidx.core:core-ktx:1.9.0") + implementation("androidx.core:core-ktx:1.12.0") implementation("androidx.appcompat:appcompat:1.6.1") - implementation("com.google.android.material:material:1.9.0") - testImplementation("junit:junit:4.13.2") - androidTestImplementation("androidx.test.ext:junit:1.1.5") - androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") } diff --git a/core/proguard-rules.pro b/core/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/core/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 \ No newline at end of file diff --git a/kndroidx/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml similarity index 100% rename from kndroidx/src/main/AndroidManifest.xml rename to core/src/main/AndroidManifest.xml diff --git a/core/src/main/java/kndroidx/KndroidX.kt b/core/src/main/java/kndroidx/KndroidX.kt new file mode 100644 index 0000000..4b599e3 --- /dev/null +++ b/core/src/main/java/kndroidx/KndroidX.kt @@ -0,0 +1,20 @@ +package kndroidx + +import android.annotation.SuppressLint +import android.content.Context + +fun kndroidx(block: KndroidX.() -> Unit) { + KndroidX.block() +} + +@SuppressLint("StaticFieldLeak") +object KndroidX { + private var _context: Context? = null + var context: Context + set(value) { + _context = value + } + get() { + return _context ?: throw NullPointerException("KndroidX didn't init.") + } +} \ No newline at end of file diff --git a/core/src/main/java/kndroidx/activity/BaseActivityX.kt b/core/src/main/java/kndroidx/activity/BaseActivityX.kt new file mode 100644 index 0000000..79e9918 --- /dev/null +++ b/core/src/main/java/kndroidx/activity/BaseActivityX.kt @@ -0,0 +1,44 @@ +@file:Suppress("UNCHECKED_CAST") + +package kndroidx.activity + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.viewbinding.ViewBinding +import java.lang.reflect.ParameterizedType + +abstract class BaseActivityX : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + beforeSetContent() + setContentView(onCreateView()) + init() + } + + open fun beforeSetContent() {} + + open fun init() {} + + abstract fun onCreateView(): View + + fun Any.createViewModel(position: Int = 0): VM { + val vbClass = + (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() + val viewModel = vbClass[position] + return ViewModelProvider(this@BaseActivityX)[viewModel] + } + + fun Any.createViewBinding( + layoutInflater: LayoutInflater = this@BaseActivityX.layoutInflater, + position: Int = 0, + ): VB { + val vbClass = + (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() + val inflate = vbClass[position].getDeclaredMethod("inflate", LayoutInflater::class.java) + return inflate.invoke(null, layoutInflater) as VB + } +} \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/extension/LogExtraFun.kt b/core/src/main/java/kndroidx/extension/LogExtraFun.kt similarity index 86% rename from kndroidx/src/main/java/kndroidx/extension/LogExtraFun.kt rename to core/src/main/java/kndroidx/extension/LogExtraFun.kt index d391a95..44238e3 100644 --- a/kndroidx/src/main/java/kndroidx/extension/LogExtraFun.kt +++ b/core/src/main/java/kndroidx/extension/LogExtraFun.kt @@ -2,7 +2,7 @@ package kndroidx.extension import android.content.pm.ApplicationInfo import android.util.Log -import kndroidx.application.ApplicationX.Companion.context +import kndroidx.KndroidX.context var TAG = "TAG" @@ -12,7 +12,7 @@ val Any.log: LogObject fun Any.log(tag: String = TAG) = LogObject.getObj(tag, this.toString()) fun isDebug(): Boolean { - return context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 + return context!!.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0 } class LogObject private constructor(private val tag: String, private val msg: String) { diff --git a/core/src/main/java/kndroidx/extension/RunnableExtraFun.kt b/core/src/main/java/kndroidx/extension/RunnableExtraFun.kt new file mode 100644 index 0000000..f1e84ef --- /dev/null +++ b/core/src/main/java/kndroidx/extension/RunnableExtraFun.kt @@ -0,0 +1,7 @@ +package kndroidx.extension + +class RunnableX(val block: Runnable.() -> Unit): Runnable { + override fun run() { + block() + } +} \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/extension/TextExtraFun.kt b/core/src/main/java/kndroidx/extension/TextExtraFun.kt similarity index 89% rename from kndroidx/src/main/java/kndroidx/extension/TextExtraFun.kt rename to core/src/main/java/kndroidx/extension/TextExtraFun.kt index 4a408c2..4d47bc5 100644 --- a/kndroidx/src/main/java/kndroidx/extension/TextExtraFun.kt +++ b/core/src/main/java/kndroidx/extension/TextExtraFun.kt @@ -1,13 +1,15 @@ package kndroidx.extension +import android.annotation.SuppressLint import android.content.ClipData import android.content.ClipboardManager import android.graphics.Color import android.os.Build import android.widget.TextView import android.widget.Toast +import androidx.annotation.StringRes import androidx.core.content.getSystemService -import kndroidx.application.ApplicationX.Companion.context +import kndroidx.KndroidX.context val clipboard = lazy { context.getSystemService() @@ -25,7 +27,9 @@ fun Any.toast(int: Int = Toast.LENGTH_SHORT) { } } +@SuppressLint("SupportAnnotationUsage") @Suppress("DEPRECATION") +@StringRes fun Int.toast(int: Int = Toast.LENGTH_SHORT) { Toast.makeText(context, this, int).apply { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) { diff --git a/kndroidx/src/main/java/kndroidx/extension/ViewExtraFun.kt b/core/src/main/java/kndroidx/extension/ViewExtraFun.kt similarity index 88% rename from kndroidx/src/main/java/kndroidx/extension/ViewExtraFun.kt rename to core/src/main/java/kndroidx/extension/ViewExtraFun.kt index b8eddd6..e745fb3 100644 --- a/kndroidx/src/main/java/kndroidx/extension/ViewExtraFun.kt +++ b/core/src/main/java/kndroidx/extension/ViewExtraFun.kt @@ -1,7 +1,7 @@ package kndroidx.extension import android.widget.TextView -import kndroidx.application.ApplicationX.Companion.context +import kndroidx.KndroidX.context operator fun TextView.compareTo(any: Any): Int { text = any.toString() diff --git a/core/src/main/java/kndroidx/fragment/BaseFragmentX.kt b/core/src/main/java/kndroidx/fragment/BaseFragmentX.kt new file mode 100644 index 0000000..65664a7 --- /dev/null +++ b/core/src/main/java/kndroidx/fragment/BaseFragmentX.kt @@ -0,0 +1,53 @@ +@file:Suppress("UNCHECKED_CAST") + +package kndroidx.fragment + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.viewbinding.ViewBinding +import java.lang.reflect.ParameterizedType + +abstract class BaseFragmentX : Fragment() { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View? { + super.onCreateView(inflater, container, savedInstanceState) + beforeSetContent() + return onCreateView(inflater) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + init() + } + + open fun init() {} + + open fun beforeSetContent() {} + + abstract fun onCreateView(inflater: LayoutInflater): View + + fun createViewModel(position: Int = 0): VM { + val vbClass = + (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() + val viewModel = vbClass[position] + return ViewModelProvider(requireActivity())[viewModel] + } + + fun createViewBinding( + layoutInflater: LayoutInflater, + position: Int = 0, + ): VB { + val vbClass = + (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() + val inflate = vbClass[position].getDeclaredMethod("inflate", LayoutInflater::class.java) + return inflate.invoke(null, layoutInflater) as VB + } +} \ No newline at end of file diff --git a/core/src/main/java/kndroidx/preference/PreferenceX.kt b/core/src/main/java/kndroidx/preference/PreferenceX.kt new file mode 100644 index 0000000..27a7aa9 --- /dev/null +++ b/core/src/main/java/kndroidx/preference/PreferenceX.kt @@ -0,0 +1,72 @@ +@file:Suppress("UNCHECKED_CAST") + +package kndroidx.preference + +import android.content.Context +import android.content.SharedPreferences +import kndroidx.KndroidX.context +import kotlin.reflect.KProperty + +class PreferencesX(private val sp: SharedPreferences) { + + constructor(name: String, mode: Int = Context.MODE_PRIVATE) : this( + context.getSharedPreferences( + name, mode + ) + ) + + operator fun getValue(thisRef: Any?, property: KProperty<*>): SharedPreferences { + return sp + } + + fun intPreference(name: String, defaultValue: Int) = Preference(sp, name, defaultValue) + + fun stringPreference(name: String, defaultValue: String) = Preference(sp, name, defaultValue) + + fun longPreference(name: String, defaultValue: Long) = Preference(sp, name, defaultValue) + + fun floatPreference(name: String, defaultValue: Float) = Preference(sp, name, defaultValue) + + fun setPreference(name: String, defaultValue: Set) = Preference(sp, name, defaultValue) + + fun booleanPreference(name: String, defaultValue: Boolean) = Preference(sp, name, defaultValue) + +} + +class Preference( + private val sp: SharedPreferences, + private val name: String, + private val defaultValue: T, +) { + operator fun getValue(thisRef: Any?, property: KProperty<*>): T { + with(sp) { + return when (defaultValue) { + is Int -> getInt(name, defaultValue) as T + is String -> getString(name, defaultValue) as T + is Float -> getFloat(name, defaultValue) as T + is Boolean -> getBoolean(name, defaultValue) as T + is Long -> getLong(name, defaultValue) as T + is Set<*> -> getStringSet(name, defaultValue as Set) as T + else -> throw IllegalArgumentException("Unsupported type") + } + } + } + + operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { + with(sp.edit()) { + when (value) { + is Int -> putInt(name, value) + is String -> putString(name, value) + is Float -> putFloat(name, value) + is Boolean -> putBoolean(name, value) + is Long -> putLong(name, value) + is Set<*> -> putStringSet(name, value as Set) + else -> throw IllegalArgumentException("Unsupported type") + } + apply() + } + } +} + + +fun preferencesX(name: String, mode: Int = Context.MODE_PRIVATE) = PreferencesX(name, mode) \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/preference/SharedPreferencesX.kt b/core/src/main/java/kndroidx/preference/SharedPreferencesX.kt similarity index 98% rename from kndroidx/src/main/java/kndroidx/preference/SharedPreferencesX.kt rename to core/src/main/java/kndroidx/preference/SharedPreferencesX.kt index 6ad257c..c425635 100644 --- a/kndroidx/src/main/java/kndroidx/preference/SharedPreferencesX.kt +++ b/core/src/main/java/kndroidx/preference/SharedPreferencesX.kt @@ -3,6 +3,7 @@ package kndroidx.preference import android.content.SharedPreferences @Suppress("UNCHECKED_CAST") +@Deprecated("Unable to use Delegate") class SharedPreferencesX(val sp: SharedPreferences) { inline fun edit(block: EditorX.() -> Unit) { @@ -71,5 +72,4 @@ class SharedPreferencesX(val sp: SharedPreferences) { } } - } \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/activity/ActivityX.kt b/kndroidx/src/main/java/kndroidx/activity/ActivityX.kt deleted file mode 100644 index f631c9a..0000000 --- a/kndroidx/src/main/java/kndroidx/activity/ActivityX.kt +++ /dev/null @@ -1,18 +0,0 @@ -package kndroidx.activity - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import androidx.viewbinding.ViewBinding -import java.lang.reflect.ParameterizedType - - -abstract class ActivityX : BaseViewActivity() { - - val viewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) { createViewModel(1) } - - private fun createViewModel(position:Int): VM { - val vbClass = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() - val viewModel = vbClass[position] as Class - return ViewModelProvider(this)[viewModel] - } -} \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/activity/BaseViewActivity.kt b/kndroidx/src/main/java/kndroidx/activity/BaseViewActivity.kt deleted file mode 100644 index 0019b5b..0000000 --- a/kndroidx/src/main/java/kndroidx/activity/BaseViewActivity.kt +++ /dev/null @@ -1,49 +0,0 @@ -package kndroidx.activity - -import android.app.Activity -import android.content.Intent -import android.os.Bundle -import android.view.LayoutInflater -import androidx.appcompat.app.AppCompatActivity -import androidx.viewbinding.ViewBinding -import java.lang.reflect.ParameterizedType - - -abstract class BaseViewActivity : AppCompatActivity() { - open val binding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { - getViewBinding(layoutInflater) - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - beforeSetContentView() - setContentView(binding.root) - init() - initView() - initData() - initAdapter() - initListener() - } - - open fun beforeSetContentView() {} - - open fun initAdapter() {} - - open fun initListener() {} - - open fun initView() {} - - open fun initData() {} - - open fun init() {} - - @Suppress("UNCHECKED_CAST") - private fun Any.getViewBinding(inflater: LayoutInflater): VB { - val vbClass = - (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() - val inflate = vbClass[0].getDeclaredMethod("inflate", LayoutInflater::class.java) - return inflate.invoke(null, inflater) as VB - } - - -} \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/application/ApplicationX.kt b/kndroidx/src/main/java/kndroidx/application/ApplicationX.kt deleted file mode 100644 index 1f25008..0000000 --- a/kndroidx/src/main/java/kndroidx/application/ApplicationX.kt +++ /dev/null @@ -1,22 +0,0 @@ -package kndroidx.application - -import android.annotation.SuppressLint -import android.app.Application -import android.content.Context - -@SuppressLint("StaticFieldLeak") -open class ApplicationX : Application() { - - companion object { - lateinit var context: Context - lateinit var application: ApplicationX - } - - override fun onCreate() { - super.onCreate() - context = this - application = this - } - -} - diff --git a/kndroidx/src/main/java/kndroidx/extension/RunnableExtraFun.kt b/kndroidx/src/main/java/kndroidx/extension/RunnableExtraFun.kt deleted file mode 100644 index 4b2272d..0000000 --- a/kndroidx/src/main/java/kndroidx/extension/RunnableExtraFun.kt +++ /dev/null @@ -1,9 +0,0 @@ -package kndroidx.extension - -fun RunnableX(block: Runnable.() -> Unit): Runnable { - return object : Runnable { - override fun run() { - block() - } - } -} \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/fragment/BaseViewFragment.kt b/kndroidx/src/main/java/kndroidx/fragment/BaseViewFragment.kt deleted file mode 100644 index 75f632b..0000000 --- a/kndroidx/src/main/java/kndroidx/fragment/BaseViewFragment.kt +++ /dev/null @@ -1,58 +0,0 @@ -package kndroidx.fragment - -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.fragment.app.Fragment -import androidx.viewbinding.ViewBinding -import java.lang.reflect.ParameterizedType - - -abstract class BaseViewFragment : Fragment() { - open val binding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { - getViewBinding(layoutInflater) - } - - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle?, - ): View? { - super.onCreateView(inflater, container, savedInstanceState) - beforeSetContentView() - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - init() - initView() - initData() - initAdapter() - initListener() - } - - open fun beforeSetContentView() {} - - open fun initAdapter() {} - - open fun initListener() {} - - open fun initView() {} - - open fun initData() {} - - open fun init() {} - - @Suppress("UNCHECKED_CAST") - private fun Any.getViewBinding(inflater: LayoutInflater): VB { - val vbClass = - (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() - val inflate = vbClass[0].getDeclaredMethod("inflate", LayoutInflater::class.java) - return inflate.invoke(null, inflater) as VB - } - - -} \ No newline at end of file diff --git a/kndroidx/src/main/java/kndroidx/fragment/FragmentX.kt b/kndroidx/src/main/java/kndroidx/fragment/FragmentX.kt deleted file mode 100644 index 0e6aadc..0000000 --- a/kndroidx/src/main/java/kndroidx/fragment/FragmentX.kt +++ /dev/null @@ -1,18 +0,0 @@ -package kndroidx.fragment - -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider -import androidx.viewbinding.ViewBinding -import java.lang.reflect.ParameterizedType - -abstract class FragmentX : BaseViewFragment() { - - val viewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) { createViewModel(1) } - - private fun createViewModel(position: Int): VM { - val vbClass = - (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance>() - val viewModel = vbClass[position] as Class - return ViewModelProvider(requireActivity())[viewModel] - } -} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index f1a1708..e8f6073 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -17,4 +17,5 @@ dependencyResolutionManagement { rootProject.name = "KndroidX" include(":app") -include(":kndroidx") +include(":core") +include(":core-databinding")