Skip to content

Commit

Permalink
'代码review'
Browse files Browse the repository at this point in the history
  • Loading branch information
oooo7777777 committed Apr 26, 2021
1 parent d7f8e0c commit 979284d
Show file tree
Hide file tree
Showing 39 changed files with 405 additions and 348 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 300
versionName "3.0.0"
versionCode 310
versionName "3.1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/v/demo/EventBusDemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ class EventBusDemoActivity : BaseActivity<ActivityEventBusDemoBinding, BlankView
override fun onClick(v: View) {
when (v.id) {
mViewBinding.bt0.id -> {

getApplicationViewModel(
mContext.application,
AppViewModel::class.java
).string.postValue("我是全局数据" + randomNumber(99))
).string.postValue("我是全局数据" + 99.randomNumber())

}
mViewBinding.bt1.id -> {
LiveDataBus.with<String>(ConstData.CONTENT)
.postData("模拟eventBus" + randomNumber(99))
.postData("模拟eventBus" + 99.randomNumber())
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/v/demo/FourFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.v.demo

import android.view.View
import com.v.base.BaseFragment
import com.v.base.utils.getApplicationViewModel
import com.v.demo.databinding.FragmentFourBinding
import com.v.demo.model.AppViewModel
import com.v.demo.model.DataViewModel

/**
Expand All @@ -23,7 +21,6 @@ class FourFragment : BaseFragment<FragmentFourBinding, DataViewModel>(), View.On
mViewBinding.tvContent.text = it
})


}

override fun onClick(v: View) {
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/v/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.fragment.app.Fragment
import com.v.base.BaseActivity
import com.v.base.BlankViewModel
import com.v.base.utils.getFragment
import com.v.base.utils.otherwise
import com.v.base.utils.yes
import com.v.demo.databinding.MainActivityBinding
import com.v.demo.view.IndicatorZoom
import net.lucode.hackware.magicindicator.ViewPagerHelper
Expand All @@ -25,9 +27,11 @@ class MainActivity : BaseActivity<MainActivityBinding, BlankViewModel>() {
if (it.value) {
permissionsCount++
}
if (permissionsCount == permissions.size) {
(permissionsCount == permissions.size).yes {
//权限全部申请成功
}
}.otherwise {
//部分权限申请失败
}
}
}

Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/com/v/demo/NetworkActivity.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package com.v.demo

import com.v.base.BaseActivity
import android.view.View
import androidx.lifecycle.Observer
import com.v.base.utils.linear
import com.v.base.utils.loadData
import com.v.base.utils.toJson
import com.v.demo.adapter.NetworkActivityAdapter
import com.v.demo.model.NetworkViewModel
import com.v.base.BaseActivity
import com.v.demo.databinding.ActivityNetworkBinding
import com.v.demo.R
import com.v.demo.bean.GirlBean
import com.v.demo.model.NetworkViewModel

/**
* author :
Expand Down
8 changes: 0 additions & 8 deletions app/src/main/java/com/v/demo/OneFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class OneFragment : BaseFragment<FragmentOneBinding, DemoViewModel>() {
override fun initData() {
mAdapter.setHeaderView(mAdapterHeaderView.root)
mViewBinding.refreshLayout.autoRefresh()
// setupViewPager()
mViewModel.getList(page)
}

Expand Down Expand Up @@ -105,11 +104,4 @@ class OneFragment : BaseFragment<FragmentOneBinding, DemoViewModel>() {
}


// private fun setupViewPager() {
// mViewPager = mAdapterHeaderView.bannerViewPager as BannerViewPager<BannerBean>
// mViewPager.apply {
// adapter = BannerAdapter()
// setLifecycleRegistry(lifecycle)
// }.create()
// }
}
3 changes: 1 addition & 2 deletions app/src/main/java/com/v/demo/ThreeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import com.v.base.BaseFragment
import com.v.base.dialog.HintDialog
import com.v.base.dialog.ImgSelectDialog
import com.v.base.dialog.ListDialog
import com.v.base.utils.*
import com.v.base.utils.goActivity
import com.v.demo.databinding.FragmentThreeBinding
import com.v.demo.model.AppViewModel
import com.v.demo.model.DataViewModel
import java.io.File

Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/com/v/demo/TwoFragment.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package com.v.demo

import android.view.View
import com.azhon.appupdate.config.UpdateConfiguration
import com.azhon.appupdate.manager.DownloadManager
import com.v.base.BaseFragment
import com.v.base.dialog.HintDialog
import com.v.base.dialog.ImgSelectDialog
import com.v.base.dialog.ListDialog
import com.v.base.utils.*
import com.v.base.utils.ext.*
import com.v.demo.databinding.FragmentTowBinding
import com.v.demo.model.AppViewModel
import com.v.demo.model.DemoViewModel
import java.io.File


/**
Expand Down Expand Up @@ -55,7 +49,7 @@ class TwoFragment : BaseFragment<FragmentTowBinding, DemoViewModel>(), View.OnCl
mViewBinding.bt5.setDrawable(null, R.mipmap.ic_movie, h = 80, w = 80)
}
mViewBinding.bt6.id -> {
mViewBinding.ivIcon.load(R.mipmap.ic_movie, randomNumber(90).toFloat())
mViewBinding.ivIcon.load(R.mipmap.ic_movie, 90.randomNumber().toFloat())
}
mViewBinding.bt7.id -> {
mViewBinding.ivIcon.loadCircle(R.mipmap.ic_movie)
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/v/demo/model/DemoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.v.demo.model
import androidx.lifecycle.MutableLiveData
import com.v.base.BaseApplication.Companion.apiBase
import com.v.base.BaseViewModel
import com.v.base.net.BaseAppException
import com.v.base.utils.ext.log
import com.v.base.utils.ext.logE
import com.v.demo.bean.BannerBean
import com.v.demo.bean.GirlBean
import com.v.demo.net.ApiResponse
Expand All @@ -25,11 +28,12 @@ class DemoViewModel : BaseViewModel() {
if (page == 1) {
getBanner()
}

request({
apiService.getGirlBean(page)
}, success = {
girlBean.value = it
},code = {
"code:$it".logE()
})
}

Expand All @@ -43,4 +47,6 @@ class DemoViewModel : BaseViewModel() {
)
}



}
6 changes: 5 additions & 1 deletion app/src/main/java/com/v/demo/model/NetworkViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class NetworkViewModel : BaseViewModel() {
string.value = it.toJson()
}, error = {
//失败数据
}, dialog = true)//是否显示加载框
string.value = it.errorMsg
}, code = {
//code返回 不管成功或者失败
}, dialog = true//是否显示加载框
)
}

//自定义api 网络请求 获取原始数据
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/v/demo/net/ApiResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ApiResponse<T> : BaseResponse<T>() {
var msg = ""
var data: T? = null

// 这里是示例,wanandroid 网站返回的 错误码为 0 就代表请求成功,请你根据自己的业务需求来编写
// 这里是示例,wanandroid 网站返回的 错误码为 100 就代表请求成功,请你根据自己的业务需求来编写
override fun isSuccess() = status == 100

override fun getResponseCode() = status
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/v/demo/view/IndicatorZoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.util.ArrayList
* time : 2021/1/11 15:44
*/
class IndicatorZoom(
context: Context?,
context: Context,
viewPager: ViewPager,
fragments: ArrayList<Fragment>,
titles: Array<String>,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_one_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:id="@+id/ivIcon"
android:layout_width="match_parent"
android:scaleType="centerCrop"
app:imgUrl="@{bean.url}"
app:imgUrl="@{bean.title}"
android:layout_height="match_parent" />

</LinearLayout>
Expand Down
4 changes: 2 additions & 2 deletions vlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 300
versionName "3.0.0"
versionCode 310
versionName "3.1.0"
}

compileOptions {
Expand Down
2 changes: 1 addition & 1 deletion vlibrary/src/main/java/com/v/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import androidx.lifecycle.ViewModelProvider
import com.noober.background.BackgroundLibrary
import com.v.base.databinding.BaseLayoutBinding
import com.v.base.dialog.LoadingDialog
import com.v.base.utils.ext.log
import com.v.base.utils.getStatusBarHeight
import com.v.base.utils.log
import com.v.base.utils.onClickAnimator
import com.v.base.utils.setViewLayoutParams
import java.lang.reflect.Field
Expand Down
2 changes: 0 additions & 2 deletions vlibrary/src/main/java/com/v/base/BaseApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.v.base.net.BaseLogInterceptor
import com.v.base.net.BaseNetApi
import com.v.base.net.FastJsonConverterFactory
import com.v.base.utils.log
import com.v.base.utils.logE
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit
Expand Down
2 changes: 1 addition & 1 deletion vlibrary/src/main/java/com/v/base/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.v.base.dialog.LoadingDialog
import com.v.base.utils.log
import com.v.base.utils.ext.log
import java.lang.reflect.ParameterizedType


Expand Down
20 changes: 11 additions & 9 deletions vlibrary/src/main/java/com/v/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ package com.v.base
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.alibaba.fastjson.serializer.SerializerFeature
import com.v.base.net.BaseAppException
import com.v.base.net.BaseExceptionHandle
import com.v.base.net.BaseResponse
import com.v.base.utils.EventLiveData
import com.v.base.utils.toBean
import kotlinx.coroutines.*
import java.lang.Exception
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.lang.reflect.WildcardType


abstract class BaseViewModel : ViewModel() {
Expand All @@ -32,12 +27,14 @@ abstract class BaseViewModel : ViewModel() {
* @param block 协程体
* @param success 成功回调
* @param error 失败回调
* @param code 返回的code
* @param dialog 是否显示请求框
*/
fun <T> request(
block: suspend CoroutineScope.() -> BaseResponse<T>,
block: suspend () -> BaseResponse<T>,
success: (T) -> Unit,
error: (BaseAppException) -> Unit = {},
code: (Int) -> Unit = {},
dialog: Boolean = false
): Job {
return viewModelScope.launch {
Expand All @@ -48,6 +45,7 @@ abstract class BaseViewModel : ViewModel() {
block()
}.onSuccess {
loadingChange.dismissDialog.postValue(false)
code(it.getResponseCode())
runCatching {
executeResponse(it) { t -> success(t) }
}.onFailure { e ->
Expand Down Expand Up @@ -114,9 +112,13 @@ abstract class BaseViewModel : ViewModel() {
}
block()
}.onSuccess {
val type: Type = object : TypeReference<T>() {}.type
resultState.postValue(it.toString().toBean(type) as T)
loadingChange.dismissDialog.postValue(false)
runCatching {
val type: Type = object : TypeReference<T>() {}.type
resultState.postValue(it.toString().toBean(type) as T)
loadingChange.dismissDialog.postValue(false)
}.onFailure { e ->
error(BaseExceptionHandle.handleException(e))
}
}.onFailure { e ->
loadingChange.dismissDialog.postValue(false)
error(BaseExceptionHandle.handleException(e))
Expand Down
10 changes: 6 additions & 4 deletions vlibrary/src/main/java/com/v/base/dialog/BaseDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import androidx.lifecycle.ViewModelProvider
import com.v.base.BaseViewModel
import com.v.base.R
import com.v.base.annotaion.DialogOrientation
import com.v.base.utils.logI
import com.v.base.utils.ext.logI
import com.v.base.utils.otherwise
import com.v.base.utils.yes
import java.lang.reflect.ParameterizedType

/**
Expand All @@ -40,7 +42,7 @@ abstract class BaseDialogFragment<VB : ViewDataBinding, VM : BaseViewModel> : Di

override fun onAttach(context: Context) {
super.onAttach(context)
if (context is Activity) {
(context is Activity).yes {
this.mContext = context
}
javaClass.name.logI()
Expand Down Expand Up @@ -115,9 +117,9 @@ abstract class BaseDialogFragment<VB : ViewDataBinding, VM : BaseViewModel> : Di
wlp.width = WindowManager.LayoutParams.MATCH_PARENT
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT

if (useDim()) {
(useDim()).yes {
wlp.dimAmount = 0.5f
} else {
} .otherwise {
wlp.dimAmount = 0f
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}
Expand Down
Loading

0 comments on commit 979284d

Please sign in to comment.