Skip to content

Commit

Permalink
optimal
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanganhtuan95ptit committed Jan 30, 2023
1 parent 35dcbd4 commit e03a205
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
Binary file modified .gradle/7.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.lock
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.one.word.data.task.dictionary.envi

import com.one.coreapp.data.usecase.ResultState
import com.one.coreapp.data.usecase.toSuccess
import com.one.coreapp.utils.extentions.logException
import com.one.word.data.task.dictionary.DictionaryTask
import com.one.word.entities.TextLevel
import com.one.word.entities.TextLevelType
Expand All @@ -13,12 +14,19 @@ class WikiEnViDictionaryTask : DictionaryTask {

override suspend fun execute(param: DictionaryTask.Param): ResultState<List<TextLevel>> {

return if (param.inputCode != "en" || param.outputCode != "vi") {
if (param.inputCode != "en" || param.outputCode != "vi") {

ResultState.Failed(RuntimeException("Not support"))
} else {
return ResultState.Failed(RuntimeException("Not support"))
}

return runCatching {

handleFromWiki(param.text)
}.getOrElse {

logException(java.lang.RuntimeException("WikiEnViDictionaryTask", it))

ResultState.Failed(it)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.one.word.data.task.spelling.en

import android.util.Log
import com.one.coreapp.data.usecase.ResultState
import com.one.coreapp.utils.extentions.logException
import com.one.word.data.task.spelling.SpellingTask
import com.one.word.entities.Spelling
import org.jsoup.Jsoup
Expand All @@ -10,12 +10,19 @@ class EnSpellingTask : SpellingTask {

override suspend fun execute(param: SpellingTask.Param): ResultState<List<Spelling>> {

return if (param.inputCode != "en") {
if (param.inputCode != "en") {

ResultState.Failed(RuntimeException("Not support"))
} else {
return ResultState.Failed(RuntimeException("Not support"))
}

return runCatching {

handleFromWiki(param.text)
}.getOrElse {

logException(java.lang.RuntimeException("EnSpellingTask", it))

ResultState.Failed(it)
}
}

Expand Down Expand Up @@ -66,7 +73,7 @@ class EnSpellingTask : SpellingTask {
}

if (text.contains("us", true) && audio != null) {
map["us"]?.audio = "https:" + audio
map["us"]?.audio = "https:$audio"
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.one.word.data.usecase
import com.one.coreapp.data.usecase.BaseUseCase
import com.one.coreapp.data.usecase.ResultState
import com.one.coreapp.utils.extentions.executeByFast
import com.one.coreapp.utils.extentions.log
import com.one.word.data.task.dictionary.DictionaryTask
import com.one.word.entities.TextLevel

Expand All @@ -13,6 +14,8 @@ class FetchWordDictionaryUseCase(
override suspend fun execute(param: Param?): ResultState<List<TextLevel>> {
checkNotNull(param)

log("fetch word dictionary use case ${param.inputCode} ${param.outputCode}")

return list.executeByFast(DictionaryTask.Param(param.text, param.inputCode, param.outputCode))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.one.word.data.usecase
import com.one.coreapp.data.usecase.BaseUseCase
import com.one.coreapp.data.usecase.ResultState
import com.one.coreapp.utils.extentions.executeByFast
import com.one.coreapp.utils.extentions.log
import com.one.word.data.task.spelling.SpellingTask
import com.one.word.entities.Spelling

Expand All @@ -13,6 +14,8 @@ class FetchWordSpellingUseCase(
override suspend fun execute(param: Param?): ResultState<List<Spelling>> {
checkNotNull(param)

log("fetch word spelling use case ${param.inputCode}")

return list.executeByFast(SpellingTask.Param(param.text, param.inputCode))
}

Expand Down
3 changes: 1 addition & 2 deletions word/src/main/java/com/one/word/ui/WordDetailViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.one.word.ui.adapters.SpellingViewItem
import com.one.word.ui.adapters.TextViewItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.suspendCancellableCoroutine
import java.util.*

Expand Down Expand Up @@ -96,7 +95,7 @@ class WordDetailViewModel(
}

val translateAsync = viewModelScope.async(handler + Dispatchers.IO) {
translateUseCase.execute(TranslateUseCase.Param(listOf(text), inputCode, Locale.getDefault().language)).toSuccess()?.data?.first()
translateUseCase.execute(TranslateUseCase.Param(listOf(text), inputCode, Locale.getDefault().language)).toSuccess()?.data?.firstOrNull()
}

val dictionaryAsync = viewModelScope.async(handler + Dispatchers.IO) {
Expand Down

0 comments on commit e03a205

Please sign in to comment.