Skip to content

Commit

Permalink
打包MVVM模块
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjianxiandev committed Jun 8, 2020
1 parent 4f9d8e1 commit a770889
Show file tree
Hide file tree
Showing 42 changed files with 1,600 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
kapt "androidx.room:room-compiler:2.2.5"
debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'
support.each { k, v -> implementation v }
implementation project(':mvvm')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' // Add this line
// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Feb 18 15:10:19 CST 2020
#Mon Jun 08 08:08:19 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
1 change: 1 addition & 0 deletions mvvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions mvvm/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.wangjianxiandev'

def rootAndroidId = rootProject.ext.android
def support = rootProject.ext.dependencies

android {
compileSdkVersion rootAndroidId.compileSdkVersion
buildToolsVersion rootAndroidId.buildToolsVersion
defaultConfig {
minSdkVersion rootAndroidId.minSdkVersion
targetSdkVersion rootAndroidId.targetSdkVersion
versionCode rootAndroidId.versionCode
versionName rootAndroidId.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
//设置支持的SO库架构
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
dataBinding {
enabled = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'
support.each { k, v -> implementation v }
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
Empty file added mvvm/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions mvvm/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.wjx.android.mvvm

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.wjx.android.mvvm.test", appContext.packageName)
}
}
5 changes: 5 additions & 0 deletions mvvm/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wjx.android.mvvm">

/
</manifest>
30 changes: 30 additions & 0 deletions mvvm/src/main/java/com/wjx/android/mvvm/BaseApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.wjx.android.mvvm

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import com.kingja.loadsir.core.LoadSir
import com.wjx.android.wanandroidmvvm.common.utils.Constant
import com.wjx.android.wanandroidmvvm.common.utils.SPreference

/**
* Created with Android Studio.
* Description:
* @author: Wangjianxian
* @CreateDate: 2020/6/8 8:28
*/
open class BaseApplication : Application() {
companion object {
lateinit var instance : BaseApplication
}
override fun onCreate() {
super.onCreate()
instance = this
SPreference.setContext(applicationContext)
initMode()
}

private fun initMode() {
var isNightMode: Boolean by SPreference(Constant.NIGHT_MODE, false)
AppCompatDelegate.setDefaultNightMode(if (isNightMode) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.wjx.android.mvvm.common.permission

import android.content.pm.PackageManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData

/**
* Created with Android Studio.
* Description:
* @author: Wangjianxian
* @CreateDate: 2020/4/27 18:56
*/
internal class PermissionFragment : Fragment() {
lateinit var liveData: MutableLiveData<PermissionResult>

private val PERMISSION_REQUEST_CODE = 100

// 非中断式的保存
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}

fun requestPermissions(permissions: Array<out String>) {
liveData = MutableLiveData()
requestPermissions(permissions, PERMISSION_REQUEST_CODE)
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == PERMISSION_REQUEST_CODE) {
val denyPermission = ArrayList<String>()
val rationalePermission = ArrayList<String>()
for ((index, value) in grantResults.withIndex()) {
if (value == PackageManager.PERMISSION_DENIED) {
if (shouldShowRequestPermissionRationale(permissions[index])) {
rationalePermission.add(permissions[index])
} else {
denyPermission.add(permissions[index])
}
}
}
if (denyPermission.isEmpty() && rationalePermission.isEmpty()) {
liveData.value = PermissionResult.Grant
} else {
if (rationalePermission.isNotEmpty()) {
liveData.value = PermissionResult.Rationale(rationalePermission.toTypedArray())
} else if (denyPermission.isNotEmpty()) {
liveData.value = PermissionResult.Deny(denyPermission.toTypedArray())
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wjx.android.mvvm.common.permission

/**
* Created with Android Studio.
* Description: 密封类实现枚举类型
* @author: Wangjianxian
* @CreateDate: 2020/4/27 18:54
*/
sealed class PermissionResult {
object Grant : PermissionResult()
class Deny(val permissions: Array<String>) : PermissionResult()
class Rationale(val permissions: Array<String>) : PermissionResult()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.wjx.android.mvvm.common.permission

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.MutableLiveData

/**
* Created with Android Studio.
* Description:
* @author: Wangjianxian
* @CreateDate: 2020/4/27 19:04
*/
class Permissions {

@Volatile
private var permissionFragment: PermissionFragment? = null

private fun getInstance(fragmentManager: FragmentManager) =
permissionFragment ?: synchronized(this) {
permissionFragment
?: if (fragmentManager.findFragmentByTag(TAG) == null) PermissionFragment().run {
fragmentManager.beginTransaction().add(this, TAG).commitNow()
this
} else fragmentManager.findFragmentByTag(TAG) as PermissionFragment
}

companion object {
const val TAG = "permissions"
}

constructor(activity: AppCompatActivity) {
permissionFragment = getInstance(activity.supportFragmentManager)
}

constructor(fragment: Fragment) {
permissionFragment = getInstance(fragment.childFragmentManager)
}

fun request(vararg permissions: String): MutableLiveData<PermissionResult> {
return this.requestArray(permissions)
}

fun requestArray(permissions: Array<out String>): MutableLiveData<PermissionResult> {
permissionFragment!!.requestPermissions(permissions)
return permissionFragment!!.liveData
}
}
12 changes: 12 additions & 0 deletions mvvm/src/main/java/com/wjx/android/mvvm/common/state/State.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wjx.android.mvvm.common.state

import androidx.annotation.StringRes

/**
* Created with Android Studio.
* Description: 状态类
* @author: Wangjianxian
* @date: 2020/02/22
* Time: 15:28
*/
data class State(var code : StateType, var message : String = "", @StringRes var tip : Int = 0)
13 changes: 13 additions & 0 deletions mvvm/src/main/java/com/wjx/android/mvvm/common/state/StateType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wjx.android.mvvm.common.state

/**
* 应用状态
*/
enum class StateType {
SUCCESS,
ERROR,
EMPTY,
NETWORK_ERROR,
LOADING,
TIP,
}
47 changes: 47 additions & 0 deletions mvvm/src/main/java/com/wjx/android/mvvm/common/utils/AppManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.wjx.android.mvvm.common.utils

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import java.util.*
import kotlin.system.exitProcess

/**
* Created with Android Studio.
* Description: Activity管理类
* @author: Wangjianxian
* @date: 2020/02/25
* Time: 19:05
*/

class AppManager {
private val activityStack : Stack<Activity> = Stack()

companion object {
val instance by lazy {
AppManager()
}
}

fun addActivity(activity: Activity) {
activityStack.add(activity)
}

fun removeActivity(activity: Activity) {
activityStack.remove(activity)
}

private fun finishAllActivity() {
for(activity in activityStack) {
activity.finish()
}
activityStack.clear()
}

fun exitApp(context: Context) {
finishAllActivity()
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
activityManager.killBackgroundProcesses(context.packageName)
exitProcess(0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.wjx.android.mvvm.common.utils

import org.greenrobot.eventbus.EventBus

/**
* Created with Android Studio.
* Description:
* @author: Wangjianxian
* @date: 2020/03/27
* Time: 18:46
*/
class ChangeThemeEvent {
fun post() {
EventBus.getDefault().post(this)
}
}
Loading

0 comments on commit a770889

Please sign in to comment.