|
@@ -24,13 +24,7 @@ import java.util.concurrent.TimeUnit
|
|
|
import java.util.concurrent.TimeoutException
|
|
|
|
|
|
data class NewsViewState(
|
|
|
- val initial: Boolean = false,
|
|
|
- val isProgress: Boolean = false,
|
|
|
- val isLoginSuccess: Boolean = false,
|
|
|
- val error: String? = null,
|
|
|
- val newsData: NewsData? = null,
|
|
|
- val highlightData: HighlightData? = null,
|
|
|
- val createdNewsData: CreatedNews? = null
|
|
|
+ val initial: Boolean = false, val isProgress: Boolean = false, val isLoginSuccess: Boolean = false, val error: String? = null, val newsData: NewsData? = null, val highlightData: HighlightData? = null, val createdNewsData: CreatedNews? = null
|
|
|
)
|
|
|
|
|
|
class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
|
|
@@ -43,16 +37,11 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
_state.value = prev().copy(isProgress = true)
|
|
|
|
|
|
disposables.add(
|
|
|
- apiService.getNews()
|
|
|
- .timeout(10, TimeUnit.SECONDS)
|
|
|
- .subscribeOn(Schedulers.io())
|
|
|
- .observeOn(AndroidSchedulers.mainThread())
|
|
|
- .subscribe({
|
|
|
+ apiService.getNews().timeout(10, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
|
|
|
if (it.resultCode == 1) {
|
|
|
_state.value = prev().copy(isProgress = false, isLoginSuccess = true, newsData = it.data!!)
|
|
|
} else {
|
|
|
- _state.value =
|
|
|
- prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
+ _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
}
|
|
|
}, {
|
|
|
val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
@@ -66,16 +55,11 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
_state.value = prev().copy(isProgress = true)
|
|
|
|
|
|
disposables.add(
|
|
|
- apiService.getHighlights()
|
|
|
- .timeout(10, TimeUnit.SECONDS)
|
|
|
- .subscribeOn(Schedulers.io())
|
|
|
- .observeOn(AndroidSchedulers.mainThread())
|
|
|
- .subscribe({
|
|
|
+ apiService.getHighlights().timeout(10, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
|
|
|
if (it.resultCode == 1) {
|
|
|
_state.value = prev().copy(isProgress = false, isLoginSuccess = true, highlightData = it.data!!)
|
|
|
} else {
|
|
|
- _state.value =
|
|
|
- prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
+ _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
}
|
|
|
}, {
|
|
|
val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
@@ -94,43 +78,11 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
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())
|
|
|
-
|
|
|
-// val file = File(request.upload)
|
|
|
-// val fileRequestBody = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
|
|
|
-// val filePart = MultipartBody.Part.createFormData("upload", file.name, fileRequestBody)
|
|
|
-//
|
|
|
-//
|
|
|
-// val requestBody = MultipartBody.Builder()
|
|
|
-// .setType(MultipartBody.FORM)
|
|
|
-// .addPart(titlePart)
|
|
|
-// .addPart(descPart)
|
|
|
-// .addPart(filePart)
|
|
|
-// .build()
|
|
|
-//
|
|
|
-// val multipartBody = MultipartBody.Part.createFormData("request", "filename", requestBody)
|
|
|
-
|
|
|
+ val filePart = MultipartBody.Part.createFormData("upload", file.name, file.asRequestBody("image/jpg".toMediaTypeOrNull()))
|
|
|
_state.value = prev().copy(isProgress = true)
|
|
|
|
|
|
disposables.add(
|
|
|
- apiService.createNews(titlePart, descPart,filePart)
|
|
|
- .timeout(60, 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({
|
|
|
+ apiService.createNews(titlePart, descPart, filePart).timeout(60, 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)
|
|
@@ -145,25 +97,18 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
} 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)
|
|
|
- })
|
|
|
+ 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)
|
|
|
+ })
|
|
|
)
|
|
|
}
|
|
|
}
|