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 27, 2023
1 parent 8157140 commit b921660
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.text.InputType
import android.util.Log
import android.util.SparseArray
import android.view.KeyEvent
import android.view.View
Expand Down Expand Up @@ -75,6 +76,7 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {
private var previousTwo = "<s>"
private var isStartSen = true
private var isKeyDown: Boolean = false
private var isSpellCheckSelected: Boolean = false
var currentInputPassword: Boolean = false
var bannerIdsData: MutableList<String> = mutableListOf()
var currentBannerIndex = 0
Expand Down Expand Up @@ -497,20 +499,10 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {

fun setCurrentText(typoWord: String, selectText: String, startPos: Int, endPos: Int) {
//TODO: send to typoWord -> selectText
mockInput()
isSpellCheckSelected = true
currentInputConnection.beginBatchEdit()
currentInputConnection.setComposingRegion(startPos, endPos);
currentInputConnection.setComposingText(selectText, selectText.length)
currentInputConnection.finishComposingText()
currentInputConnection.endBatchEdit()
}

fun mockInput () {
// val newText = "អត្ថបទសម្រាប់កាពិនិត្យអក្ខរាវិរុទ្ធ"
val newText = "អត្ថបទ\nសម្រាប់កាពិនិត្យអក្ខរាវិរុទ្ធ"
currentInputConnection.beginBatchEdit()
currentInputConnection.setComposingRegion(0,getCurrentText().length);
currentInputConnection.commitText(newText, newText.length)
currentInputConnection.setComposingText(selectText, startPos)
currentInputConnection.finishComposingText()
currentInputConnection.endBatchEdit()
}
Expand Down Expand Up @@ -552,6 +544,11 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {
candidatesStart, candidatesEnd
)

if(isSpellCheckSelected) {
isSpellCheckSelected = false
return
}

smartbarManager.performSpellChecking()

if (candidateChoosed && firstCommitCandidate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.rathanak.khmerroman.keyboard.smartbar
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
Expand All @@ -23,9 +22,9 @@ import io.realm.Realm
import kotlinx.android.synthetic.main.smartbar.view.*
import kotlinx.coroutines.*

enum class SPELLCHECKER { NORMAL, TYPING, VALIDATION, SPELLING_ERROR, NETWORK_ERROR, REACH_LIMIT_ERROR, INVALID_ERROR, OPEN_TOGGLE }
enum class SPELLCHECKER { NORMAL, TYPING, VALIDATION, SPELLING_ERROR, NETWORK_ERROR, REACH_LIMIT_ERROR, TOKEN_INVALID_ERROR }

class SmartbarManager(private val r_2_khmer: R2KhmerService) {
class SmartbarManager(private val r2Khmer: R2KhmerService) {
private var smartbarView: LinearLayout? = null
private var isComposingEnabled: Boolean = false
private var isDarkMood: Boolean = false
Expand All @@ -35,10 +34,10 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
private var result: List<String> = emptyList()
private var viewState: SPELLCHECKER = SPELLCHECKER.NORMAL
private var isAppToggleChecked: Boolean = false
private val spellSuggestionManager: SpellSuggestionManager = SpellSuggestionManager(this, r_2_khmer)
private val spellSuggestionManager: SpellSuggestionManager = SpellSuggestionManager(this, r2Khmer)

fun createSmartbarView(): LinearLayout {
val smartbarView = View.inflate(r_2_khmer.context, R.layout.smartbar, null) as LinearLayout
val smartbarView = View.inflate(r2Khmer.context, R.layout.smartbar, null) as LinearLayout
this.smartbarView = smartbarView
this.smartbarView!!.btnOpenApp.setOnClickListener {
launchApp()
Expand All @@ -48,22 +47,14 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
if (isChecked) {
toggleBarLayOut(true)
this.smartbarView!!.settingsList.visibility = View.GONE
if (isComposingEnabled) {
r_2_khmer.customInputMethodView?.visibility = View.VISIBLE;
spellSuggestionManager.spellSuggestionView?.visibility = View.GONE
}
} else {
checkButtonOptionsVisibility()
toggleBarLayOut(false)
this.smartbarView!!.settingsList.visibility = View.VISIBLE

if (isComposingEnabled) {
r_2_khmer.customInputMethodView?.visibility = View.GONE;
spellSuggestionManager.spellSuggestionView?.visibility = View.VISIBLE
}
}
isAppToggleChecked = isChecked
updateLogoBtnImage()
updateSpellSuggestionView()
}
this.smartbarView!!.btnDownloadData.setOnClickListener {
it.visibility = View.GONE
Expand All @@ -76,10 +67,10 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
}

this.smartbarView!!.bannerImage.setOnClickListener {
if(r_2_khmer.bannerTargetUrl != "") {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(r_2_khmer.bannerTargetUrl))
if(r2Khmer.bannerTargetUrl != "") {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(r2Khmer.bannerTargetUrl))
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
r_2_khmer.context.startActivity(intent)
r2Khmer.context.startActivity(intent)
}
}

Expand Down Expand Up @@ -109,7 +100,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
}

if (R2KhmerService.downloadDataStatus == KeyboardPreferences.STATUS_NONE) {
Glide.with(r_2_khmer.context)
Glide.with(r2Khmer.context)
.load(R.drawable.banner_download_data)
.into(this.smartbarView!!.btnDownloadData)

Expand All @@ -120,7 +111,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {

return
} else if (R2KhmerService.downloadDataStatus == KeyboardPreferences.STATUS_DOWNLOAD_FAIL) {
Glide.with(r_2_khmer.context)
Glide.with(r2Khmer.context)
.load(R.drawable.banner_download_data_fail)
.into(this.smartbarView!!.btnDownloadData)

Expand Down Expand Up @@ -154,7 +145,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
this.smartbarView!!.candidatesContainer.visibility = View.GONE
this.smartbarView!!.numbersList!!.visibility = View.GONE
if (show) {
if (r_2_khmer.currentInputPassword) {
if (r2Khmer.currentInputPassword) {
this.smartbarView!!.numbersList!!.visibility = View.VISIBLE
} else if (suggestionCount > 0) {
this.smartbarView!!.candidatesContainer.visibility = View.VISIBLE
Expand All @@ -171,12 +162,12 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
}

if(bannerID != "") {
Glide.with(r_2_khmer.context)
Glide.with(r2Khmer.context)
.load(bannerID)
.error(R.drawable.banner_default_animate)
.into(this.smartbarView!!.bannerImage)
} else {
Glide.with(r_2_khmer.context)
Glide.with(r2Khmer.context)
.load(R.drawable.banner_default_animate)
.error(R.drawable.banner_default_animate)
.into(this.smartbarView!!.bannerImage)
Expand Down Expand Up @@ -231,7 +222,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
this.smartbarView!!.candidatesList.removeAllViews()
val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
if (result.isNotEmpty()) for(word in result) {
val btnSuggestion = Button(r_2_khmer.context)
val btnSuggestion = Button(r2Khmer.context)
btnSuggestion.layoutParams =layoutParams
btnSuggestion.text = word.toString()
btnSuggestion.setTextColor(Styles.keyStyle.labelColor)
Expand All @@ -248,20 +239,16 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {

fun setTyping(typing: Boolean) {
isTyping = typing
viewState = if(isTyping) {
SPELLCHECKER.TYPING
} else {
SPELLCHECKER.NORMAL
}
updateLogoBtnImage()
}

fun destroy() {
spellSuggestionManager.destroy()
}

fun performSpellChecking() {
spellSuggestionManager.performSpellChecking(r_2_khmer.getCurrentText())
if(isSpellSuggestionEnable()) {
spellSuggestionManager.performSpellChecking(r2Khmer.getCurrentText())
}
}

fun setCurrentViewState(state: SPELLCHECKER) {
Expand Down Expand Up @@ -291,7 +278,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
this.smartbarView!!.btnToggleENCorrection.visibility = View.VISIBLE
}

if (r_2_khmer.currentInputPassword) {
if (r2Khmer.currentInputPassword) {
this.smartbarView!!.btnOpenApp.visibility = View.GONE
} else {
this.smartbarView!!.btnOpenApp.visibility = View.VISIBLE
Expand Down Expand Up @@ -321,54 +308,82 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
this.smartbarView!!.btnToggleENCorrection.isChecked = isENChecked!!

this.smartbarView!!.btnToggleAutoCorrection.setOnCheckedChangeListener { buttonView, isChecked ->
KhmerLangApp.preferences?.putBoolean(KeyboardPreferences.KEY_AUTO_TYPING_CORRECTION_MODE, isChecked)
if (isChecked) {
buttonView.setBackgroundResource(R.drawable.btn_auto)
// TODO: checkInput sentence spell check
performSpellChecking()
} else {
buttonView.setBackgroundResource(R.drawable.btn_auto_off)
// TODO: change to keyboard layout
}
KhmerLangApp.preferences?.putBoolean(KeyboardPreferences.KEY_AUTO_TYPING_CORRECTION_MODE, isChecked)
updateSpellSuggestionView()
}
val autoTypeSpellCheck = KhmerLangApp.preferences?.getBoolean(KeyboardPreferences.KEY_AUTO_TYPING_CORRECTION_MODE, false)
this.smartbarView!!.btnToggleAutoCorrection.isChecked = autoTypeSpellCheck!!
}

private fun updateLogoBtnImage() {
if (isAppToggleChecked) {
if(this.smartbarView == null) {
return
}

if(this.smartbarView!!.btnAppLogo == null) {
return
}

if (!isAppToggleChecked) {
this.smartbarView!!.btnAppLogo.setBackgroundResource(R.drawable.ic_btn_khmerlang_off_v2)
return
}

var currentIcon = R.drawable.ic_btn_khmerlang
when(viewState) {
if (!isSpellSuggestionEnable()) {
this.smartbarView!!.btnAppLogo.setBackgroundResource(R.drawable.ic_btn_khmerlang)
return
}

var currentIcon = when(viewState) {
SPELLCHECKER.TYPING -> {
currentIcon = R.drawable.btn_base_typing
R.drawable.btn_base_validation
}
SPELLCHECKER.VALIDATION -> {
currentIcon = R.drawable.btn_base_validation
R.drawable.btn_base_validation
}
SPELLCHECKER.NETWORK_ERROR -> {
currentIcon = R.drawable.btn_base_network_error
R.drawable.btn_base_network_error
}
SPELLCHECKER.REACH_LIMIT_ERROR -> {
currentIcon = R.drawable.btn_base_react_limit
R.drawable.btn_base_react_limit
}
SPELLCHECKER.INVALID_ERROR -> {
currentIcon = R.drawable.btn_base_invalid_token
SPELLCHECKER.TOKEN_INVALID_ERROR -> {
R.drawable.btn_base_invalid_token
}
SPELLCHECKER.SPELLING_ERROR -> {
currentIcon = R.drawable.btn_khmerlang_mobile_danger
}
SPELLCHECKER.OPEN_TOGGLE -> {
currentIcon = R.drawable.ic_btn_khmerlang_off_v2
R.drawable.btn_khmerlang_mobile_danger
}
else -> {
currentIcon = R.drawable.ic_btn_khmerlang
R.drawable.ic_btn_khmerlang
}
}

this.smartbarView!!.btnAppLogo.setBackgroundResource(currentIcon)
}

private fun updateSpellSuggestionView() {
r2Khmer.customInputMethodView?.visibility = View.VISIBLE;
spellSuggestionManager.spellSuggestionView?.visibility = View.GONE

if (!isAppToggleChecked && isSpellSuggestionEnable()) {
if (isComposingEnabled) {
r2Khmer.customInputMethodView?.visibility = View.GONE;
spellSuggestionManager.spellSuggestionView?.visibility = View.VISIBLE
}
}
}

private fun isSpellSuggestionEnable(): Boolean {
val autoTypeSpellCheck = KhmerLangApp.preferences?.getBoolean(KeyboardPreferences.KEY_AUTO_TYPING_CORRECTION_MODE, false)
return autoTypeSpellCheck == true && isComposingEnabled
}

// load spell suggestion data
private suspend fun getSuggestion(prevOne: String, prevTwo: String, composingText: String, isStartSen: Boolean) {
coroutineScope {
Expand Down Expand Up @@ -419,16 +434,16 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
val text = view.text.toString()
if (text.isNotEmpty()) {
// r_2_khmer.commitCandidate(text + " ")
r_2_khmer.commitCandidate(text, isCorrection)
r2Khmer.commitCandidate(text, isCorrection)
}
}

private fun launchApp() {
val pm = r_2_khmer.context.packageManager
val intent:Intent? = pm.getLaunchIntentForPackage(r_2_khmer.context.packageName)
val pm = r2Khmer.context.packageManager
val intent:Intent? = pm.getLaunchIntentForPackage(r2Khmer.context.packageName)
intent?.addCategory(Intent.CATEGORY_LAUNCHER)
if(intent!=null){
r_2_khmer.context.startActivity(intent)
r2Khmer.context.startActivity(intent)
}
}

Expand All @@ -454,7 +469,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService) {
R.id.btnNum9 -> KeyData(57, "9")
else -> KeyData(0)
}
r_2_khmer.sendKeyPress(keyData)
r2Khmer.sendKeyPress(keyData)
}

private fun listenJobDone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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, var suggestionsList: java.util.ArrayList<SpellCheckResultDTO>) : BaseAdapter() {
class SpellSuggestionAdapter(private val smartSpell: SpellSuggestionManager, private val context: Context, var suggestionsList: java.util.ArrayList<SpellCheckResultDTO>) : BaseAdapter() {
override fun getCount(): Int {
return suggestionsList.size
}
Expand All @@ -42,6 +42,7 @@ class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val
val btnSpellItemClose = convertView.findViewById(R.id.btnSpellItemClose) as ImageButton
btnSpellItemClose.setOnClickListener {
suggestionsList.removeAt(position); // remove the item from the data list
handleAfterRemoveItem()
notifyDataSetChanged();
}

Expand All @@ -60,8 +61,13 @@ class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val
val word = it
btnWord.text = word
btnWord.setOnClickListener {
r_2_khmer.setCurrentText(typoWord, word, startIndex, endIndex + 1)
suggestionsList.removeAt(position);
smartSpell.setCurrentText(typoWord, word, startIndex, endIndex + 1)
suggestionsList.removeAt(position)
if(typoWord.length != word.length) {
updateNextListStartEndPos(position, word.length - typoWord.length)
}

handleAfterRemoveItem()
notifyDataSetChanged();
}

Expand All @@ -70,4 +76,17 @@ class SpellSuggestionAdapter(private val r_2_khmer: R2KhmerService, private val

return convertView
}

private fun handleAfterRemoveItem() {
if(suggestionsList.isNullOrEmpty()) {
smartSpell.onCallEmptyResult()
}
}

private fun updateNextListStartEndPos(position: Int, updateLength: Int) {
for (index in position until suggestionsList.size) {
suggestionsList[index].startIndex += updateLength
suggestionsList[index].endIndex += updateLength
}
}
}
Loading

0 comments on commit b921660

Please sign in to comment.