Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature issue48 #55

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

ext {
koin_version = "2.0.0-alpha-3"
koin_version = "2.0.0-alpha-6"
}

android {
Expand Down Expand Up @@ -50,15 +50,15 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'

// Google & Android libraries
implementation "androidx.appcompat:appcompat:1.1.0-alpha01"
implementation "com.google.android.material:material:1.1.0-alpha02"
implementation "androidx.appcompat:appcompat:1.1.0-alpha03"
implementation "com.google.android.material:material:1.1.0-alpha05"
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha2"
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha3"
withPlayServicesImplementation('com.crashlytics.sdk.android:crashlytics:2.9.7@aar') {
transitive = true
}
withPlayServicesImplementation 'com.google.firebase:firebase-core:16.0.6'
withPlayServicesImplementation 'com.google.firebase:firebase-config:16.1.2'
withPlayServicesImplementation 'com.google.firebase:firebase-core:16.0.8'
withPlayServicesImplementation 'com.google.firebase:firebase-config:16.4.1'

// Other libraries
implementation "com.squareup.retrofit2:retrofit:2.5.0"
Expand All @@ -70,9 +70,9 @@ dependencies {
implementation "org.koin:koin-androidx-viewmodel:$koin_version"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha01"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha01"
testImplementation "androidx.arch.core:core-testing:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha03"
testImplementation "androidx.arch.core:core-testing:2.0.1"

// Tests
testImplementation "junit:junit:4.12"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/anthony/deepl/openl/DeepLApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import com.anthony.deepl.openl.di.deeplAppModule

import com.anthony.deepl.openl.util.FirebaseManager
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import org.koin.core.logger.Level

Expand All @@ -20,6 +21,7 @@ class DeepLApplication : Application() {
setupLogTool()
startKoin {
logger(Level.ERROR)
androidContext(this@DeepLApplication)
modules(listOf(deeplAppModule))
}
}
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/anthony/deepl/openl/di/app_module.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.anthony.deepl.openl.di

import com.anthony.deepl.openl.backend.DeepLService
import com.anthony.deepl.openl.util.FirebaseManager
import com.anthony.deepl.openl.view.translation.TranslationViewModel
import org.koin.android.ext.koin.androidApplication
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

val deeplAppModule = module {

// ViewModel for Translation View
viewModel { TranslationViewModel(get()) }
viewModel { TranslationViewModel(androidApplication(), get(), get()) }

// Provide DeepL Data Repository
single { createDeepLService() }
single { FirebaseManager(androidContext()) }

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.anthony.deepl.openl.model

data class CompletedTranslation(val request: TranslationRequest, val response: TranslationResponse)
11 changes: 11 additions & 0 deletions app/src/main/java/com/anthony/deepl/openl/model/ResultResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.anthony.deepl.openl.model

sealed class ResultResource<out T : Any>

data class SuccessResource<out T : Any>(val data: T) : ResultResource<T>()

data class FailureResource(val error: Throwable?, val message: String?) : ResultResource<Nothing>()

data class LoadingResource(val message: String? = null) : ResultResource<Nothing>()

data class IdleResource(val message: String? = null) : ResultResource<Nothing>()
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import java.text.BreakIterator
import java.util.ArrayList

data class TranslationRequest(
private val sentence: String,
private val fromLanguage: String,
private val toLanguage: String,
private val userPreferredLanguages: List<String>,
val sentence: String,
val fromLanguage: String,
val toLanguage: String,
val userPreferredLanguages: List<String>,
@field:Json(name="jsonrpc") private val jsonRpc: String = "2.0",
@field:Json(name="method") private val method: String = "LMT_handle_jobs") {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import android.content.ClipboardManager
import android.content.Context
import android.content.Context.CLIPBOARD_SERVICE
import android.content.res.Resources
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.Fragment


// DIMENSIONS
val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()


// CLIPBOARD
fun Fragment.getClipboardText(): String? {
var text: String? = null
Expand All @@ -39,6 +45,7 @@ fun Fragment.copyToClipboard(text: String) {
}
}


// KEYBAORD
fun Activity.hideKeyboard() {
hideKeyboard(if (currentFocus == null) View(this) else currentFocus)
Expand All @@ -53,4 +60,31 @@ fun Fragment.hideKeyboard() {
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}


// TEXT VIEWS
fun EditText.onTextChanged(lambda: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {}

override fun onTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
lambda.invoke(p0.toString())
}

override fun afterTextChanged(editable: Editable) {}
})
}


fun TextView.onTextChanged(lambda: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {}

override fun onTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
lambda.invoke(p0.toString())
}

override fun afterTextChanged(editable: Editable) {}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import androidx.appcompat.widget.Toolbar

import com.anthony.deepl.openl.manager.LanguageManager
import com.anthony.deepl.openl.R
import com.anthony.deepl.openl.util.FirebaseManager
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
import org.koin.androidx.viewmodel.ext.viewModel

import java.util.Locale

Expand All @@ -27,8 +27,8 @@ class TranslationActivity : AppCompatActivity(), TranslationFragment.OnFragmentI
private const val SPEECH_TO_TEXT_REQUEST_CODE = 32
}

private lateinit var mTranslationFragment: TranslationFragment
private lateinit var mFirebaseManager: FirebaseManager
private val viewModel by viewModel<TranslationViewModel>()
private lateinit var translationFragment: TranslationFragment

override val currentMediaVolume: Int
get() {
Expand All @@ -49,22 +49,18 @@ class TranslationActivity : AppCompatActivity(), TranslationFragment.OnFragmentI
if (intent?.action == Intent.ACTION_SEND && intent.type == "text/plain") {
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
if (sharedText != null) {
mTranslationFragment.setToTranslateText(sharedText)
translationFragment.setToTranslateText(sharedText)
}
}

// We init and fetch values from Firebase analytics and remote config
mFirebaseManager = FirebaseManager(this)
mFirebaseManager.fetchRemoteConfigValues()
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == SPEECH_TO_TEXT_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
val text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)[0]
mTranslationFragment.setToTranslateText(text)
logEvent("speech_to_text_success", null)
translationFragment.setToTranslateText(text)
viewModel.logEvent("speech_to_text_success", null)
}
}

Expand All @@ -76,22 +72,18 @@ class TranslationActivity : AppCompatActivity(), TranslationFragment.OnFragmentI

try {
startActivityForResult(intent, SPEECH_TO_TEXT_REQUEST_CODE)
logEvent("speech_to_text_displayed", null)
viewModel.logEvent("speech_to_text_displayed", null)
} catch (e: ActivityNotFoundException) {
mTranslationFragment.view?.let {
translationFragment.view?.let {
Snackbar.make(it, R.string.speech_to_text_error, Snackbar.LENGTH_SHORT).show()
}
Timber.e(e)
}
}

override fun logEvent(event: String, bundle: Bundle?) {
mFirebaseManager.logEvent(event, bundle)
}

private fun initViews() {
setSupportActionBar(toolbar as Toolbar)
mTranslationFragment = supportFragmentManager.findFragmentById(R.id.main_fragment) as TranslationFragment
translationFragment = supportFragmentManager.findFragmentById(R.id.main_fragment) as TranslationFragment
}

}
}
Loading