Skip to content

Commit

Permalink
[feat]:加入base模块、ViewBinding模块
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyanggmail committed Nov 13, 2022
1 parent 82c2741 commit 13a4712
Show file tree
Hide file tree
Showing 65 changed files with 3,255 additions and 849 deletions.
12 changes: 9 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {

android {
namespace 'com.guoyang.androidutils'
compileSdk 32
compileSdk 33

defaultConfig {
applicationId "com.guoyang.androidutils"
minSdk 21
targetSdk 32
targetSdk 33
versionCode 1
versionName "1.0"

Expand All @@ -30,11 +30,17 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true // 开启viewBinding
}
}

dependencies {
api project(path: ':base')
api project(path: ':xloghelper')
implementation 'androidx.core:core-ktx:1.7.0'
api project(path: ':utils-helper')
api project(path: ':viewbinding-helper')
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools" >

<application
android:name=".MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -11,10 +12,17 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidUtils"
tools:targetApi="31">
tools:targetApi="31" >
<activity
android:name=".InfoActivity"
android:exported="false" >
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/guoyang/androidutils/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.guoyang.androidutils

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.guoyang.viewbinding_helper.ActivityBinding
import com.guoyang.viewbinding_helper.ActivityBindingDelegate

/**
* @author yang.guo on 2022/11/13
* @describe Activity基类
*/
abstract class BaseActivity<T : ViewBinding> : AppCompatActivity(),
ActivityBinding<T> by ActivityBindingDelegate() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentViewWithBinding()
initView(savedInstanceState)
}

abstract fun initView(savedInstanceState: Bundle?)
}
20 changes: 20 additions & 0 deletions app/src/main/java/com/guoyang/androidutils/InfoActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.guoyang.androidutils

import android.os.Bundle
import com.guoyang.androidutils.databinding.ActivityInfoBinding
import com.guoyang.utils_helper.bundle
import com.guoyang.utils_helper.doOnClick

class InfoActivity : BaseActivity<ActivityInfoBinding>() {
private val name: String? by bundle()
private val age: Int? by bundle()

override fun initView(savedInstanceState: Bundle?) {
binding.apply {
tvName.text = name.toString()
tvAge.text = age.toString()
btnSend.doOnClick {
}
}
}
}
20 changes: 15 additions & 5 deletions app/src/main/java/com/guoyang/androidutils/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.guoyang.androidutils

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.guoyang.androidutils.databinding.ActivityMainBinding
import com.guoyang.utils_helper.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun initView(savedInstanceState: Bundle?) {
immersive(binding.toolBar)
binding.button.doOnDebouncingClick {
startActivity<InfoActivity>(
"name" to "guoyang",
"age" to 18,
)
}

AppForegroundObserver().observe(this) {
toast("当前状态: $it")
}
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/guoyang/androidutils/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.guoyang.androidutils

import android.app.Application

/**
* @author yang.guo on 2022/11/13
* @describe
*/
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/guoyang/androidutils/UserInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.guoyang.androidutils

/**
* @author yang.guo on 2022/11/13
* @describe
*/
data class UserInfo(val name: String, val age: Int) : java.io.Serializable
35 changes: 35 additions & 0 deletions app/src/main/res/layout/activity_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InfoActivity">

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/tv_age"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/tv_name"
app:layout_constraintStart_toStartOf="@id/tv_name"
app:layout_constraintTop_toBottomOf="@id/tv_name" />

<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SendEvent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_age" />
</androidx.constraintlayout.widget.ConstraintLayout>
19 changes: 18 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/black"
android:text="@string/app_name"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/white"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@id/button"
app:layout_constraintStart_toStartOf="@id/button"
app:layout_constraintTop_toBottomOf="@id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AndroidUtils" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.AndroidUtils" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions base/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

android {
namespace 'com.guoyang.base'
compileSdkVersion 33

defaultConfig {
minSdkVersion 21
targetSdkVersion 33
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// 开启module混淆
consumerProguardFiles "consumer-rules.pro"
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

//androidx
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

//kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20"
implementation 'androidx.core:core-ktx:1.9.0'
implementation "androidx.fragment:fragment-ktx:1.5.3"

//lifecycle
def lifecycleVersion = '2.5.1'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:${lifecycleVersion}"
implementation "androidx.lifecycle:lifecycle-common-java8:${lifecycleVersion}"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${lifecycleVersion}"

//LiveData数据倒灌问题 https://github.com/KunMinX/UnPeek-LiveData
implementation 'com.kunminx.arch:unpeek-livedata:7.8.0'
//权限框架 https://github.com/guolindev/PermissionX
implementation 'com.guolindev.permissionx:permissionx:1.7.1'

implementation 'androidx.multidex:multidex:2.0.1'

//test
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
8 changes: 8 additions & 0 deletions base/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 图片选择库 https://github.com/LuckSiege/PictureSelector/blob/version_component/README_CN.md
-keep class com.luck.picture.lib.** { *; }
# 如果引入了Camerax库请添加混淆
-keep class com.luck.lib.camerax.** { *; }
# 如果引入了Ucrop库请添加混淆
-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.guoyang.event_helper
package com.guoyang.base

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -19,6 +19,6 @@ class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.guoyang.event_helper.test", appContext.packageName)
assertEquals("com.guoyang.base.test", appContext.packageName)
}
}
File renamed without changes.
21 changes: 21 additions & 0 deletions base/src/main/java/com/guoyang/base/BaseApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.guoyang.base

import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex

/***
* 提供基类的Application
* @author Yang.Guo on 2021/5/31.
*/
open class BaseApp : Application() {

override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
MultiDex.install(this)
}

override fun onCreate() {
super.onCreate()
}
}
9 changes: 9 additions & 0 deletions base/src/main/java/com/guoyang/base/event/EventLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.guoyang.base.event

import com.kunminx.architecture.ui.callback.UnPeekLiveData

/***
* 解决数据倒灌的LiveData,可以用来做事件订阅
* @author Yang.Guo on 2021/6/3.
*/
class EventLiveData<T> : UnPeekLiveData<T>()
28 changes: 28 additions & 0 deletions base/src/main/java/com/guoyang/base/ext/ContextExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.guoyang.base.ext

import android.content.Context
import android.os.Process

// <editor-fold desc="Context相关的扩展类">
/**
* 判断当前是否是主进程
* @param block: 结果回调(回调在主进程中执行)
*/
inline fun Context.runMainProcess(block: () -> Unit) {
val myPid = Process.myPid()
val mActivityManager =
this.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
val var3 = mActivityManager.runningAppProcesses?.iterator()
while (var3?.hasNext() == true) {
val appProcessInfo = var3.next() as android.app.ActivityManager.RunningAppProcessInfo
if (appProcessInfo.pid == myPid && appProcessInfo.processName.equals(
this.packageName, ignoreCase = true
)
) {
block()
break
}
}
}

// </editor-fold>
Loading

0 comments on commit 13a4712

Please sign in to comment.