Skip to content

Commit

Permalink
优化viewmodel获取方式,优化列表赋值重复代码
Browse files Browse the repository at this point in the history
  • Loading branch information
hegaojian committed Jun 10, 2020
1 parent 93ebb21 commit 46f09de
Show file tree
Hide file tree
Showing 61 changed files with 1,263 additions and 1,399 deletions.
16 changes: 9 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "me.hgj.jetpackmvvm.demo"
minSdkVersion 21
targetSdkVersion 29
versionCode 5
versionName "1.0.5"
versionCode 6
versionName "1.0.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
/* ndk {
// 设置支持的SO库架构 一般只设置这个就阔以设配所有的设备了,还可以减少apk的大小
Expand Down Expand Up @@ -70,7 +70,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.core:core-ktx:1.3.0'
//test
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand All @@ -80,7 +80,7 @@ dependencies {
//androidx UI
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'com.google.android.material:material:1.3.0-alpha01'
implementation 'androidx.cardview:cardview:1.0.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
implementation 'androidx.preference:preference:1.1.1'
Expand All @@ -100,7 +100,7 @@ dependencies {
implementation 'com.tencent:mmkv:1.0.22'
//项目核心框架
implementation project(path: ':JetpackMvvm')
// implementation 'me.hegj:JetpackMvvm:1.1.0'
// implementation 'me.hegj:JetpackMvvm:1.1.1'
//轮播图
implementation 'com.github.zhpanvip:BannerViewPager:2.6.6'
//BaseAdapter
Expand All @@ -114,14 +114,16 @@ dependencies {
//官方的
implementation 'com.google.android:flexbox:2.0.1'
//加载webview
implementation 'com.just.agentweb:agentweb:4.1.2'
implementation 'com.just.agentweb:agentweb:4.1.3'
implementation 'com.just.agentweb:filechooser:4.1.3'
implementation 'com.download.library:Downloader:4.1.3'
//Bugly
implementation'com.tencent.bugly:crashreport_upgrade:1.4.5'
implementation'com.tencent.bugly:nativecrashreport:3.7.1'
//glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
//屏幕适配
implementation 'me.jessyan:autosize:1.2.0'
implementation 'me.jessyan:autosize:1.2.1'
//防崩溃
implementation 'cat.ereza:customactivityoncrash:2.3.0'

Expand Down
973 changes: 625 additions & 348 deletions app/priguardMapping.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
-keep class com.just.agentweb.** {
*;
}

-dontwarn com.just.agentweb.**

-keepattributes *Annotation*
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package="me.hgj.jetpackmvvm.demo">

<uses-sdk tools:overrideLibrary="me.hgj.jetpackmvvm" />

<application
android:name=".app.App"
android:allowBackup="true"
Expand All @@ -20,7 +21,6 @@
android:theme="@style/FullscreenThemse">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand All @@ -44,7 +44,7 @@
android:value="375" />
<meta-data
android:name="design_height_in_dp"
android:value="100000" />
android:value="667" />
</application>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ abstract class BaseFragment<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDb
/**
* 懒加载 只有当前fragment视图显示时才会触发该方法
*/
abstract override fun lazyLoadData()
override fun lazyLoadData(){}

/**
* 创建LiveData观察者 懒加载之后才会触发
* 创建LiveData观察者 Fragment执行onViewCreated后触发
*/
override fun createObserver() {}


/**
* Fragment执行onViewCreated后触发的方法
* Fragment执行onViewCreated后触发
*/
override fun initData() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,17 @@ import me.hgj.jetpackmvvm.demo.data.model.bean.UserInfo
* 比如 全局可使用的 地理位置信息,账户信息,App的基本配置等等,
*/
class AppViewModel(app: Application) : BaseViewModel(app) {

//是否是第一次启动
var isFirst =
UnPeekNotNullLiveData<Boolean>()

//是否已经登录过
var isLogin =
UnPeekNotNullLiveData<Boolean>()

//App的账户信息 使用UnPeekLiveData 因为账户信息有可能为空
var userinfo =
UnPeekLiveData<UserInfo>()
var userinfo = UnPeekLiveData<UserInfo>()

//App主题颜色 中大型项目不推荐以这种方式改变主题颜色,比较繁琐耦合,且容易有遗漏某些控件没有设置主题色
var appColor =
UnPeekNotNullLiveData<Int>()
var appColor = UnPeekNotNullLiveData<Int>()

//App 列表动画
var appAnimation =
UnPeekNotNullLiveData<Int>()
var appAnimation = UnPeekNotNullLiveData<Int>()

init {
//初始化
isFirst.value = CacheUtil.isFirst()
//初始化
isLogin.value = CacheUtil.isLogin()
//默认值保存的账户信息,没有登陆过则为null
userinfo.value = CacheUtil.getUser()
//默认值颜色
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package me.hgj.jetpackmvvm.demo.app.event

import android.app.Application
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.livedata.UnPeekLiveData
import me.hgj.jetpackmvvm.callback.livedata.event.EventLiveData
import me.hgj.jetpackmvvm.demo.data.model.bean.CollectBus

/**
Expand All @@ -13,14 +13,12 @@ import me.hgj.jetpackmvvm.demo.data.model.bean.CollectBus
class EventViewModel(app: Application) : BaseViewModel(app) {

//全局收藏,在任意一个地方收藏或取消收藏,监听该值的界面都会收到消息
var collect =
UnPeekLiveData<CollectBus>()
val collectEvent = EventLiveData<CollectBus>()

//分享文章通知
var shareArticle =
UnPeekLiveData<Boolean>()
val shareArticleEvent = EventLiveData<Boolean>()

//添加toto通知
var addTodo = UnPeekLiveData<Boolean>()
//添加TODO通知
val todoEvent = EventLiveData<Boolean>()

}
4 changes: 1 addition & 3 deletions app/src/main/java/me/hgj/jetpackmvvm/demo/app/ext/AppExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ fun NavController.jumpByLogin(action: (NavController) -> Unit) {
if (CacheUtil.isLogin()) {
action(this)
} else {
//注意一下,这里我是确定我所有的拦截登录都是在MainFragment中的,所以我可以写死,但是如果不在MainFragment中时跳转,你会报错,
//当然你也可以执行下面那个方法 自己写跳转
this.navigate(R.id.action_mainFragment_to_loginFragment)
this.navigate(R.id.action_to_loginFragment)
}
}

Expand Down
Loading

0 comments on commit 46f09de

Please sign in to comment.