Skip to content

Commit

Permalink
重构全局配置方法
Browse files Browse the repository at this point in the history
  • Loading branch information
oooo7777777 committed Feb 16, 2022
1 parent e6b2f4f commit 5b170fe
Show file tree
Hide file tree
Showing 22 changed files with 299 additions and 292 deletions.
62 changes: 12 additions & 50 deletions app/src/main/java/com/v/demo/DemoApplication.kt
Original file line number Diff line number Diff line change
@@ -1,70 +1,32 @@
package com.v.demo

import android.graphics.Color
import android.view.View
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.v.base.VBApplication
import com.v.base.net.VBFastJsonConverterFactory
import com.v.base.net.VBLogInterceptor
import com.v.base.utils.ext.vbGetLayoutView
import com.v.demo.net.NetworkHeadInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit
import com.v.base.VBConfig
import com.v.base.VBConfigOptions
import com.v.base.net.VBNetOptions

class DemoApplication : VBApplication() {

/**
* 重写此方法 开启日志打印(日志TAG为 PRETTY_LOGGER)
* 开启日志打印(日志TAG为 PRETTY_LOGGER)
*/
override fun isDebug(): Boolean {
return true
}

/**
* 重写此方法 设置全局状态栏颜色 状态栏颜色趋近于白色时 会智能将状态栏字体颜色变换为黑色
*/
override fun statusBarColor(): Int {
return Color.parseColor("#ffffff")
}

/**
* 使用VLibrary库自带的网络请求时候请重写此方法
* 传入baseUrl
*/
override fun baseUrl(): String {
return "https://www.wanandroid.com/"
}

/**
* 一般不用动~~~~~
* 如需要对VLibrary库自带的网络请求Retrofit.Builder做任意操作 比如添加json解析器 请重写此方法
*
*/
override fun retrofitBuilder(): Retrofit.Builder {
return Retrofit.Builder().apply {
addConverterFactory(VBFastJsonConverterFactory.create())
addCallAdapterFactory(CoroutineCallAdapterFactory())
}
}

/**
* 如需要对VLibrary库自带的网络请求OkHttpClient做任意操作 在这里可以添加拦截器 请求头
*/
override fun okHttpClient(): OkHttpClient {
override fun initData() {

return OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS) //超时时间 连接、读、写
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.addInterceptor(NetworkHeadInterceptor()) //添加公共heads 注意要设置在日志拦截器之前,不然Log中会不显示head信息
.addInterceptor(VBLogInterceptor())// 日志拦截器(这里请使用BaseLogInterceptor,不然网络请求日志不会打印出来)
val netOptions = VBNetOptions.Builder()
.setBaseUrl("https://www.wanandroid.com/")
.build()
}


override fun initData() {
val options = VBConfigOptions.Builder()
.setNetOptions(netOptions)
.setStatusBarColor(Color.parseColor("#ffffff"))
.build()

VBConfig.init(options)

}

Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/v/demo/OneFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.v.demo.model.DemoViewModel
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.constants.PageStyle
import com.zhpan.indicator.enums.IndicatorSlideMode
import kotlin.time.days

/**
* @Author : ww
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/v/demo/ThreeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class ThreeFragment : VBFragment<FragmentThreeBinding, VBBlankViewModel>(), View
}

mDataBinding.bt3.id -> {
VBHintDialog(mContext)
.setTitle("提示")
VBHintDialog(mContext).setTitle("提示")
.setContent("确定保存吗?")
.setButtonText("取消", "确定")
.setClickListener { hintDialog, position ->
hintDialog.dismiss()
}.show()
}
.setCanceled(false)
.show()
}
mDataBinding.bt4.id -> {

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/v/demo/model/DemoViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.v.demo.model

import androidx.lifecycle.MutableLiveData
import com.v.base.VBApplication.Companion.apiBase
import com.v.base.VBViewModel
import com.v.base.apiBase
import com.v.demo.bean.BannerBean
import com.v.demo.bean.HomeBean
import com.v.demo.net.ApiResponse
import kotlinx.coroutines.delay


/**
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/v/demo/model/NetworkViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.v.demo.model

import androidx.lifecycle.MutableLiveData
import com.v.base.VBApplication.Companion.apiBase
import com.v.base.VBViewModel
import com.v.base.apiBase
import com.v.base.utils.vbToJson
import com.v.demo.net.Network.Companion.apiService
import kotlinx.coroutines.delay
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 358
versionName "3.5.8"
versionCode 359
versionName "3.5.9"
}

compileOptions {
Expand Down
8 changes: 4 additions & 4 deletions vlibrary/src/main/java/com/v/base/VBActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ abstract class VBActivity<VB : ViewDataBinding, VM : VBViewModel> : AppCompatAct
mRootDataBinding.layoutContent.addView(mDataBinding.root)
if (useRecyclerViewErrorShow()) {
//加载错误布局
VBApplication.getRecyclerViewErrorView()?.run {
VBConfig.options.recyclerViewErrorLayout.run {
mRootDataBinding.layoutError.removeAllViews()
mRootDataBinding.layoutError.addView(mContext.vbGetLayoutView(this))
}
Expand Down Expand Up @@ -140,7 +140,7 @@ abstract class VBActivity<VB : ViewDataBinding, VM : VBViewModel> : AppCompatAct
* @param color 颜色
*/
protected open fun statusBarColor(
color: Int = VBApplication.getStatusBarColor()
color: Int = VBConfig.options.statusBarColor
) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Expand Down Expand Up @@ -338,8 +338,8 @@ abstract class VBActivity<VB : ViewDataBinding, VM : VBViewModel> : AppCompatAct
}
if (it) {
currentRefreshLayout?.run {
if (VBApplication.getRecyclerViewErrorView() != null && useRecyclerViewErrorShow()) {
//如果是下拉加载 就肯定是第第一页
if (useRecyclerViewErrorShow()) {
//如果是下拉加载 就肯定是第一页
if (isRefreshing) {
mRootDataBinding.layoutError.visibility = View.VISIBLE
mRootDataBinding.layoutError.setOnClickListener {
Expand Down
143 changes: 0 additions & 143 deletions vlibrary/src/main/java/com/v/base/VBApplication.kt
Original file line number Diff line number Diff line change
@@ -1,178 +1,35 @@
package com.v.base

import android.app.Application
import android.content.Context
import android.graphics.Color
import android.view.View
import androidx.annotation.LayoutRes
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.Logger
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.v.base.net.VBLogInterceptor
import com.v.base.net.VBNetApi
import com.v.base.net.VBFastJsonConverterFactory
import com.v.base.utils.ext.vbEmptyView
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit


abstract class VBApplication : Application() {


companion object {
private lateinit var context: VBApplication

private var isNetToast = true

private var baseUrl: String? = null
private var builder: Retrofit.Builder? = null

private var statusBarColor: Int = 0

private var recyclerViewEmptyView: Int? = null

private var recyclerViewErrorView: Int? = null


fun getApplication(): Application {
return context
}

fun getRetrofitBuilder(): Retrofit.Builder? {
return builder
}

fun getBaseUrl(): String? {
return baseUrl
}

fun getStatusBarColor(): Int {
return statusBarColor
}

fun getRecyclerViewEmptyView(): Int? {
return recyclerViewEmptyView
}

fun getRecyclerViewErrorView(): Int? {
return recyclerViewErrorView
}

fun isNetToast(): Boolean {
return isNetToast
}

//直接使用base库里面的网络请求
val apiBase: VBNetApi by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
if (getBaseUrl().isNullOrEmpty()) {
throw IllegalStateException("baseUrl为空,请继承BaseApplication重写baseUrl()")
}
getRetrofitBuilder()?.build()!!.create(VBNetApi::class.java)
}
}


/**
* 重写父类的okHttpClient方法,
* 在这里可以添加拦截器
*/
protected open fun okHttpClient(): OkHttpClient = OkHttpClient.Builder().apply {
connectTimeout(10, TimeUnit.SECONDS) //超时时间 连接、读、写
readTimeout(5, TimeUnit.SECONDS)
writeTimeout(5, TimeUnit.SECONDS)
addInterceptor(VBLogInterceptor())// 日志拦截器
}.build()

/**
* 重写父类的retrofitBuilder方法,
* 在这里可以对Retrofit.Builder做任意操作,比如添加json解析器
*/
protected open fun retrofitBuilder(): Retrofit.Builder = Retrofit.Builder()
.apply {
addConverterFactory(VBFastJsonConverterFactory.create())
addCallAdapterFactory(CoroutineCallAdapterFactory())
}

/**
* 使用 VLibrary库自带的网络请求时候请重写此方法
*/
protected open fun baseUrl(): String = ""

/**
* 设置状态栏颜色
*/
protected open fun statusBarColor(): Int = Color.parseColor("#000000")


/**
* 设置默认RecyclerView 数据为空界面
*/
protected open fun recyclerViewEmptyView(): Int? = R.layout.vb_layout_empty

/**
* 设置默认RecyclerView错误界面 只会在page为1并且没有数据的时候显示
*/
@LayoutRes
protected open fun recyclerViewErrorView(): Int? = R.layout.vb_layout_error


/**
* 是否开启debug模式 关联输出日志
*/
protected open fun isDebug(): Boolean = true

/**
* 是否开启网络请求错误 弹出toast
*/
protected open fun isNetToast(): Boolean = true

override fun onCreate() {
super.onCreate()
context = this
statusBarColor = statusBarColor()
recyclerViewEmptyView = recyclerViewEmptyView()
recyclerViewErrorView = recyclerViewErrorView()

isNetToast = isNetToast()

if (isDebug()) {
Logger.addLogAdapter(AndroidLogAdapter())
}

if (!baseUrl().isNullOrEmpty()) {
baseUrl = baseUrl()
builder = retrofitBuilder().apply {
baseUrl(baseUrl)
client(okHttpClient())
}

}

initSmartRefreshLayout()
initData()

}

/**
* 全局设置SmartRefreshLayout
*/
private fun initSmartRefreshLayout() {
//设置全局的Header构建器
SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
ClassicsHeader(context)
}

//设置全局的Footer构建器
SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
ClassicsFooter(context)
}
}


protected abstract fun initData()


Expand Down
34 changes: 34 additions & 0 deletions vlibrary/src/main/java/com/v/base/VBConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.v.base

import com.v.base.net.VBNetApi


//直接使用base库里面的网络请求
val apiBase: VBNetApi by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
val netOptions = VBConfig.options.netOptions

if (netOptions.baseUrl.isNullOrEmpty()) {
throw IllegalStateException("baseUrl为空,请继承BaseApplication重写baseUrl()")
}
netOptions.retrofitBuilder.apply {
baseUrl(netOptions.baseUrl)
client(netOptions.okHttpClient)
}.build().create(VBNetApi::class.java)
}

/**
* author : ww
* desc : 网络请求配置
* time : 2022-02-16
*/
object VBConfig {

var options: VBConfigOptions = VBConfigOptions.Builder().build()

fun init(options: VBConfigOptions) {
this.options = options
}

}


Loading

0 comments on commit 5b170fe

Please sign in to comment.