Skip to content

Commit

Permalink
不知道有什么了,还没养成写commit的习惯
Browse files Browse the repository at this point in the history
  • Loading branch information
Sky233ml committed Jan 9, 2024
1 parent 90d37de commit bf09ae5
Show file tree
Hide file tree
Showing 31 changed files with 370 additions and 196 deletions.
8 changes: 4 additions & 4 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
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.
75 changes: 75 additions & 0 deletions core-databinding/build.gradle.kts
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.
4 changes: 4 additions & 0 deletions core-databinding/src/main/AndroidManifest.xml
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 core-databinding/src/main/java/kndroidx/activity/ViewActivityX.kt
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 core-databinding/src/main/java/kndroidx/fragment/ViewFragmentX.kt
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
}
}
1 change: 1 addition & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
13 changes: 4 additions & 9 deletions kndroidx/build.gradle.kts → core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
21 changes: 21 additions & 0 deletions core/proguard-rules.pro
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.
20 changes: 20 additions & 0 deletions core/src/main/java/kndroidx/KndroidX.kt
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.")
}
}
44 changes: 44 additions & 0 deletions core/src/main/java/kndroidx/activity/BaseActivityX.kt
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/kndroidx/extension/RunnableExtraFun.kt
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()
}
}
Original file line number Diff line number Diff line change
@@ -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<ClipboardManager>()
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
Loading

0 comments on commit bf09ae5

Please sign in to comment.