From 245f6a502df2108918d108d3b0cec4b79dea4660 Mon Sep 17 00:00:00 2001 From: Sreang Rathanak Date: Tue, 26 Dec 2023 20:04:28 +0700 Subject: [PATCH] Add add post request --- .../smartbar/SpellSuggestionAdapter.kt | 15 ++-- .../keyboard/smartbar/SpellSuggestionItem.kt | 4 - .../smartbar/SpellSuggestionManager.kt | 84 ++++++++++++------- .../rathanak/khmerroman/request/ApiClient.kt | 2 +- .../rathanak/khmerroman/request/ApiService.kt | 6 +- .../khmerroman/request/SpellCheckResultDTO.kt | 35 +++++++- 6 files changed, 100 insertions(+), 46 deletions(-) delete mode 100644 app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionItem.kt diff --git a/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionAdapter.kt b/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionAdapter.kt index 80b2c26..8d51616 100644 --- a/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionAdapter.kt +++ b/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionAdapter.kt @@ -17,8 +17,9 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.flexbox.FlexboxLayout import com.rathanak.khmerroman.R import com.rathanak.khmerroman.keyboard.R2KhmerService +import com.rathanak.khmerroman.request.SpellCheckResultDTO -class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val context: Context, private val suggestionsList: java.util.ArrayList) : BaseAdapter() { +class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val context: Context, var suggestionsList: java.util.ArrayList) : BaseAdapter() { override fun getCount(): Int { return suggestionsList.size } @@ -35,7 +36,7 @@ class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val var convertView = convertView convertView = LayoutInflater.from(context).inflate(R.layout.spell_suggestion_item, parent, false) val serialNum = convertView.findViewById(R.id.typoText) as TextView - val typoWord = suggestionsList[position].typoWord + val typoWord = suggestionsList[position].word serialNum.text = typoWord val btnSpellItemClose = convertView.findViewById(R.id.btnSpellItemClose) as ImageButton @@ -50,14 +51,16 @@ class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val LinearLayout.LayoutParams.WRAP_CONTENT ) params.setMargins(4,4,4,4) - suggestionsList[position].wordsSuggestion.forEach { + val startIndex = suggestionsList[position].startIndex + val endIndex = suggestionsList[position].endIndex + suggestionsList[position].suggestions.forEach { val btnWord = Button(ContextThemeWrapper(context, R.style.ButtonSpellSuggestion), null, R.style.ButtonSpellSuggestion) btnWord.minHeight = 0 btnWord.minWidth = 0 - val suggestion = it - btnWord.text = suggestion.word + val word = it + btnWord.text = word btnWord.setOnClickListener { - r_2_khmer.setCurrentText(typoWord, suggestion.word, suggestion.startPos, suggestion.endPos + 1) + r_2_khmer.setCurrentText(typoWord, word, startIndex, endIndex + 1) suggestionsList.removeAt(position); notifyDataSetChanged(); } diff --git a/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionItem.kt b/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionItem.kt deleted file mode 100644 index 883974c..0000000 --- a/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionItem.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.rathanak.khmerroman.keyboard.smartbar - -class WordSuggestion(val word: String, val startPos: Int, val endPos: Int) {} -class SpellSuggestionItem(val typoWord: String, val wordsSuggestion: List) {} diff --git a/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionManager.kt b/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionManager.kt index 8d94ec4..49f4205 100644 --- a/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionManager.kt +++ b/app/src/main/java/com/rathanak/khmerroman/keyboard/smartbar/SpellSuggestionManager.kt @@ -8,42 +8,50 @@ import com.rathanak.khmerroman.R import com.rathanak.khmerroman.keyboard.R2KhmerService import com.rathanak.khmerroman.keyboard.common.Styles import com.rathanak.khmerroman.request.ApiClient +import com.rathanak.khmerroman.request.SpellCheckRequestDTO +import com.rathanak.khmerroman.request.SpellCheckRespondDTO import com.rathanak.khmerroman.request.SpellCheckResultDTO import kotlinx.android.synthetic.main.smartbar.view.btnOpenApp import kotlinx.android.synthetic.main.smartbar.view.smartbar import kotlinx.android.synthetic.main.spell_suggestion.view.spellSuggestionList import kotlinx.android.synthetic.main.spell_suggestion.view.noDataText import kotlinx.android.synthetic.main.spell_suggestion.view.noInternetConnection +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import retrofit2.Call import retrofit2.Callback import retrofit2.Response class SpellSuggestionManager(private val r_2_khmer: R2KhmerService) { + private var spellCheckJob: Job? = null var currentSentence: String = "" var spellSuggestionView: LinearLayout? = null private var isDarkMood: Boolean = false var spellSuggestionAdapter: SpellSuggestionAdapter? = null - var spellSuggestionItems: ArrayList = ArrayList() + var spellSuggestionItems: ArrayList = ArrayList() fun createSpellSuggestionView(): LinearLayout { var spellSuggestionView = View.inflate(r_2_khmer.context, R.layout.spell_suggestion, null) as LinearLayout this.spellSuggestionView = spellSuggestionView val listView = spellSuggestionView.spellSuggestionList - var wordsList: ArrayList = ArrayList() - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) - spellSuggestionItems.add(SpellSuggestionItem("កាពិនិត្យ", wordsList)) - spellSuggestionItems.add(SpellSuggestionItem("កាពិនិត្យ", wordsList)) - spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) - spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) - spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) - spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) +// var wordsList: ArrayList = ArrayList() +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// wordsList.add(WordSuggestion("ការពិនិត្យ", 14, 22)) +// spellSuggestionItems.add(SpellSuggestionItem("កាពិនិត្យ", wordsList)) +// spellSuggestionItems.add(SpellSuggestionItem("កាពិនិត្យ", wordsList)) +// spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) +// spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) +// spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) +// spellSuggestionItems.add(SpellSuggestionItem( "កាពិនិត្យ", wordsList)) spellSuggestionAdapter = SpellSuggestionAdapter(r_2_khmer, r_2_khmer.context, spellSuggestionItems) listView.adapter = spellSuggestionAdapter @@ -84,22 +92,36 @@ class SpellSuggestionManager(private val r_2_khmer: R2KhmerService) { } currentSentence = sentence + spellCheckJob?.cancel() + spellCheckJob = GlobalScope.launch(Dispatchers.Main) { + delay(500) + spellChecking(currentSentence) + } + } - Log.i("koko", "CALL api") -// val call = ApiClient.apiService.spellCheckIng(1) -// call.enqueue(object : Callback { -// override fun onResponse(call: Call, response: Response) { -// if (response.isSuccessful) { -// val post = response.body() -// // Handle the retrieved post data -// } else { -// // Handle error -// } -// } -// -// override fun onFailure(call: Call, t: Throwable) { -// // Handle failure -// } -// }) + private fun spellChecking(searchText: String) { + val requestBody = SpellCheckRequestDTO("អត្ថបទសម្រាប់ការពិនិត្យអក្ខរាវិរុទ្") + val call = ApiClient.apiService.spellCheckIng(requestBody) + call.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + // Handle the retrieved spell check data + val responseBody = response.body() + if (responseBody != null) { + // TODO: update icon status + spellSuggestionAdapter?.suggestionsList = responseBody.results + spellSuggestionAdapter?.notifyDataSetChanged() + } + } else { + // Handle error + // TODO: update icon status + } + } + + override fun onFailure(call: Call, t: Throwable) { + // Handle failure + // TODO: update icon status + } + }) } } \ No newline at end of file diff --git a/app/src/main/java/com/rathanak/khmerroman/request/ApiClient.kt b/app/src/main/java/com/rathanak/khmerroman/request/ApiClient.kt index e65d0e6..54bfc46 100644 --- a/app/src/main/java/com/rathanak/khmerroman/request/ApiClient.kt +++ b/app/src/main/java/com/rathanak/khmerroman/request/ApiClient.kt @@ -4,7 +4,7 @@ import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory object RetrofitClient { - private const val BASE_URL = "https://api.khmerlang.com/v1" + private const val BASE_URL = "https://api.khmerlang.com/" val retrofit: Retrofit by lazy { Retrofit.Builder() diff --git a/app/src/main/java/com/rathanak/khmerroman/request/ApiService.kt b/app/src/main/java/com/rathanak/khmerroman/request/ApiService.kt index 4fd1fe6..5352e64 100644 --- a/app/src/main/java/com/rathanak/khmerroman/request/ApiService.kt +++ b/app/src/main/java/com/rathanak/khmerroman/request/ApiService.kt @@ -1,10 +1,12 @@ package com.rathanak.khmerroman.request import retrofit2.Call +import retrofit2.http.Body import retrofit2.http.GET +import retrofit2.http.POST import retrofit2.http.Path interface ApiService { - @GET("/spelling-check") - fun spellCheckIng(@Path("id") postId: Int): Call + @POST("/v1/spelling-check") + fun spellCheckIng(@Body requestBody: SpellCheckRequestDTO): Call } \ No newline at end of file diff --git a/app/src/main/java/com/rathanak/khmerroman/request/SpellCheckResultDTO.kt b/app/src/main/java/com/rathanak/khmerroman/request/SpellCheckResultDTO.kt index 92fbcb3..47e613b 100644 --- a/app/src/main/java/com/rathanak/khmerroman/request/SpellCheckResultDTO.kt +++ b/app/src/main/java/com/rathanak/khmerroman/request/SpellCheckResultDTO.kt @@ -1,4 +1,35 @@ package com.rathanak.khmerroman.request -class SpellCheckResultDTO { -} \ No newline at end of file +data class SpellCheckResultDTO( + val startIndex: Int, + val endIndex: Int, + val word: String, + val suggestions: Array, + val scores: Array +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as SpellCheckResultDTO + + if (startIndex != other.startIndex) return false + if (endIndex != other.endIndex) return false + if (word != other.word) return false + if (!suggestions.contentEquals(other.suggestions)) return false + return scores.contentEquals(other.scores) + } + + override fun hashCode(): Int { + var result = startIndex + result = 31 * result + endIndex + result = 31 * result + word.hashCode() + result = 31 * result + suggestions.contentHashCode() + result = 31 * result + scores.contentHashCode() + return result + } +} + +data class SpellCheckRespondDTO(val results: ArrayList) + +data class SpellCheckRequestDTO (val text: String) \ No newline at end of file