Skip to content

Commit

Permalink
Add add post request
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreang Rathanak committed Dec 26, 2023
1 parent 0645fd9 commit 245f6a5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpellSuggestionItem>) : BaseAdapter() {
class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val context: Context, var suggestionsList: java.util.ArrayList<SpellCheckResultDTO>) : BaseAdapter() {
override fun getCount(): Int {
return suggestionsList.size
}
Expand All @@ -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
Expand All @@ -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();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<SpellSuggestionItem> = ArrayList()
var spellSuggestionItems: ArrayList<SpellCheckResultDTO> = 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<WordSuggestion> = 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<WordSuggestion> = 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
Expand Down Expand Up @@ -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<SpellCheckResultDTO> {
// override fun onResponse(call: Call<SpellCheckResultDTO>, response: Response<SpellCheckResultDTO>) {
// if (response.isSuccessful) {
// val post = response.body()
// // Handle the retrieved post data
// } else {
// // Handle error
// }
// }
//
// override fun onFailure(call: Call<SpellCheckResultDTO>, t: Throwable) {
// // Handle failure
// }
// })
private fun spellChecking(searchText: String) {
val requestBody = SpellCheckRequestDTO("អត្ថបទសម្រាប់ការពិនិត្យអក្ខរាវិរុទ្")
val call = ApiClient.apiService.spellCheckIng(requestBody)
call.enqueue(object : Callback<SpellCheckRespondDTO> {
override fun onResponse(call: Call<SpellCheckRespondDTO>, response: Response<SpellCheckRespondDTO>) {
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<SpellCheckRespondDTO>, t: Throwable) {
// Handle failure
// TODO: update icon status
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SpellCheckResultDTO>
@POST("/v1/spelling-check")
fun spellCheckIng(@Body requestBody: SpellCheckRequestDTO): Call<SpellCheckRespondDTO>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
package com.rathanak.khmerroman.request

class SpellCheckResultDTO {
}
data class SpellCheckResultDTO(
val startIndex: Int,
val endIndex: Int,
val word: String,
val suggestions: Array<String>,
val scores: Array<Float>
) {
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<SpellCheckResultDTO>)

data class SpellCheckRequestDTO (val text: String)

0 comments on commit 245f6a5

Please sign in to comment.