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

[#38] 번역 API 연결 후 기능 구현 #39

Open
wants to merge 9 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
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.8.9'

// OkHttp
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'

// Json
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0'

Expand All @@ -119,6 +123,9 @@ dependencies {
// DataStore
implementation 'androidx.datastore:datastore-preferences:1.0.0'

// add floating action button
implementation 'com.google.android.material:material:1.3.0-alpha02'

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
android:name=".presentation.view.HomeActivity"
android:exported="false"/>
<activity
android:name=".presentation.view.WriteDiaryKoreanActivity"
android:name=".presentation.view.WriteDiaryStep1Activity"
android:exported="false"
android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
/>
Expand Down Expand Up @@ -77,6 +77,10 @@
android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
android:exported="false"
/>
<activity android:name=".presentation.view.WriteDiaryForeignActivity"
android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
android:exported="false"
/>
</application>

</manifest>
9 changes: 8 additions & 1 deletion app/src/main/java/com/sopt/smeme/Smeme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import android.app.Application
import com.kakao.sdk.common.KakaoSdk
import com.sopt.smeme.BuildConfig.KAKAO_API_KEY
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber

@HiltAndroidApp
class Smeme : Application() {
override fun onCreate() {
super.onCreate()

setupTimber()
KakaoSdk.init(this, KAKAO_API_KEY)
}
}
}

private fun setupTimber() {
if (BuildConfig.DEBUG)
Timber.plant(Timber.DebugTree()) // 팀버 초기화
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ object ApiConnectionModule {

}

@Module
@InstallIn(ViewModelComponent::class)
object MDir {
@Provides
@ViewModelScoped
fun provideMyDiaries(@ConnectCluster(Cluster.ORIGIN) retrofit: Retrofit): MyDiaryApi =
retrofit.create(MyDiaryApi::class.java)

@Provides
@ViewModelScoped
fun providePapago(@ConnectCluster(Cluster.PAPAGO) retrofit: Retrofit): PapagoAPI =
retrofit.create(PapagoAPI::class.java)
}

object ODir {

}
object ODir
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sopt.smeme.bridge.controller

import retrofit2.http.Body
import retrofit2.http.GET

interface MyDiaryApi {
@GET
fun getDiaries(@Body request: RequestWriteDiaryDto): MyDiartResponse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun getDiaries(@Body request: RequestWriteDiaryDto): MyDiartResponse
fun getDiaries(@Body request: RequestWriteDiaryDto): Call<MyDiartResponse>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sopt.smeme.bridge.controller

import kotlinx.serialization.Serializable

@Serializable
data class MyDiartResponse(
val status: Int,
val success: Boolean,
val message:String,
val data: MyDiaryData
)
@Serializable
data class MyDiaryData(
val id: Int
)
19 changes: 19 additions & 0 deletions app/src/main/java/com/sopt/smeme/bridge/controller/PapagoAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.sopt.smeme.bridge.controller

import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.Header
import retrofit2.http.POST

interface PapagoAPI {
@FormUrlEncoded
@POST("v1/papago/n2mt")
fun transferPapago(
@Field("source") source: String,
@Field("target") target: String,
@Field("text") text: String,
@Header("X-Naver-Client-Id") clientId: String,
@Header("X-Naver-Client-Secret") clientSecret : String
): Call<TranslateResult>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sopt.smeme.bridge.controller

import kotlinx.serialization.SerialName

@kotlinx.serialization.Serializable
data class RequestWriteDiaryDto(
@SerialName("content")
val content : String,
@SerialName("targetLang")
val targetLang : String,
@SerialName("topic")
val topic : String,
@SerialName("isPublic")
val isPublic: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.sopt.smeme.bridge.controller

import com.google.gson.annotations.SerializedName

data class TranslateResult(
@SerializedName("message")
val message: Message
) {
data class Message(
@SerializedName("result")
val result: Result
) {
data class Result(
@SerializedName("srcLangType")
val srcLangType: String,
@SerializedName("tarLangType")
val tarLangType: String,
@SerializedName("translatedText")
val translatedText: String
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.sopt.smeme.business.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel

class ForeignViewModel : ViewModel() {
val diary: MutableLiveData<String> = MutableLiveData("")
val isDiarySuit: LiveData<Boolean> =
Transformations.map(diary) { isValidDiaryFormat(it) }

private val _isCompleteActive = MutableLiveData(false)
val isCompleteActive get() = _isCompleteActive

// val sourceDiary: MutableLiveData<String> = MutableLiveData()
val content: LiveData<String> = Transformations.map(diary) { it }

fun updateText(newText: String) {
diary.value = newText
}

private fun isValidDiaryFormat(diary: String): Boolean {
if (diary.replace("[^a-zA-Z]".toRegex(), "").length >= 10) {
return true
}
return false
}

fun setCompleteState() {
_isCompleteActive.value = (isDiarySuit.value == true)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.sopt.smeme.business.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.sopt.smeme.BuildConfig
import com.sopt.smeme.bridge.controller.PapagoAPI
import com.sopt.smeme.bridge.controller.TranslateResult
import dagger.hilt.android.lifecycle.HiltViewModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject

@HiltViewModel
class Step1ViewModel @Inject constructor(
private val papagoAPI: PapagoAPI
) : ViewModel() {
val diary: MutableLiveData<String> = MutableLiveData("")
val isDiarySuit: LiveData<Boolean> =
Transformations.map(diary) { isValidDiaryFormat(it) }

val sourceDiary: MutableLiveData<String> = MutableLiveData()
val content: LiveData<String> = Transformations.map(sourceDiary) { it }

private val _isNextActive = MutableLiveData(false)
val isNextActive get() = _isNextActive

fun updateText(newText: String) {
sourceDiary.value = newText
}

private fun isValidDiaryFormat(diary: String) = diary.filterNot{it.isWhitespace()}.length >= 10

fun setNextState() {
_isNextActive.value = (isDiarySuit.value == true)
}

fun translate(
text: String,
onCompleted: (String) -> Unit = {},
onError: (Throwable) -> Unit = {}
) {
papagoAPI.transferPapago(
"ko", "en", text,
BuildConfig.CLIENT_ID, BuildConfig.CLIENT_SECRET
).enqueue(object : Callback<TranslateResult> {
override fun onResponse(
call: Call<TranslateResult>,
response: Response<TranslateResult>
) {
if (response.isSuccessful) {
var result = response.body()?.message?.result?.translatedText

if (result != null) {
onCompleted.invoke(result)
Log.d("번역 성공", "변역된 일기:$result")
} else {
throw IllegalStateException("TODO : change to SmemeException")
}
} else {
onError.invoke(IllegalStateException("번역이 정상적으로 수행되지 않았습니다. ${response.code()} : ${response.message()}"))
}
}

override fun onFailure(call: Call<TranslateResult>, t: Throwable) {
onError.invoke(t)
}
})
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.sopt.smeme.business.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class Step2ViewModel @Inject constructor(): ViewModel() {
val diary: MutableLiveData<String> = MutableLiveData("")
val isDiarySuit: LiveData<Boolean> =
Transformations.map(diary) { isValidDiaryFormat(it) }

private val _isCompleteActive = MutableLiveData(false)
val isCompleteActive get() = _isCompleteActive

// val sourceDiary: MutableLiveData<String> = MutableLiveData()
val content: LiveData<String> = Transformations.map(diary) { it }

fun updateText(newText: String) {
diary.value = newText
}

private fun isValidDiaryFormat(diary: String) : Boolean {
if (diary.replace("[^a-zA-Z]".toRegex(),"").length >= 10) {
return true
}
return false
}

fun setCompleteState() {
_isCompleteActive.value = (isDiarySuit.value == true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ class BinderActivity : ViewBoundActivity<ActivityBinderBinding>(R.layout.activit
startActivity(toHomeActivity)
}

binding.btnWriteKorenDiary.setOnClickListener {
val toWriteKoreanDiaryActivity = Intent(this, WriteDiaryStep1Activity::class.java)
startActivity(toWriteKoreanDiaryActivity)
}

}
}
Loading