108 lines
4.6 KiB
Kotlin
108 lines
4.6 KiB
Kotlin
package com.gh.common.util
|
|
|
|
import android.content.Context
|
|
import com.gh.gamecenter.eventbus.EBCollectionChanged
|
|
import com.gh.gamecenter.manager.UserManager
|
|
import com.gh.gamecenter.retrofit.Response
|
|
import com.gh.gamecenter.retrofit.RetrofitManager
|
|
import okhttp3.ResponseBody
|
|
import org.greenrobot.eventbus.EventBus
|
|
import org.json.JSONObject
|
|
import retrofit2.HttpException
|
|
import rx.Observable
|
|
import rx.android.schedulers.AndroidSchedulers
|
|
import rx.schedulers.Schedulers
|
|
|
|
/**
|
|
* Created by khy on 26/07/17.
|
|
*/
|
|
object CollectionUtils {
|
|
|
|
enum class CollectionType {
|
|
toolkit, article, answer
|
|
}
|
|
|
|
fun postCollection(context: Context, content: String, type: CollectionType, listener: OnCollectionListener) {
|
|
|
|
val postCollection = when (type) {
|
|
CollectionType.article -> RetrofitManager.getInstance(context).getApi().postCollectionArticle(UserManager.getInstance().userId, content)
|
|
CollectionType.toolkit -> RetrofitManager.getInstance(context).getApi().postCollectionTools(UserManager.getInstance().userId, content)
|
|
CollectionType.answer -> RetrofitManager.getInstance(context).getApi().postCollectionAnswer(UserManager.getInstance().userId, content)
|
|
}
|
|
postCollection
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(object : Response<ResponseBody>() {
|
|
override fun onResponse(response: ResponseBody?) {
|
|
super.onResponse(response)
|
|
listener.onSuccess()
|
|
if(type != CollectionType.answer)
|
|
EventBus.getDefault().post(EBCollectionChanged(content, true, type))
|
|
}
|
|
|
|
override fun onFailure(e: HttpException?) {
|
|
super.onFailure(e)
|
|
if (e != null) {
|
|
try {
|
|
val string = e.response()?.errorBody()?.string()
|
|
val errorBody = JSONObject(string)
|
|
if (errorBody.getInt("status") == 403009) {
|
|
listener.onSuccess()
|
|
return
|
|
}
|
|
} catch (e: Exception) {
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
listener.onError()
|
|
}
|
|
})
|
|
}
|
|
|
|
fun deleteCollection(context: Context, id: String, type: CollectionType, listener: OnCollectionListener) {
|
|
|
|
val postCollection: Observable<ResponseBody>
|
|
when (type) {
|
|
CollectionType.article -> postCollection = RetrofitManager.getInstance(context).getApi().deletaCollectionArticle(UserManager.getInstance().userId, id)
|
|
CollectionType.toolkit -> postCollection = RetrofitManager.getInstance(context).getApi().deleteCollectionTools(UserManager.getInstance().userId, id)
|
|
CollectionType.answer -> postCollection = RetrofitManager.getInstance(context).getApi().deleteCollectionAnswer(UserManager.getInstance().userId, id)
|
|
}
|
|
postCollection
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(object : Response<ResponseBody>() {
|
|
override fun onResponse(response: ResponseBody?) {
|
|
super.onResponse(response)
|
|
listener.onSuccess()
|
|
if(type != CollectionType.answer)
|
|
EventBus.getDefault().post(EBCollectionChanged(id, false, type))
|
|
}
|
|
|
|
override fun onFailure(e: HttpException?) {
|
|
super.onFailure(e)
|
|
listener.onError()
|
|
}
|
|
})
|
|
}
|
|
|
|
fun patchCollection(context: Context, id: String, type: CollectionType) {
|
|
// val postCollection = when (type) {
|
|
// CollectionType.article -> RetrofitManager.getInstance(context).getApi().patchCollectionArticle(id)
|
|
// CollectionType.toolkit -> RetrofitManager.getInstance(context).getApi().patchCollectionTools(id)
|
|
// else -> {
|
|
// return
|
|
// }
|
|
// }
|
|
// postCollection
|
|
// .subscribeOn(Schedulers.io())
|
|
// .observeOn(AndroidSchedulers.mainThread())
|
|
// .subscribe(object : Response<ResponseBody>() {})
|
|
}
|
|
|
|
|
|
interface OnCollectionListener {
|
|
fun onSuccess()
|
|
fun onError()
|
|
}
|
|
|
|
} |