riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cn.flightfeather.thirdapp.repository
 
import cn.flightfeather.thirdapp.common.net.ResultCallBack
import cn.flightfeather.thirdapp.common.net.ResultObserver
import cn.flightfeather.thirdapp.common.net.RetrofitFactory
import cn.flightfeather.thirdapp.httpservice.CommonService
import cn.flightfeather.thirdapp.model.enumreation.MediaFileType
import cn.flightfeather.thirdapp.repository.dao.MediaTypeAliasDao
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response
import java.io.File
 
/**
 * @author riku
 * Date: 2020/4/23
 */
class CommonRepository {
    private val mediaTypeAliasDao = MediaTypeAliasDao()
 
    fun upLoadCrashInfo(accountName: String, file: File, resultCallBack: ResultCallBack<Boolean>) {
        val builder = MultipartBody.Builder()
        val requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        builder.addFormDataPart("files", file.name, requestBody)
        builder.setType(MultipartBody.FORM)
        val multipartBody = builder.build()
        val partList = mutableListOf<MultipartBody.Part>()
        partList.addAll(multipartBody.parts())
 
        val service = RetrofitFactory.instance.retrofit.create(CommonService::class.java)
            .upLoadCrashInfo(accountName, partList)
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.trampoline())
            .subscribe({
                resultCallBack.onSuccess(it.body())
            }, {
                resultCallBack.onFailure()
            })
    }
 
 
    /**
     * 获取“任意拍”模块展示的图片类型
     */
    fun getMediaFileTypes(sceneTypeId: Int, resultCallBack: ResultCallBack<List<MediaFileType>>) {
        resultCallBack.onSuccess(MediaFileType.getList(sceneTypeId))
    }
 
    /**
     * 获取媒体文件类型的别名
     * @param sceneTypeId 场景id
     * @param mediaFileType 媒体文件类型id
     */
    fun getAlias(sceneTypeId: Int, mediaFileType: MediaFileType, resultCallBack: ResultCallBack<String>) {
        val dbService = mediaTypeAliasDao.getAlias(sceneTypeId, mediaFileType)
                .map { Response.success(it) }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<String>() {
            override fun onSuccess(result: String?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    /**
     * 更新媒体文件类型的别名
     * @param sceneTypeId 场景id
     * @param mediaFileType 媒体文件类型id
     * @param alias 别名
     */
    fun updateAlias(sceneTypeId: Int, mediaFileType: MediaFileType, alias: String, resultCallBack: ResultCallBack<Boolean>) {
        val dbService = mediaTypeAliasDao.updateAlias(sceneTypeId, mediaFileType, alias)
                .map { Response.success(it) }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<Boolean>() {
            override fun onSuccess(result: Boolean?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
}