Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
hoanganhtuan95ptit committed Aug 21, 2024
1 parent a9fb907 commit e0309b7
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 99 deletions.
65 changes: 45 additions & 20 deletions constant/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,51 @@ project.ext {

hoanganhtuan95Dependencies = [

base : "com.github.hoanganhtuan95ptit.core:base:1.1.16",
android : "com.github.hoanganhtuan95ptit.core:android:1.1.16",
base : project(':core:base'),
android : project(':core:android'),

job : project(':library:job'),
task : project(':library:task'),
state : project(':library:state'),
image : project(':library:image'),
binding : project(':library:binding'),
adapter : project(':library:adapter'),

analytics : project(':library:analytics'),
analytics_log : project(':library:analytics-log'),
analytics_firebase : project(':library:analytics-firebase'),

crashlytics : project(':library:crashlytics'),
crashlytics_log : project(':library:crashlytics-log'),
crashlytics_firebase: project(':library:crashlytics-firebase'),

detect : project(':library:detect'),
detect_mlkit : project(':library:detect-mlkit'),

translate : project(':library:translate'),
translate_mlkit : project(':library:translate-mlkit'),

// base : "com.github.hoanganhtuan95ptit.core:base:1.1.17",
// android : "com.github.hoanganhtuan95ptit.core:android:1.1.17",
//
// task : "com.github.hoanganhtuan95ptit.core:task:1.1.17",
// state : "com.github.hoanganhtuan95ptit.core:state:1.1.17",
// image : "com.github.hoanganhtuan95ptit.core:image:1.1.17",
// adapter : "com.github.hoanganhtuan95ptit.core:adapter:1.1.17",
//
// analytics : "com.github.hoanganhtuan95ptit.core:analytics:1.1.17",
// analytics_log : "com.github.hoanganhtuan95ptit.core:analytics-log:1.1.17",
// analytics_firebase : "com.github.hoanganhtuan95ptit.core:analytics-firebase:1.1.17",
//
// crashlytics : "com.github.hoanganhtuan95ptit.core:crashlytics:1.1.17",
// crashlytics_log : "com.github.hoanganhtuan95ptit.core:crashlytics-log:1.1.17",
// crashlytics_firebase: "com.github.hoanganhtuan95ptit.core:crashlytics-firebase:1.1.17",
//
// detect : "com.github.hoanganhtuan95ptit.core:detect:1.1.17",
// detect_mlkit : "com.github.hoanganhtuan95ptit.core:detect-mlkit:1.1.17",
//
// translate : "com.github.hoanganhtuan95ptit.core:translate:1.1.17",
// translate_mlkit : "com.github.hoanganhtuan95ptit.core:translate-mlkit:1.1.17"

task : "com.github.hoanganhtuan95ptit.core:task:1.1.16",
state : "com.github.hoanganhtuan95ptit.core:state:1.1.16",
image : "com.github.hoanganhtuan95ptit.core:image:1.1.16",
adapter : "com.github.hoanganhtuan95ptit.core:adapter:1.1.16",

analytics : "com.github.hoanganhtuan95ptit.core:analytics:1.1.16",
analytics_log : "com.github.hoanganhtuan95ptit.core:analytics-log:1.1.16",
analytics_firebase : "com.github.hoanganhtuan95ptit.core:analytics-firebase:1.1.16",

crashlytics : "com.github.hoanganhtuan95ptit.core:crashlytics:1.1.16",
crashlytics_log : "com.github.hoanganhtuan95ptit.core:crashlytics-log:1.1.16",
crashlytics_firebase: "com.github.hoanganhtuan95ptit.core:crashlytics-firebase:1.1.16",

detect : "com.github.hoanganhtuan95ptit.core:detect:1.1.16",
detect_mlkit : "com.github.hoanganhtuan95ptit.core:detect-mlkit:1.1.16",

translate : "com.github.hoanganhtuan95ptit.core:translate:1.1.16",
translate_mlkit : "com.github.hoanganhtuan95ptit.core:translate-mlkit:1.1.16"
]
}
5 changes: 3 additions & 2 deletions library/analytics-log/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apply from: "../../constant/build.gradle"

android {
namespace "com.simple.analytics.log"

defaultConfig {
compileSdk project.ext.compileSdkVersion
minSdkVersion project.ext.minSdkVersion
Expand All @@ -34,7 +34,8 @@ android {

dependencies {

implementation project(path: ':library:analytics')
implementation project(':core:base')
implementation project(':library:analytics')

implementation koinDependencies.values()
implementation androidxDependencies.values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.simple.analytics.log

import android.annotation.SuppressLint
import com.simple.analytics.Analytics
import com.simple.core.utils.extentions.toJson

class LogAnalytics : Analytics {

@SuppressLint("MissingPermission")
override suspend fun execute(eventName: String, vararg params: Pair<String, String>) {

android.util.Log.d("LogAnalytics", "eventName:$eventName params:$params")
android.util.Log.d("LogAnalytics", "eventName:$eventName params:${params.toJson()}")
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package com.simple.crashlytics.firebase

import com.google.firebase.FirebaseApp
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.simple.crashlytics.Crashlytics

class FirebaseCrashlytics(private val firebaseApp: FirebaseApp) : Crashlytics {

private val firebaseCrashlytics by lazy {

firebaseApp.get(FirebaseCrashlytics::class.java) as FirebaseCrashlytics
}
class FirebaseCrashlytics : Crashlytics {

override suspend fun execute(eventName: String, throwable: Throwable, vararg params: Pair<String, String>) {

val map = hashMapOf("eventName" to eventName, *params)

firebaseCrashlytics.recordException(RuntimeException(map.toString(), throwable))
FirebaseCrashlytics.getInstance().recordException(RuntimeException(map.toString(), throwable))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FirebaseCrashlyticsInitializer : Initializer<Unit> {

loadKoinModules(listOf(module {

single { FirebaseCrashlytics(get()) } bind Crashlytics::class
single { FirebaseCrashlytics() } bind Crashlytics::class

}))

Expand Down
1 change: 1 addition & 0 deletions library/crashlytics-log/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ android {

dependencies {

implementation project(':core:base')
implementation project(':library:crashlytics')

implementation koinDependencies.values()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.simple.crashlytics.log

import com.simple.core.utils.extentions.toJson
import com.simple.crashlytics.Crashlytics

class LogCrashlytics() : Crashlytics {

override suspend fun execute(eventName: String, throwable: Throwable, vararg params: Pair<String, String>) {

android.util.Log.d("LogCrashlytics", "eventName:${eventName} params:$params", throwable)
android.util.Log.d("LogCrashlytics", "eventName:${eventName} params:${params.toJson()}", throwable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,14 @@ abstract class LanguageDetectStateTask : Task<LanguageDetectStateTask.Param, Det
}
}

override suspend fun logStart(param: Param, taskId: String) {
}

override suspend fun logSuccess(param: Param, taskId: String) {
}

override suspend fun logFailed(param: Param, taskId: String, throwable: Throwable) {
}

data class Param(val languageCode: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@ import com.simple.task.Task

interface DetectStateTask : Task<DetectStateTask.Param, Pair<DetectProvider, DetectState>> {

override suspend fun logStart(param: Param, taskId: String) {
}

override suspend fun logSuccess(param: Param, taskId: String) {
}

override suspend fun logFailed(param: Param, taskId: String, throwable: Throwable) {
}

data class Param(val languageCode: String)
}
2 changes: 1 addition & 1 deletion library/task/src/main/java/com/simple/task/Task.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interface Task<Param, Result> {

suspend fun logFailed(param: Param, taskId: String, throwable: Throwable) {

logCrashlytics("${tag()}_${taskId}_failed", throwable)
if (throwable !is LowException) logCrashlytics("${tag()}_${taskId}_failed", throwable)
}

suspend fun executeTask(param: Param): Result {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package com.simple.translate.mlkit

object Constants {

const val TRANSLATE_MLKIT = "TRANSLATE_MLKIT"
}
val COUNTRY_LANGUAGE_MAP: Map<String, String> = hashMapOf(
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import com.simple.state.ResultState
import com.simple.state.isFailed
import com.simple.state.toSuccess
import com.simple.translate.data.tasks.TranslateTask
import com.simple.translate.entities.TranslateRequest
import com.simple.translate.entities.TranslateResponse
import com.simple.translate.mlkit.COUNTRY_LANGUAGE_MAP
import com.simple.translate.mlkit.utils.exts.checkModelDownloaded
import com.simple.translate.mlkit.utils.exts.downloadModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.suspendCancellableCoroutine
Expand All @@ -28,24 +32,62 @@ class MlkitTranslateTask(
private val context: Context
) : TranslateTask {

private val map: Map<String, String> = hashMapOf(
)

override suspend fun tag(): String {

return "mlkit_translate_task"
}

override suspend fun executeTask(param: TranslateTask.Param): List<String> = withContext(coroutineContext) {
override suspend fun executeTask(param: TranslateTask.Param): List<TranslateResponse> {

val list = param.input.groupBy {

it.languageCode
}.map {

translateInput(it.value, it.key, param.outputCode)
}

return list.awaitAll().flatMap { it }
}

private suspend fun translateInput(input: List<TranslateRequest>, inputLanguageCode: String, outputLanguageCode: String) = CoroutineScope(coroutineContext).async {

val state = kotlin.runCatching {

val list = translateText(input.map { it.text }, inputLanguageCode, outputLanguageCode)

ResultState.Success(list)
}.getOrElse {

ResultState.Failed(it)
}


return@async input.mapIndexed { index, translateRequest ->

val translateState = if (state is ResultState.Failed) {
state
} else {
ResultState.Success(state.toSuccess()?.data.orEmpty().getOrNull(index).orEmpty())
}

TranslateResponse(
text = translateRequest.text,
translateState = translateState
)
}
}

private suspend fun translateText(text: List<String>, inputLanguageCode: String, outputLanguageCode: String) = withContext(coroutineContext) {

val inputCode = map[param.inputCode] ?: param.inputCode
val inputCode = COUNTRY_LANGUAGE_MAP[inputLanguageCode] ?: inputLanguageCode

val outputCode = map[param.outputCode] ?: param.outputCode
val outputCode = COUNTRY_LANGUAGE_MAP[outputLanguageCode] ?: outputLanguageCode


if (inputCode == outputCode) {

return@withContext param.text
return@withContext text
}


Expand Down Expand Up @@ -102,7 +144,7 @@ class MlkitTranslateTask(
val translator = Translation.getClient(options)


val translateStateList = param.text.map {
val translateStateList = text.map {

async {
translate(translator, it)
Expand All @@ -125,6 +167,19 @@ class MlkitTranslateTask(
return@withContext list
}

private suspend fun translate(translator: Translator, text: String): ResultState<String> = suspendCancellableCoroutine { continuation ->

translator.translate(text).addOnSuccessListener { translatedText ->

continuation.resumeActive(ResultState.Success(translatedText))
}.addOnFailureListener {

logCrashlytics("mlkit_translate_failed", it, "input" to text)

continuation.resumeActive(ResultState.Failed(it))
}
}

private suspend fun downloadModelIfNeededTimeout(languageCode: String): ResultState<Boolean> {

val remoteModel = TranslateRemoteModel.Builder(languageCode).build()
Expand All @@ -151,17 +206,4 @@ class MlkitTranslateTask(
remoteModel.downloadModel()
}
}

private suspend fun translate(translator: Translator, text: String): ResultState<String> = suspendCancellableCoroutine { continuation ->

translator.translate(text).addOnSuccessListener { translatedText ->

continuation.resumeActive(ResultState.Success(translatedText))
}.addOnFailureListener {

logCrashlytics("mlkit_translate_failed", it, "input" to text)

continuation.resumeActive(ResultState.Failed(it))
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.simple.translate

import android.content.Context
import androidx.startup.Initializer
import com.simple.translate.data.usecase.TranslateUseCase
import org.koin.core.context.loadKoinModules
import org.koin.dsl.module

Expand All @@ -12,8 +11,6 @@ class TranslateInitializer : Initializer<Unit> {

loadKoinModules(listOf(module {

single { TranslateUseCase(getAll()) }

}))

return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@ import com.simple.translate.entities.TranslateState

interface TranslateStateTask : Task<TranslateStateTask.Param, Pair<TranslateProvider, TranslateState>> {

override suspend fun logStart(param: Param, taskId: String) {
}

override suspend fun logSuccess(param: Param, taskId: String) {
}

override suspend fun logFailed(param: Param, taskId: String, throwable: Throwable) {
}

data class Param(val languageCode: String)
}
Loading

0 comments on commit e0309b7

Please sign in to comment.