basemodule是基于Android应用程序技术栈Jetpack封装的基础模块,旨在快速构建应用,提升开发效率。
Add a dependency code to your module's build.gradle
file.
Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.by8023hxy:basemodule:Tag'
}
-
Minimum SDK level 26
-
Kotlin based + Coroutines+Flow for asynchronous.
-
Koin 依赖注入框架
-
JetPack
- LiveData - notify domain layer data to views.
- Lifecycle - dispose of observing data when lifecycle state changes.
- ViewModel - UI related data holder, lifecycle aware.
-
Architecture
- MVVM Architecture (View - DataBinding - ViewModel - Model)
- Repository pattern 数据仓库
-
Retrofit2 & OkHttp3 - construct the REST APIs and paging network data.
-
Glide (https://github.com/florent37/GlidePalette) - loading images.
-
Gson 操作java对象和json数据之间的相互转换
BasicFramework is based on MVVM architecture and a repository pattern.
abstract class DataBindingActivity : AppCompatActivity() {
protected abstract fun getDataBindingConfig(): DataBindingConfig
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initDataBinding()
setObserve()
initView()
initData()
}
}
class MainActivity : DataBindingActivity() {
private val mainViewModel: MainViewModel by viewModel()
private val dialog: ProgressDialogFragment by lazy { ProgressDialogFragment() }
override fun getDataBindingConfig(): DataBindingConfig {
return DataBindingConfig(R.layout.activity_main,BR.vm,mainViewModel)
.addBindingParam(BR.adapter,DemoAdapter())
}
override fun initView() {
mainViewModel.apply {
getBannerList()
}
}
override fun initData() {
}
override fun setObserve() {
mainViewModel.isLoading().observe(this, {
operateDialog(it)
})
}
}
inline fun <T> flowScope(crossinline request: suspend () -> BaseResponse<T>): LiveData<RequestState<T>> {
return flow {
"flowScope onThread=${Thread.currentThread().name}".logD(TAG)
val response = request()
"flowScope response=${Gson().toJson(response)}".logD(TAG)
if (response.isSuccess()) {
emit(RequestState.Success(response.getResponseData()))
} else {
emit(
RequestState.Error(
ApiException(
response.getResponseMsg(),
response.getResponseCode()
)
)
)
}
}.onStart {
emit(RequestState.Loading)
}.catch {
val apiException = getApiException(it)
"flowScope exception==${Gson().toJson(it)}".logE()
emit(RequestState.Error(apiException))
}.asLiveData()
}
class AppRepository(private val remoteService: RemoteService) {
suspend fun getBannerList(id:String) = flowScope { remoteService.getBanner() }
}
class MainViewModel(private val repository: AppRepository) : LiveCoroutinesViewModel() {
val banner: ObservableField<List<BannerInfo>> = ObservableField()
private val _bannerList = MutableLiveData<String>()
val bannerList=_bannerList.switchMap {
launchOnViewModelScope { repository.getBannerList() }
}
fun getBannerList(id:String){
_bannerList.value=id
}
}
class MyApp : BaseApp() {
companion object {
@JvmStatic
lateinit var CONTEXT: BaseApp
@JvmStatic
val shareViewModel: ShareViewModel by lazy { ShareViewModel() }
}
override fun onCreate() {
super.onCreate()
"MyApp onCreate!!!".logD()
CONTEXT = this
MMKV.initialize(this)
startKoin {
androidLogger(Level.NONE)
androidContext(this@MyApp)
androidFileProperties()
modules(applicationModules)
}
}
}
val viewModelModule = module {
viewModel { MainViewModel(get()) }
}
val repositoryModule = module {
single { AppRepository(get()) }
}
val httpModule = module {
single<OkHttpClient> {
OkHttpClient.Builder()
.cache(Cache(File(MyApp.CONTEXT.cacheDir, "okhttp_cache"), 1024 * 1024 * 256L))
.addInterceptor(CacheInterceptor())
.addInterceptor(HttpLoggingInterceptor { message -> message.logD("MyHttpLog") }.apply {
this.level = HttpLoggingInterceptor.Level.BODY
}) // 日志拦截器
.addNetworkInterceptor(CacheNetworkInterceptor())
.connectTimeout(BaseConstant.HTTP_CONNECT_TIME, TimeUnit.SECONDS)
.readTimeout(BaseConstant.HTTP_READ_TIME, TimeUnit.SECONDS)
.writeTimeout(BaseConstant.HTTP_WRITE_TIME, TimeUnit.SECONDS)
.build()
}
single<RemoteService> {
Retrofit.Builder()
.client(get())
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(RemoteService.BASE_URL)
.build().create(RemoteService::class.java)
}
}
Support it by joining basemodule for this repository. ⭐
And follow me for my next creations! 🤩