-
Notifications
You must be signed in to change notification settings - Fork 0
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
taeheeL
wants to merge
9
commits into
develop
Choose a base branch
from
feature/mdir/korean
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,024
−256
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d63be3
set next button state using livedata
taeheeL 533bfb4
translate with coroutine
taeheeL 099876e
my diary api
taeheeL 02ce8de
fix translate api call for not suspend
daehwan2yo b988fb6
fix translate start
taeheeL eeea0b7
fix remove space logic
taeheeL 9167c23
translate thread manage
taeheeL 0352d3f
connect foreign step
taeheeL bc02a06
update logic
taeheeL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/sopt/smeme/bridge/controller/MyDiaryApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/sopt/smeme/bridge/controller/MyDiaryResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
app/src/main/java/com/sopt/smeme/bridge/controller/PapagoAPI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/sopt/smeme/bridge/controller/RequestWriteDiaryDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/sopt/smeme/bridge/controller/TranslateResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/sopt/smeme/business/viewmodel/ForeignViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/sopt/smeme/business/viewmodel/Step1ViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/sopt/smeme/business/viewmodel/Step2ViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.