Skip to content

Commit

Permalink
optimize net
Browse files Browse the repository at this point in the history
  • Loading branch information
loongwind committed Aug 19, 2022
1 parent 18095f3 commit a0c93f0
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 41 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ dependencies {
// implementation 'com.loongwind.ardf:base:1.0.1'
// implementation project(path: ':recyclerview')
implementation project(path: ':base')
implementation project(path: ':net')

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
19 changes: 0 additions & 19 deletions app/src/main/java/com/loongwind/ardf/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,18 @@ package com.loongwind.ardf.demo

import android.content.Intent
import com.loongwind.ardf.base.BaseBindingViewModelActivity
import com.loongwind.ardf.base.ext.toast
import com.loongwind.ardf.demo.databinding.ActivityMainBinding
import com.loongwind.ardf.net.request
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.android.ext.android.inject

class MainActivity : BaseBindingViewModelActivity<ActivityMainBinding, MainViewModel>() {

val apiService : ApiService by inject()

override fun onEvent(eventId: Int) {
super.onEvent(eventId)
when(eventId){
MainViewModel.EVENT_SIMPLE -> startActivity(Intent(this, RecycleViewSimpleActivity::class.java))
MainViewModel.EVENT_ITEM_CLICK -> startActivity(Intent(this, RecycleViewSimpleItemClickActivity::class.java))
MainViewModel.EVENT_ITEM_EVENT -> startActivity(Intent(this, RecycleViewSimpleItemEventActivity::class.java))
MainViewModel.EVENT_MULTI_ITEM_VIEW -> startActivity(Intent(this, RecycleViewSimpleMultiItemViewActivity::class.java))
MainViewModel.EVENT_API -> requestApi()
}

}

private fun requestApi(){
request{
val user = apiService.getUser()
toast(user.name)
}

}

}
12 changes: 10 additions & 2 deletions app/src/main/java/com/loongwind/ardf/demo/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package com.loongwind.ardf.demo

import com.loongwind.ardf.base.BaseViewModel
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class MainViewModel : BaseViewModel() {
class MainViewModel : BaseViewModel() , KoinComponent{

private val apiService:ApiService by inject()

companion object {
const val EVENT_SIMPLE = 0x00
const val EVENT_ITEM_CLICK = 0x01
const val EVENT_ITEM_EVENT = 0x02
const val EVENT_MULTI_ITEM_VIEW = 0x03
const val EVENT_API = 0x04
}


fun onClick(event : Int){
postEvent(event)
}

fun getUser() = launch {
val user = apiService.getUser()
postHintText("------" + user.name + "------")
}
}
8 changes: 7 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="api请求"
android:onClick="@{()->vm.onClick(MainViewModel.EVENT_API)}"
android:onClick="@{()->vm.getUser()}"
app:layout_constraintTop_toTopOf="parent"/>

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="@{vm.isLoading}"/>


</LinearLayout>
</layout>
1 change: 1 addition & 0 deletions base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
api "io.insert-koin:koin-core:${versions.koin}"
api "io.insert-koin:koin-android:${versions.koin}"
api "pub.devrel:easypermissions:${versions.easypermissions}"
api project(path: ':net')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
29 changes: 27 additions & 2 deletions base/src/main/java/com/loongwind/ardf/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import androidx.databinding.Observable
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.loongwind.ardf.base.event.EVENT_BACK
import com.loongwind.ardf.base.event.Event
import com.loongwind.ardf.net.CoroutineLambda
import com.loongwind.ardf.net.ErrorHandle
import com.loongwind.ardf.net.request

/**
* @Description: ViewModel 基类,定义数据加载状态(isLoading)、提示信息(hintText/hintTextRes)、
Expand Down Expand Up @@ -55,11 +59,32 @@ open class BaseViewModel: ViewModel() {
postEvent(EVENT_BACK)
}

private fun showLoading(){
fun showLoading(){
showLoadingCount.set(showLoadingCount.get() + 1)
}

private fun dismissLoading(){
fun dismissLoading(){
showLoadingCount.set(showLoadingCount.get() - 1)
}


fun launch(
isShowLoading: Boolean = true,
onError: ErrorHandle? = null,
block: CoroutineLambda
) {
request(coroutineScope = viewModelScope, error = { t ->
isLoading.value = false
onError?.invoke(t) == true
}) {
if(isShowLoading){
showLoading()
}
block()
if(isShowLoading){
dismissLoading()
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.loongwind.ardf.base.ext

import android.view.View
import android.widget.ImageView
import androidx.databinding.BindingAdapter

/**
*Author: chengminghui
*Time: 2019-09-04
*Description: xxx
*/

@BindingAdapter("android:visibility")
fun setVisibility(view: View, isVisibility: Boolean) {
view.visibility = if (isVisibility) View.VISIBLE else View.GONE
}


@BindingAdapter("android:src")
fun setImage(imageView: ImageView, imgRes: Int) {
imageView.setImageResource(imgRes)
}
20 changes: 4 additions & 16 deletions net/src/main/java/com/loongwind/ardf/net/HttpRequester.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.loongwind.ardf.net
import kotlinx.coroutines.*
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.core.scope.Scope

/**
*Author: chengminghui
Expand All @@ -14,23 +15,10 @@ typealias ErrorHandle = (Throwable) -> Boolean
typealias CoroutineLambda = suspend CoroutineScope.() -> Unit


private fun request(block: CoroutineLambda) {
val exceptionHandle = CoroutineExceptionHandler { coroutineContext, throwable ->
throwable.printStackTrace()
}
GlobalScope.launch(exceptionHandle) {
withContext(Dispatchers.Main) {
block()
}
}


}

fun request(exceptionHandler: HttpExceptionHandler? = getHttpExceptionHandler(),
fun request(coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.Main), exceptionHandler: HttpExceptionHandler? = getHttpExceptionHandler(),
error: ErrorHandle? = null,
block: CoroutineLambda) {
request {
coroutineScope.launch {
try {
block()
} catch (e: Exception) {
Expand All @@ -46,4 +34,4 @@ private fun getHttpExceptionHandler() = object : KoinComponent {
fun getExceptionHandler(): HttpExceptionHandler {
return get()
}
}.getExceptionHandler()
}.getExceptionHandler()

0 comments on commit a0c93f0

Please sign in to comment.