-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sky233ml
committed
Jan 9, 2024
1 parent
90d37de
commit bf09ae5
Showing
31 changed files
with
370 additions
and
196 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MavenPublication>("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") | ||
} |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
21 changes: 21 additions & 0 deletions
21
core-databinding/src/main/java/kndroidx/activity/ViewActivityX.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<VB : ViewBinding, VM : ViewModel> : BaseActivityX() { | ||
|
||
open val binding by lazy { createViewBinding<VB>(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 | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
core-databinding/src/main/java/kndroidx/fragment/ViewFragmentX.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<VB : ViewBinding, VM : ViewModel> : 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <VM : ViewModel> Any.createViewModel(position: Int = 0): VM { | ||
val vbClass = | ||
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance<Class<VM>>() | ||
val viewModel = vbClass[position] | ||
return ViewModelProvider(this@BaseActivityX)[viewModel] | ||
} | ||
|
||
fun <VB : ViewBinding> Any.createViewBinding( | ||
layoutInflater: LayoutInflater = [email protected], | ||
position: Int = 0, | ||
): VB { | ||
val vbClass = | ||
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments.filterIsInstance<Class<VB>>() | ||
val inflate = vbClass[position].getDeclaredMethod("inflate", LayoutInflater::class.java) | ||
return inflate.invoke(null, layoutInflater) as VB | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package kndroidx.extension | ||
|
||
class RunnableX(val block: Runnable.() -> Unit): Runnable { | ||
override fun run() { | ||
block() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...n/java/kndroidx/extension/ViewExtraFun.kt → ...n/java/kndroidx/extension/ViewExtraFun.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.