|
@@ -8,15 +8,19 @@ import com.sambath.kunkhmer.base.BaseViewModel
|
|
import com.sambath.kunkhmer.remote.CreateNewsRequest
|
|
import com.sambath.kunkhmer.remote.CreateNewsRequest
|
|
import com.sambath.kunkhmer.remote.CreatedNews
|
|
import com.sambath.kunkhmer.remote.CreatedNews
|
|
import com.sambath.kunkhmer.remote.HighlightData
|
|
import com.sambath.kunkhmer.remote.HighlightData
|
|
-import com.sambath.kunkhmer.remote.LoginRequest
|
|
|
|
import com.sambath.kunkhmer.remote.NewsData
|
|
import com.sambath.kunkhmer.remote.NewsData
|
|
import com.sambath.kunkhmer.remote.service.ApiService
|
|
import com.sambath.kunkhmer.remote.service.ApiService
|
|
-import com.sambath.kunkhmer.util.Const
|
|
|
|
-import com.sambath.kunkhmer.util.ModelPreferencesManager
|
|
|
|
import com.sambath.kunkhmer.util.PrefHelper
|
|
import com.sambath.kunkhmer.util.PrefHelper
|
|
|
|
+import io.reactivex.Flowable
|
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
|
import io.reactivex.schedulers.Schedulers
|
|
import io.reactivex.schedulers.Schedulers
|
|
|
|
+import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
|
|
+import okhttp3.MultipartBody
|
|
|
|
+import okhttp3.RequestBody.Companion.asRequestBody
|
|
|
|
+import okhttp3.RequestBody.Companion.toRequestBody
|
|
|
|
+import java.io.File
|
|
import java.util.concurrent.TimeUnit
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
+import java.util.concurrent.TimeoutException
|
|
|
|
|
|
data class NewsViewState(
|
|
data class NewsViewState(
|
|
val initial: Boolean = false,
|
|
val initial: Boolean = false,
|
|
@@ -82,24 +86,70 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
fun createNews(title: String, desc: String, upload: String) {
|
|
fun createNews(title: String, desc: String, upload: String) {
|
|
if (_state.value!!.isProgress) return
|
|
if (_state.value!!.isProgress) return
|
|
val request = CreateNewsRequest(title, desc, upload)
|
|
val request = CreateNewsRequest(title, desc, upload)
|
|
- _state.value = prev().copy(isProgress = true)
|
|
|
|
|
|
|
|
- disposables.add(
|
|
|
|
- apiService.createNews(request)
|
|
|
|
- .timeout(10, TimeUnit.SECONDS)
|
|
|
|
- .subscribeOn(Schedulers.io())
|
|
|
|
- .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
- .subscribe({
|
|
|
|
- Log.d("login", it.toString())
|
|
|
|
- if (it.resultCode == 1) {
|
|
|
|
- _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
|
|
|
|
- } else {
|
|
|
|
- _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
|
- }
|
|
|
|
- }, {
|
|
|
|
- val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
|
- _state.value = prev().copy(isProgress = false, error = message)
|
|
|
|
- })
|
|
|
|
- )
|
|
|
|
|
|
+ val titlePart = request.title.toRequestBody("text/plain".toMediaTypeOrNull())
|
|
|
|
+ val descPart = request.desc.toRequestBody("text/plain".toMediaTypeOrNull())
|
|
|
|
+
|
|
|
|
+ if(!upload.isNullOrEmpty()) {
|
|
|
|
+ // Assuming 'upload' is a file path, you may need to modify this part based on your actual requirements
|
|
|
|
+ val file = File(request.upload)
|
|
|
|
+ //val filePart = MultipartBody.Part.createFormData("upload", file.name, file.asRequestBody("multipart/form-data".toMediaTypeOrNull()))
|
|
|
|
+ val filePart = MultipartBody.Part.createFormData("upload", file.name, file.asRequestBody())
|
|
|
|
+
|
|
|
|
+ _state.value = prev().copy(isProgress = true)
|
|
|
|
+
|
|
|
|
+ disposables.add(
|
|
|
|
+ apiService.createNews(titlePart, descPart, filePart)
|
|
|
|
+ .timeout(10, TimeUnit.SECONDS)
|
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
+// .retryWhen { errors ->
|
|
|
|
+// errors
|
|
|
|
+// .zipWith(Flowable.range(1, 3)) { throwable, retryCount -> Pair(throwable, retryCount) }
|
|
|
|
+// .flatMap { (throwable, retryCount) ->
|
|
|
|
+// if (throwable is TimeoutException && retryCount <= 3) {
|
|
|
|
+// Flowable.timer(5, TimeUnit.SECONDS) // Retry after 5 seconds
|
|
|
|
+// } else {
|
|
|
|
+// Flowable.error(throwable)
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// }
|
|
|
|
+ .subscribe({
|
|
|
|
+ Log.d("login", it.toString())
|
|
|
|
+ if (it.resultCode == 1) {
|
|
|
|
+ _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
|
|
|
|
+ } else {
|
|
|
|
+ _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
|
+ _state.value = prev().copy(isProgress = false, error = message)
|
|
|
|
+ })
|
|
|
|
+ )
|
|
|
|
+ } else {
|
|
|
|
+ _state.value = prev().copy(isProgress = true)
|
|
|
|
+
|
|
|
|
+ disposables.add(
|
|
|
|
+ apiService.createNews(titlePart, descPart)
|
|
|
|
+ .timeout(10, TimeUnit.SECONDS)
|
|
|
|
+ .doOnSubscribe { Log.d("Network", "Request started") }
|
|
|
|
+ .doOnSuccess { Log.d("Network", "Request succeeded: $it") }
|
|
|
|
+ .doOnError { Log.e("Network", "Request failed", it) }
|
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
|
+ .subscribe({
|
|
|
|
+ Log.d("login", it.toString())
|
|
|
|
+ if (it.resultCode == 1) {
|
|
|
|
+ _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
|
|
|
|
+ } else {
|
|
|
|
+ _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
|
+ }
|
|
|
|
+ }, {
|
|
|
|
+ val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
|
+ _state.value = prev().copy(isProgress = false, error = message)
|
|
|
|
+ })
|
|
|
|
+ )
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|