|
@@ -5,6 +5,7 @@ import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import com.sambath.kunkhmer.app.getErrorCode
|
|
|
import com.sambath.kunkhmer.base.BaseViewModel
|
|
|
+import com.sambath.kunkhmer.remote.CreateHighlightRequest
|
|
|
import com.sambath.kunkhmer.remote.CreateNewsRequest
|
|
|
import com.sambath.kunkhmer.remote.CreatedNews
|
|
|
import com.sambath.kunkhmer.remote.HighlightData
|
|
@@ -38,15 +39,15 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
|
|
|
disposables.add(
|
|
|
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}]")
|
|
|
- }
|
|
|
- }, {
|
|
|
- val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
- _state.value = prev().copy(isProgress = false, error = message)
|
|
|
- })
|
|
|
+ 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}]")
|
|
|
+ }
|
|
|
+ }, {
|
|
|
+ val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
+ _state.value = prev().copy(isProgress = false, error = message)
|
|
|
+ })
|
|
|
)
|
|
|
}
|
|
|
|
|
@@ -56,8 +57,31 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
|
|
|
disposables.add(
|
|
|
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}]")
|
|
|
+ }
|
|
|
+ }, {
|
|
|
+ val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
+ _state.value = prev().copy(isProgress = false, error = message)
|
|
|
+ })
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ fun createHighlight(title: String, date: String, url: String) {
|
|
|
+ if (_state.value!!.isProgress) return
|
|
|
+ val request = CreateHighlightRequest(title, date, url)
|
|
|
+ _state.value = prev().copy(isProgress = true)
|
|
|
+
|
|
|
+ disposables.add(
|
|
|
+ apiService.createHighlight(request).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, highlightData = it.data!!)
|
|
|
+ _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
|
|
|
} else {
|
|
|
_state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
}
|
|
@@ -83,16 +107,16 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
|
|
|
|
|
|
disposables.add(
|
|
|
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)
|
|
|
- } else {
|
|
|
- _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
|
|
|
- }
|
|
|
- }, {
|
|
|
- val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
|
|
|
- _state.value = prev().copy(isProgress = false, error = message)
|
|
|
- })
|
|
|
+ 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)
|