Compare commits
10 Commits
c6dddba8e4
...
df93eeec3f
| Author | SHA1 | Date | |
|---|---|---|---|
| df93eeec3f | |||
| e07c98e0a0 | |||
| e0835808e3 | |||
| 337f7fa15e | |||
| 2fd0c18b29 | |||
| be9073af7c | |||
| e17f540e44 | |||
| 980d7053e2 | |||
| ec8712a23f | |||
| cdb8722d58 |
@ -6,7 +6,7 @@
|
||||
|
||||
## Demo
|
||||
|
||||
以鸿洋大神的玩安卓开放Api做的一款极简风格的玩安卓,仓库地址:[WanAndroidMVVM](https://github.com/Quyunshuo/WanAndroidMVVM),该项目正在开发中,由于工作较忙需要一段时间才能完成。如果想看旧版本的demo,可以直接打开demo分支看。
|
||||
以鸿洋大神的玩安卓开放Api做了简单的页面示例,仓库地址:[WanAndroidMVVM](https://github.com/Quyunshuo/WanAndroidMVVM)
|
||||
|
||||
## 框架图示
|
||||
|
||||
@ -387,5 +387,4 @@ fun test() {
|
||||
|
||||
三方库源码笔记(13)-可能是全网第一篇 Coil 的源码分析文章:[https://juejin.cn/post/6897872882051842061](https://juejin.cn/post/6897872882051842061)
|
||||
|
||||
【奇技淫巧】新的图片加载库?基于Kotlin协程的图片加载库——Coil:[https://juejin.cn/post/6844904159527829518](https://juejin.cn/post/6844904159527829518)
|
||||
|
||||
【奇技淫巧】新的图片加载库?基于Kotlin协程的图片加载库——Coil:[https://juejin.cn/post/6844904159527829518](https://juejin.cn/post/6844904159527829518)
|
||||
@ -4,28 +4,31 @@
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.*
|
||||
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'com.alibaba.arouter'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
apply plugin: 'dagger.hilt.android.plugin'
|
||||
plugins {
|
||||
alias(libs.plugins.application)
|
||||
alias(libs.plugins.kotlin)
|
||||
alias(libs.plugins.hilt)
|
||||
id "kotlin-kapt"
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdkVersion ProjectBuildConfig.compileSdkVersion
|
||||
namespace 'com.quyunshuo.androidbaseframemvvm'
|
||||
compileSdk ProjectBuildConfig.compileSdkVersion
|
||||
|
||||
defaultConfig {
|
||||
applicationId ProjectBuildConfig.applicationId
|
||||
minSdkVersion ProjectBuildConfig.minSdkVersion
|
||||
targetSdkVersion ProjectBuildConfig.targetSdkVersion
|
||||
minSdk ProjectBuildConfig.minSdkVersion
|
||||
targetSdk ProjectBuildConfig.targetSdkVersion
|
||||
versionCode ProjectBuildConfig.versionCode
|
||||
versionName ProjectBuildConfig.versionName
|
||||
|
||||
testInstrumentationRunner DependencyConfig.AndroidX.AndroidJUnitRunner
|
||||
multiDexKeepProguard file("multidexKeep.pro")
|
||||
|
||||
ndk {
|
||||
// 设置支持的SO库架构
|
||||
//abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
|
||||
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86','x86_64'
|
||||
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,14 +77,13 @@ android {
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
jvmTarget = '17'
|
||||
}
|
||||
namespace 'com.quyunshuo.androidbaseframemvvm'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
41
app/proguard-rules.pro
vendored
41
app/proguard-rules.pro
vendored
@ -1,27 +1,14 @@
|
||||
# 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
|
||||
|
||||
# 避免 ViewBinding 类被混淆导致反射初始化失败
|
||||
-keep public interface androidx.viewbinding.ViewBinding
|
||||
-keepclassmembers class * implements androidx.viewbinding.ViewBinding{
|
||||
*;
|
||||
}
|
||||
# AGP 8.x 警告生成
|
||||
# Please add these rules to your existing keep rules in order to suppress warnings.
|
||||
# This is generated automatically by the Android Gradle plugin.
|
||||
-dontwarn dalvik.system.VMStack
|
||||
-dontwarn javax.lang.model.element.Element
|
||||
-dontwarn org.bouncycastle.jsse.BCSSLParameters
|
||||
-dontwarn org.bouncycastle.jsse.BCSSLSocket
|
||||
-dontwarn org.bouncycastle.jsse.provider.BouncyCastleJsseProvider
|
||||
-dontwarn org.conscrypt.Conscrypt$Version
|
||||
-dontwarn org.conscrypt.Conscrypt
|
||||
-dontwarn org.conscrypt.ConscryptHostnameVerifier
|
||||
-dontwarn org.openjsse.javax.net.ssl.SSLParameters
|
||||
-dontwarn org.openjsse.javax.net.ssl.SSLSocket
|
||||
-dontwarn org.openjsse.net.ssl.OpenJSSE
|
||||
@ -4,11 +4,6 @@
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.*
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
apply plugin: 'dagger.hilt.android.plugin'
|
||||
|
||||
android {
|
||||
compileSdkVersion ProjectBuildConfig.compileSdkVersion
|
||||
|
||||
@ -28,12 +23,12 @@ android {
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||
jvmTarget = "17"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
|
||||
@ -4,22 +4,12 @@
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.*
|
||||
|
||||
if (ProjectBuildConfig.isAppMode) {
|
||||
apply plugin: 'com.android.application'
|
||||
} else {
|
||||
apply plugin: 'com.android.library'
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
apply plugin: 'dagger.hilt.android.plugin'
|
||||
|
||||
android {
|
||||
compileSdkVersion ProjectBuildConfig.compileSdkVersion
|
||||
compileSdk ProjectBuildConfig.compileSdkVersion
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion ProjectBuildConfig.minSdkVersion
|
||||
targetSdkVersion ProjectBuildConfig.targetSdkVersion
|
||||
minSdk ProjectBuildConfig.minSdkVersion
|
||||
targetSdk ProjectBuildConfig.targetSdkVersion
|
||||
versionCode ProjectBuildConfig.versionCode
|
||||
versionName ProjectBuildConfig.versionName
|
||||
testInstrumentationRunner DependencyConfig.AndroidX.AndroidJUnitRunner
|
||||
@ -32,12 +22,12 @@ android {
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
sourceCompatibility JavaVersion.VERSION_17
|
||||
targetCompatibility JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = JavaVersion.VERSION_1_8.toString()
|
||||
jvmTarget = "17"
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
|
||||
28
build.gradle
28
build.gradle
@ -1,28 +0,0 @@
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.*
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
maven { url 'https://maven.google.com' }
|
||||
}
|
||||
dependencies {
|
||||
classpath ProjectPluginManager.AndroidToolsPlugin
|
||||
classpath ProjectPluginManager.KotlinPlugin
|
||||
classpath ProjectPluginManager.ARouterRegister
|
||||
classpath ProjectPluginManager.HiltPlugin
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
google()
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
8
build.gradle.kts
Normal file
8
build.gradle.kts
Normal file
@ -0,0 +1,8 @@
|
||||
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
|
||||
plugins {
|
||||
alias(libs.plugins.application) apply false
|
||||
alias(libs.plugins.library) apply false
|
||||
alias(libs.plugins.kotlin) apply false
|
||||
alias(libs.plugins.hilt) apply false
|
||||
}
|
||||
true // Needed to make the Suppress annotation work for the plugins block
|
||||
@ -1,2 +1,2 @@
|
||||
plugins { `kotlin-dsl` }
|
||||
repositories { jcenter() }
|
||||
repositories { mavenCentral() }
|
||||
@ -22,8 +22,8 @@ object DependencyConfig {
|
||||
const val ConstraintLayout = "2.1.3" // 约束布局
|
||||
const val TestExtJunit = "1.1.2"
|
||||
const val TestEspresso = "3.3.0"
|
||||
const val ActivityKtx = "1.4.0"
|
||||
const val FragmentKtx = "1.4.1"
|
||||
const val ActivityKtx = "1.5.1"
|
||||
const val FragmentKtx = "1.5.2"
|
||||
const val MultiDex = "2.0.1"
|
||||
|
||||
// Android---------------------------------------------------------------
|
||||
@ -35,8 +35,8 @@ object DependencyConfig {
|
||||
const val Coroutines = "1.6.1" // 协程
|
||||
|
||||
// JetPack---------------------------------------------------------------
|
||||
const val Lifecycle = "2.4.1" // Lifecycle相关(ViewModel & LiveData & Lifecycle)
|
||||
const val Hilt = "2.38.1" // DI框架-Hilt
|
||||
const val Lifecycle = "2.4.1" // Lifecycle
|
||||
const val Hilt = "2.44" // DI框架-Hilt
|
||||
|
||||
// GitHub----------------------------------------------------------------
|
||||
const val OkHttp = "4.9.0" // OkHttp
|
||||
|
||||
@ -7,10 +7,10 @@ package com.quyunshuo.androidbaseframemvvm.buildsrc
|
||||
* @since 4/24/21 5:56 PM
|
||||
*/
|
||||
object ProjectBuildConfig {
|
||||
const val compileSdkVersion = 31
|
||||
const val compileSdkVersion = 33
|
||||
const val applicationId = "com.quyunshuo.androidbaseframemvvm"
|
||||
const val minSdkVersion = 21
|
||||
const val targetSdkVersion = 31
|
||||
const val targetSdkVersion = 33
|
||||
const val versionCode = 1
|
||||
const val versionName = "1.0"
|
||||
const val isAppMode = false
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.buildsrc
|
||||
|
||||
/**
|
||||
* 项目级插件管理
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 4/24/21 5:56 PM
|
||||
*/
|
||||
object ProjectPluginManager {
|
||||
const val AndroidToolsPlugin = "com.android.tools.build:gradle:7.2.0"
|
||||
const val KotlinPlugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
|
||||
const val ARouterRegister = "com.alibaba:arouter-register:1.0.2"
|
||||
const val HiltPlugin = "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
|
||||
}
|
||||
@ -18,4 +18,8 @@ android.useAndroidX=true
|
||||
# Automatically convert third-party libraries to use AndroidX
|
||||
android.enableJetifier=true
|
||||
# Kotlin code style for this project: "official" or "obsolete":
|
||||
kotlin.code.style=official
|
||||
kotlin.code.style=official
|
||||
android.defaults.buildfeatures.buildconfig=true
|
||||
android.nonTransitiveRClass=false
|
||||
android.nonFinalResIds=false
|
||||
android.enableR8.fullMode=false
|
||||
20
gradle/libs.versions.toml
Normal file
20
gradle/libs.versions.toml
Normal file
@ -0,0 +1,20 @@
|
||||
[versions]
|
||||
# plugin
|
||||
agp = "8.1.0"
|
||||
kotlin-android = "1.8.0"
|
||||
hilt = "2.44"
|
||||
|
||||
#lib
|
||||
|
||||
|
||||
[libraries]
|
||||
|
||||
|
||||
[plugins]
|
||||
application = { id = "com.android.application", version.ref = "agp" }
|
||||
library = { id = "com.android.library", version.ref = "agp" }
|
||||
kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin-android" }
|
||||
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
|
||||
|
||||
|
||||
[bundles]
|
||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
|
||||
|
||||
@ -2,8 +2,14 @@
|
||||
//********** lib_base 的配置文件 ***********
|
||||
//****************************************
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.library)
|
||||
alias(libs.plugins.kotlin)
|
||||
alias(libs.plugins.hilt)
|
||||
id "kotlin-kapt"
|
||||
}
|
||||
|
||||
apply from: '../base_lib.gradle'
|
||||
apply plugin: 'dagger.hilt.android.plugin'
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.*
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import android.app.Application
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ActivityStackManager
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ForegroundBackgroundHelper
|
||||
|
||||
/**
|
||||
* Activity生命周期监听
|
||||
@ -23,6 +24,7 @@ class ActivityLifecycleCallbacksImpl : Application.ActivityLifecycleCallbacks {
|
||||
|
||||
override fun onActivityStarted(activity: Activity) {
|
||||
Log.e(TAG, "${activity.javaClass.simpleName} --> onActivityStarted")
|
||||
ForegroundBackgroundHelper.onActivityStarted()
|
||||
}
|
||||
|
||||
override fun onActivityResumed(activity: Activity) {
|
||||
@ -35,6 +37,7 @@ class ActivityLifecycleCallbacksImpl : Application.ActivityLifecycleCallbacks {
|
||||
|
||||
override fun onActivityStopped(activity: Activity) {
|
||||
Log.e(TAG, "${activity.javaClass.simpleName} --> onActivityStopped")
|
||||
ForegroundBackgroundHelper.onActivityStopped()
|
||||
}
|
||||
|
||||
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
|
||||
|
||||
@ -2,6 +2,8 @@ package com.quyunshuo.androidbaseframemvvm.base.ktx
|
||||
|
||||
import android.app.Activity
|
||||
import android.view.WindowManager
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.Lifecycle
|
||||
|
||||
/**
|
||||
* 设置当前 [Activity] 是否允许截屏操作
|
||||
@ -15,4 +17,9 @@ fun Activity.isAllowScreenCapture(isAllow: Boolean) {
|
||||
} else {
|
||||
window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前 Activity 是否是 [Lifecycle.State.RESUMED]
|
||||
*/
|
||||
fun AppCompatActivity.isResumed(): Boolean = lifecycle.currentState == Lifecycle.State.RESUMED
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
package com.ad.newad
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
|
||||
/**
|
||||
* 网络相关工具类
|
||||
*/
|
||||
object NetUtils {
|
||||
|
||||
/**
|
||||
* 当前网络是否是 Wi-Fi
|
||||
*/
|
||||
fun currentNetIsWiFi(context: Context): Boolean {
|
||||
val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val networkCapabilities =
|
||||
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
|
||||
return networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ?: false
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,7 @@ import android.os.Looper
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.androidbaseframemvvm.base.R
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.*
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.network.AutoRegisterNetListener
|
||||
@ -22,12 +23,10 @@ import me.jessyan.autosize.AutoSizeCompat
|
||||
abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : AppCompatActivity(),
|
||||
FrameView<VB>, NetworkStateChangeListener {
|
||||
|
||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||
BindingReflex.reflexViewBinding(javaClass, layoutInflater)
|
||||
}
|
||||
|
||||
protected abstract val mViewModel: VM
|
||||
|
||||
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { createVB() }
|
||||
|
||||
/**
|
||||
* 是否有 [RegisterEventBus] 注解,避免重复调用 [Class.isAnnotation]
|
||||
*/
|
||||
@ -80,7 +79,7 @@ abstract class BaseFrameActivity<VB : ViewBinding, VM : BaseViewModel> : AppComp
|
||||
* @return Unit
|
||||
*/
|
||||
override fun networkConnectChange(isConnected: Boolean) {
|
||||
toast(if (isConnected) "网络已连接" else "网络已断开")
|
||||
toast(if (isConnected) getString(R.string.base_network_connected) else getString(R.string.base_network_disconnected))
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
@ -8,7 +8,6 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.BindingReflex
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
|
||||
|
||||
@ -40,7 +39,7 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : BaseViewModel> : Fragmen
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
_binding = BindingReflex.reflexViewBinding(javaClass, layoutInflater)
|
||||
_binding = createVB()
|
||||
return _binding?.root
|
||||
}
|
||||
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.mvvm.v
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.status.ViewStatusHelper
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/8/5 <p>
|
||||
* - 文件描述 : 基础状态管理Activity
|
||||
*/
|
||||
open class BaseFrameStatusActivity : AppCompatActivity() {
|
||||
/**
|
||||
* 基础状态管理帮助类
|
||||
*/
|
||||
private var mBaseStatusHelper: ViewStatusHelper? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
//注册状态帮助类
|
||||
mBaseStatusHelper = onRegisterStatusHelper()
|
||||
//恢复状态数据
|
||||
mBaseStatusHelper?.onRestoreInstanceStatus(savedInstanceState)
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存状态
|
||||
*/
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
mBaseStatusHelper?.onSaveInstanceState(outState)
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册状态管理帮助类,子类重写此方法以注册帮助类。
|
||||
* 每一层都有可能有自己的状态管理帮助类,所以继承重写的时候,需要将super的对象传入自己的帮助类构造函数中
|
||||
*/
|
||||
protected open fun onRegisterStatusHelper(): ViewStatusHelper? {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.mvvm.v
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.status.ViewStatusHelper
|
||||
|
||||
/**
|
||||
* @author DBoy 2021/8/5 <p>
|
||||
* - 文件描述 : 基础状态管理Fragment
|
||||
*/
|
||||
open class BaseFrameStatusFragment : Fragment() {
|
||||
/**
|
||||
* 基础状态管理帮助类
|
||||
*/
|
||||
private var mBaseStatusHelper: ViewStatusHelper? = null
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
//注册状态帮助类
|
||||
mBaseStatusHelper = onRegisterStatusHelper()
|
||||
//恢复状态数据
|
||||
mBaseStatusHelper?.onRestoreInstanceStatus(savedInstanceState)
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存状态
|
||||
*/
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
mBaseStatusHelper?.onSaveInstanceState(outState)
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册状态管理帮助类,子类重写此方法以注册帮助类。
|
||||
* 每一层都有可能有自己的状态管理帮助类,所以继承重写的时候,需要将super的对象传入自己的帮助类构造函数中
|
||||
*/
|
||||
protected open fun onRegisterStatusHelper(): ViewStatusHelper? {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.mvvm.v
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.viewbinding.ViewBinding
|
||||
|
||||
/**
|
||||
@ -11,12 +12,17 @@ import androidx.viewbinding.ViewBinding
|
||||
interface FrameView<VB : ViewBinding> {
|
||||
|
||||
/**
|
||||
* 初始化View
|
||||
* 创建 [ViewBinding] 实例
|
||||
*/
|
||||
fun createVB(): VB
|
||||
|
||||
/**
|
||||
* 初始化 View
|
||||
*/
|
||||
fun VB.initView()
|
||||
|
||||
/**
|
||||
* 订阅LiveData
|
||||
* 订阅 [LiveData]
|
||||
*/
|
||||
fun initObserve()
|
||||
|
||||
|
||||
@ -1,156 +0,0 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.ViewModelStoreOwner
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.lang.reflect.ParameterizedType
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* @author DBoy
|
||||
* @date 2020/9/29
|
||||
* Class 描述 : 用于反射获取 ViewModel 和 ViewBinding
|
||||
*/
|
||||
object BindingReflex {
|
||||
/**
|
||||
* 反射获取ViewBinding
|
||||
*
|
||||
* @param <V> ViewBinding 实现类
|
||||
* @param aClass 当前类
|
||||
* @param from layouinflater
|
||||
* @return viewBinding实例
|
||||
</V> */
|
||||
fun <V : ViewBinding> reflexViewBinding(aClass: Class<*>, from: LayoutInflater?): V {
|
||||
try {
|
||||
val actualTypeArguments =
|
||||
(Objects.requireNonNull(aClass.genericSuperclass) as ParameterizedType).actualTypeArguments
|
||||
for (i in actualTypeArguments.indices) {
|
||||
val tClass: Class<Any>
|
||||
try {
|
||||
tClass = actualTypeArguments[i] as Class<Any>
|
||||
} catch (e: Exception) {
|
||||
continue
|
||||
}
|
||||
if (ViewBinding::class.java.isAssignableFrom(tClass)) {
|
||||
val inflate = tClass.getMethod("inflate", LayoutInflater::class.java)
|
||||
return inflate.invoke(null, from) as V
|
||||
}
|
||||
}
|
||||
return reflexViewBinding<V>(aClass.superclass, from)
|
||||
} catch (e: NoSuchMethodException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: IllegalAccessException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: InvocationTargetException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
throw RuntimeException("ViewBinding初始化失败")
|
||||
}
|
||||
|
||||
/**
|
||||
* 反射获取ViewBinding\
|
||||
*/
|
||||
fun <V : ViewBinding> reflexViewBinding(
|
||||
aClass: Class<*>,
|
||||
from: LayoutInflater?,
|
||||
viewGroup: ViewGroup?,
|
||||
b: Boolean
|
||||
): V {
|
||||
try {
|
||||
val actualTypeArguments =
|
||||
(Objects.requireNonNull(aClass.genericSuperclass) as ParameterizedType).actualTypeArguments
|
||||
for (i in actualTypeArguments.indices) {
|
||||
val tClass: Class<Any>
|
||||
try {
|
||||
tClass = actualTypeArguments[i] as Class<Any>
|
||||
} catch (e: Exception) {
|
||||
continue
|
||||
}
|
||||
if (ViewBinding::class.java.isAssignableFrom(tClass)) {
|
||||
val inflate = tClass.getDeclaredMethod(
|
||||
"inflate",
|
||||
LayoutInflater::class.java,
|
||||
ViewGroup::class.java,
|
||||
Boolean::class.javaPrimitiveType
|
||||
)
|
||||
return inflate.invoke(null, from, viewGroup, b) as V
|
||||
}
|
||||
}
|
||||
return reflexViewBinding<ViewBinding>(aClass.superclass, from, viewGroup, b) as V
|
||||
} catch (e: NoSuchMethodException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: IllegalAccessException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: InvocationTargetException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
throw RuntimeException("ViewBinding初始化失败")
|
||||
}
|
||||
|
||||
/**
|
||||
* 反射获取ViewModel
|
||||
*
|
||||
* @param <VM> ViewModel实现类
|
||||
* @param aClass 当前class
|
||||
* @param owner 生命周期管理
|
||||
* @return ViewModel实例
|
||||
</VM> */
|
||||
fun <VM : ViewModel> reflexViewModel(aClass: Class<*>, owner: ViewModelStoreOwner): VM {
|
||||
try {
|
||||
val actualTypeArguments =
|
||||
(Objects.requireNonNull(aClass.genericSuperclass) as ParameterizedType).actualTypeArguments
|
||||
for (i in actualTypeArguments.indices) {
|
||||
val tClass: Class<Any>
|
||||
try {
|
||||
tClass = actualTypeArguments[i] as Class<Any>
|
||||
} catch (e: Exception) {
|
||||
continue
|
||||
}
|
||||
if (ViewModel::class.java.isAssignableFrom(tClass)) {
|
||||
return ViewModelProvider(owner)[tClass as Class<VM>]
|
||||
}
|
||||
}
|
||||
return reflexViewModel<VM>(aClass.superclass, owner)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
throw RuntimeException("ViewModel初始化失败")
|
||||
}
|
||||
|
||||
/**
|
||||
* 反射获取ViewModel,这个方法只提供给fragment使用.
|
||||
* 如果fragment的父Activity有相同的ViewModel 那么生成的ViewModel将会是同一个实例,做到Fragment与Activity的数据同步,
|
||||
* 或者说是同一个Activity中的多个Fragment同步使用用一个ViewModel达到数据之间的同步。
|
||||
*
|
||||
* @param <VM> ViewModel实现类
|
||||
* @param aClass 当前class
|
||||
* @param fragment fragment 调用 [Fragment.requireActivity] 方法
|
||||
* @return ViewModel实例
|
||||
</VM> */
|
||||
fun <VM : ViewModel> reflexViewModelShared(aClass: Class<*>, fragment: Fragment): VM {
|
||||
try {
|
||||
val actualTypeArguments =
|
||||
(Objects.requireNonNull(aClass.genericSuperclass) as ParameterizedType).actualTypeArguments
|
||||
for (i in actualTypeArguments.indices) {
|
||||
val tClass: Class<Any>
|
||||
try {
|
||||
tClass = actualTypeArguments[i] as Class<Any>
|
||||
} catch (e: Exception) {
|
||||
continue
|
||||
}
|
||||
if (ViewModel::class.java.isAssignableFrom(tClass)) {
|
||||
return ViewModelProvider(fragment.requireActivity())[tClass as Class<VM>]
|
||||
}
|
||||
}
|
||||
return reflexViewModelShared<VM>(aClass.superclass, fragment)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
throw RuntimeException("ViewModel初始化失败")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
|
||||
|
||||
/**
|
||||
* 剪切板工具类
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2023/5/31 10:27
|
||||
*/
|
||||
object ClipboardUtils {
|
||||
|
||||
/**
|
||||
* 复制内容到剪切板
|
||||
*
|
||||
* @param text String 内容
|
||||
* @param label String 标签,用于区分内容
|
||||
*/
|
||||
fun copyToClipboard(text: String, label: String = "") {
|
||||
val clipboard =
|
||||
BaseApplication.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clip = ClipData.newPlainText(label, text)
|
||||
clipboard.setPrimaryClip(clip)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
/**
|
||||
* 前后台切换帮助类,该类实现了前后台监听以及支持注册变化响应监听
|
||||
*
|
||||
* @see ForegroundBackgroundObserver
|
||||
* @see ForegroundBackgroundSubject
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2023/5/31 14:22
|
||||
*/
|
||||
object ForegroundBackgroundHelper : ForegroundBackgroundSubject {
|
||||
|
||||
private var mActivityStartCount = 0
|
||||
|
||||
private var mIsForeground = false
|
||||
|
||||
private val mObservers = mutableListOf<ForegroundBackgroundObserver>()
|
||||
|
||||
fun onActivityStarted() {
|
||||
mActivityStartCount++
|
||||
if (mActivityStartCount == 1) {
|
||||
mIsForeground = true
|
||||
notifyObservers()
|
||||
}
|
||||
}
|
||||
|
||||
fun onActivityStopped() {
|
||||
mActivityStartCount--
|
||||
if (mActivityStartCount == 0) {
|
||||
mIsForeground = false
|
||||
notifyObservers()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通知所有订阅者状态变化
|
||||
*/
|
||||
override fun notifyObservers() {
|
||||
mObservers.forEach {
|
||||
it.foregroundBackgroundNotify(mIsForeground)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加订阅者
|
||||
*
|
||||
* @param observer ForegroundBackgroundObserver
|
||||
*/
|
||||
override fun addObserve(observer: ForegroundBackgroundObserver) {
|
||||
mObservers.add(observer)
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除订阅者
|
||||
*
|
||||
* @param observer ForegroundBackgroundObserver
|
||||
*/
|
||||
override fun removeObserver(observer: ForegroundBackgroundObserver) {
|
||||
mObservers.remove(observer)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅者需要实现的接口
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2023/5/31 14:23
|
||||
*/
|
||||
interface ForegroundBackgroundObserver {
|
||||
fun foregroundBackgroundNotify(isForeground: Boolean)
|
||||
}
|
||||
|
||||
/**
|
||||
* 被观察者抽象主题
|
||||
*
|
||||
* @author Qu Yunshuo
|
||||
* @since 2023/5/31 14:24
|
||||
*/
|
||||
interface ForegroundBackgroundSubject {
|
||||
fun notifyObservers()
|
||||
fun addObserve(observer: ForegroundBackgroundObserver)
|
||||
fun removeObserver(observer: ForegroundBackgroundObserver)
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication.Companion.application as app
|
||||
|
||||
fun getString(@StringRes stringRes: Int): String {
|
||||
return app.getString(stringRes)
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,46 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.StringRes
|
||||
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
|
||||
|
||||
private val mToastHandler by lazy { Handler(Looper.getMainLooper()) }
|
||||
|
||||
private var mToast: Toast? = null
|
||||
|
||||
@JvmOverloads
|
||||
fun toast(text: String, duration: Int = Toast.LENGTH_SHORT) {
|
||||
postToast(text, duration)
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
fun toast(@StringRes id: Int, duration: Int = Toast.LENGTH_SHORT) {
|
||||
postToast(getString(id), duration)
|
||||
}
|
||||
|
||||
private fun postToast(text: String, duration: Int) {
|
||||
mToastHandler.post {
|
||||
setToast(text, duration)
|
||||
mToast?.show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun setToast(text: String, duration: Int) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(BaseApplication.context, text, duration)
|
||||
} else {
|
||||
mToast?.duration = duration
|
||||
mToast?.setText(text)
|
||||
}
|
||||
} else {
|
||||
if (mToast != null) {
|
||||
mToast?.cancel()
|
||||
mToast = null
|
||||
}
|
||||
mToast = Toast.makeText(BaseApplication.context, text, duration)
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
package com.quyunshuo.androidbaseframemvvm.base.utils
|
||||
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.alibaba.android.arouter.launcher.ARouter
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
@ -41,23 +40,4 @@ fun sendEvent(event: Any) = EventBusUtils.postEvent(event)
|
||||
*/
|
||||
fun aRouterJump(routerUrl: String) {
|
||||
ARouter.getInstance().build(routerUrl).navigation()
|
||||
}
|
||||
|
||||
/**************************************************************************************************/
|
||||
/**
|
||||
* toast
|
||||
* @param msg String 文案
|
||||
* @param duration Int 时间
|
||||
*/
|
||||
fun toast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
|
||||
ToastUtils.showToast(msg, duration)
|
||||
}
|
||||
|
||||
/**
|
||||
* toast
|
||||
* @param msgId Int String资源ID
|
||||
* @param duration Int 时间
|
||||
*/
|
||||
fun toast(msgId: Int, duration: Int = Toast.LENGTH_SHORT) {
|
||||
ToastUtils.showToast(msgId, duration)
|
||||
}
|
||||
4
lib_base/src/main/res/values/strings.xml
Normal file
4
lib_base/src/main/res/values/strings.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<resources>
|
||||
<string name="base_network_disconnected">网路已断开</string>
|
||||
<string name="base_network_connected">网络已连接</string>
|
||||
</resources>
|
||||
@ -2,6 +2,13 @@
|
||||
//********* lib_common 的配置文件 **********
|
||||
//****************************************
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.library)
|
||||
alias(libs.plugins.kotlin)
|
||||
alias(libs.plugins.hilt)
|
||||
id "kotlin-kapt"
|
||||
}
|
||||
|
||||
apply from: '../base_lib.gradle'
|
||||
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.*
|
||||
|
||||
@ -9,6 +9,7 @@ import com.google.auto.service.AutoService
|
||||
import com.quyunshuo.androidbaseframemvvm.base.app.ApplicationLifecycle
|
||||
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
|
||||
import com.quyunshuo.androidbaseframemvvm.base.constant.VersionStatus
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ForegroundBackgroundObserver
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.ProcessUtils
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
|
||||
import com.quyunshuo.androidbaseframemvvm.base.utils.network.NetworkStateClient
|
||||
@ -24,7 +25,7 @@ import com.tencent.smtt.sdk.QbSdk.PreInitCallback
|
||||
* @since 4/16/21 3:37 PM
|
||||
*/
|
||||
@AutoService(ApplicationLifecycle::class)
|
||||
class CommonApplication : ApplicationLifecycle {
|
||||
class CommonApplication : ApplicationLifecycle, ForegroundBackgroundObserver {
|
||||
|
||||
companion object {
|
||||
// 全局CommonApplication
|
||||
@ -146,4 +147,8 @@ class CommonApplication : ApplicationLifecycle {
|
||||
)
|
||||
return "Bugly -->> init complete"
|
||||
}
|
||||
|
||||
override fun foregroundBackgroundNotify(isForeground: Boolean) {
|
||||
Log.d("ForegroundBackground", "isForeground: $isForeground")
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,20 @@
|
||||
import com.quyunshuo.androidbaseframemvvm.buildsrc.ProjectBuildConfig
|
||||
|
||||
//****************************************
|
||||
//******** module_home 的配置文件 *********
|
||||
//****************************************
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.kotlin)
|
||||
alias(libs.plugins.hilt)
|
||||
id "kotlin-kapt"
|
||||
}
|
||||
if (ProjectBuildConfig.isAppMode) {
|
||||
apply plugin: 'com.android.application'
|
||||
} else {
|
||||
apply plugin: 'com.android.library'
|
||||
}
|
||||
|
||||
apply from: '../base_module.gradle'
|
||||
|
||||
android {
|
||||
|
||||
@ -21,6 +21,8 @@ class MainActivity : BaseActivity<HomeActivityMainBinding, HomeViewModel>() {
|
||||
*/
|
||||
override val mViewModel by viewModels<HomeViewModel>()
|
||||
|
||||
override fun createVB() = HomeActivityMainBinding.inflate(layoutInflater)
|
||||
|
||||
override fun HomeActivityMainBinding.initView() {}
|
||||
|
||||
override fun initObserve() {
|
||||
|
||||
@ -1,3 +1,24 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
maven { url = java.net.URI.create("https://maven.google.com") }
|
||||
}
|
||||
}
|
||||
|
||||
dependencyResolutionManagement {
|
||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
maven { url = java.net.URI.create("https://maven.google.com") }
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.name = "AndroidBaseFrameMVVM"
|
||||
|
||||
include(
|
||||
":app",
|
||||
":lib_base",
|
||||
|
||||
Reference in New Issue
Block a user