Skip to content

Commit

Permalink
Progress report : update some layout
Browse files Browse the repository at this point in the history
  • Loading branch information
RathanakSreang committed Dec 26, 2023
1 parent 791e2ec commit 8157140
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {
var bannerTargetUrl = "http://khmerlang.com/"
var lastFetchBannerAt: Date? = null

private val spellSuggestionManager: SpellSuggestionManager = SpellSuggestionManager(this)
private val smartbarManager: SmartbarManager = SmartbarManager(this, spellSuggestionManager)
private val smartbarManager: SmartbarManager = SmartbarManager(this)
var rootView: LinearLayout? = null
val context: Context
get() = rootView?.context ?: this
Expand All @@ -109,7 +108,6 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {
super.onDestroy()
wordTokenize.destroy()
smartbarManager.destroy()
spellSuggestionManager.destroy()
}

override fun onWindowShown() {
Expand Down Expand Up @@ -226,7 +224,6 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {
Styles.keyStyle.labelPaint.textSize = resources.getDimension(R.dimen.default_key_text_size)
customInputMethodView?.setBackgroundColor(Styles.keyboardStyle.keyboardBackground)
smartbarManager.setDarkMood(isDarkMood)
spellSuggestionManager.setDarkMood(isDarkMood)
}

@ColorInt
Expand All @@ -250,7 +247,7 @@ class R2KhmerService : InputMethodService(), KeyboardActionListener {
}
rootView!!.addView(customInputMethodView)

rootView!!.addView(spellSuggestionManager.createSpellSuggestionView())
rootView!!.addView(smartbarManager.createSpellSuggestionView())

return rootView
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import io.realm.Realm
import kotlinx.android.synthetic.main.smartbar.view.*
import kotlinx.coroutines.*

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

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

fun createSmartbarView(): LinearLayout {
val smartbarView = View.inflate(r_2_khmer.context, R.layout.smartbar, null) as LinearLayout
this.smartbarView = smartbarView
this.smartbarView!!.btnOpenApp.setOnClickListener {
launchApp()
}
this.smartbarView!!.btnAppLogo.setOnClickListener {
isAppLogoOn = !isAppLogoOn
handleBtnAppLogoClick()

this.smartbarView!!.btnAppLogo.setOnCheckedChangeListener { _, isChecked ->
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()
}
this.smartbarView!!.btnDownloadData.setOnClickListener {
it.visibility = View.GONE
Expand Down Expand Up @@ -72,75 +91,18 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService, private val spellSu
return smartbarView
}

fun createSpellSuggestionView(): LinearLayout {
return spellSuggestionManager.createSpellSuggestionView()
}

fun setDarkMood(darkMood: Boolean) {
isDarkMood = darkMood
spellSuggestionManager.setDarkMood(isDarkMood)
if (this.smartbarView != null) {
updateByMood()
}
}

private fun updateByMood() {
this.smartbarView!!.smartbar.setBackgroundColor(Styles.keyboardStyle.keyboardBackground)
DrawableCompat.setTint(this.smartbarView!!.smartbar.btnOpenApp.background, Styles.keyStyle.labelColor)
for (numberButton in this.smartbarView!!.numbersList.children) {
if (numberButton is Button) {
DrawableCompat.setTint(numberButton.background, Styles.keyStyle.normalBackgroundColor)
numberButton.setTextColor(Styles.keyStyle.labelColor)
}
}
}

private fun checkButtonOptionsVisibility() {
val selectedLangIdx = KhmerLangApp.preferences?.getInt(KeyboardPreferences.KEY_CURRENT_LANGUAGE_IDX, 0)
if(selectedLangIdx == 1) {
this.smartbarView!!.btnToggleRMCorrection.visibility = View.GONE//View.INVISIBLE//View.GONE
this.smartbarView!!.btnToggleENCorrection.visibility = View.GONE//View.INVISIBLE//View.GONE
} else {
this.smartbarView!!.btnToggleRMCorrection.visibility = View.VISIBLE
this.smartbarView!!.btnToggleENCorrection.visibility = View.VISIBLE
}

if (r_2_khmer.currentInputPassword) {
this.smartbarView!!.btnOpenApp.visibility = View.GONE
} else {
this.smartbarView!!.btnOpenApp.visibility = View.VISIBLE
}
}
private fun initToggleButton() {
this.smartbarView!!.btnToggleRMCorrection.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
buttonView.setBackgroundResource(R.drawable.ic_btn_roman)
} else {
buttonView.setBackgroundResource(R.drawable.ic_btn_roman_off)
}
KhmerLangApp.preferences?.putBoolean(KeyboardPreferences.KEY_RM_CORRECTION_MODE, isChecked)
}
val isRMChecked = KhmerLangApp.preferences?.getBoolean(KeyboardPreferences.KEY_RM_CORRECTION_MODE, true)
this.smartbarView!!.btnToggleRMCorrection.isChecked = isRMChecked!!

this.smartbarView!!.btnToggleENCorrection.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
buttonView.setBackgroundResource(R.drawable.ic_btn_english)
} else {
buttonView.setBackgroundResource(R.drawable.ic_btn_english_off)
}
KhmerLangApp.preferences?.putBoolean(KeyboardPreferences.KEY_EN_CORRECTION_MODE, isChecked)
}
val isENChecked = KhmerLangApp.preferences?.getBoolean(KeyboardPreferences.KEY_EN_CORRECTION_MODE, false)
this.smartbarView!!.btnToggleENCorrection.isChecked = isENChecked!!

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

fun toggleBarLayOut(show: Boolean) {
if (this.smartbarView == null) {
return
Expand Down Expand Up @@ -180,7 +142,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService, private val spellSu
this.smartbarView!!.hasDataContainer!!.visibility = View.VISIBLE
}

isAppLogoOn = show
this.smartbarView!!.btnAppLogo.isChecked = show
if(isTyping) {
return
}
Expand Down Expand Up @@ -267,8 +229,8 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService, private val spellSu
}
suggestionJob!!.invokeOnCompletion {
this.smartbarView!!.candidatesList.removeAllViews()
var layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
if (!result.isNullOrEmpty()) for(word in result) {
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)
btnSuggestion.layoutParams =layoutParams
btnSuggestion.text = word.toString()
Expand All @@ -295,35 +257,87 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService, private val spellSu
}

fun destroy() {
spellSuggestionManager.destroy()
}

private fun handleBtnAppLogoClick() {
// display gif image, typing, request_checking, error_found, no_error
if (isAppLogoOn) {
toggleBarLayOut(true)
this.smartbarView!!.settingsList.visibility = View.GONE
if (isComposingEnabled) {
r_2_khmer.customInputMethodView?.visibility = View.VISIBLE;
spellSuggestionManager.spellSuggestionView?.visibility = View.GONE
fun performSpellChecking() {
spellSuggestionManager.performSpellChecking(r_2_khmer.getCurrentText())
}

fun setCurrentViewState(state: SPELLCHECKER) {
viewState = state
updateLogoBtnImage()
}


private fun updateByMood() {
this.smartbarView!!.smartbar.setBackgroundColor(Styles.keyboardStyle.keyboardBackground)
DrawableCompat.setTint(this.smartbarView!!.smartbar.btnOpenApp.background, Styles.keyStyle.labelColor)
for (numberButton in this.smartbarView!!.numbersList.children) {
if (numberButton is Button) {
DrawableCompat.setTint(numberButton.background, Styles.keyStyle.normalBackgroundColor)
numberButton.setTextColor(Styles.keyStyle.labelColor)
}
}
}

viewState = SPELLCHECKER.NORMAL
private fun checkButtonOptionsVisibility() {
val selectedLangIdx = KhmerLangApp.preferences?.getInt(KeyboardPreferences.KEY_CURRENT_LANGUAGE_IDX, 0)
if(selectedLangIdx == 1) {
this.smartbarView!!.btnToggleRMCorrection.visibility = View.GONE//View.INVISIBLE//View.GONE
this.smartbarView!!.btnToggleENCorrection.visibility = View.GONE//View.INVISIBLE//View.GONE
} else {
checkButtonOptionsVisibility()
toggleBarLayOut(false)
this.smartbarView!!.settingsList.visibility = View.VISIBLE
this.smartbarView!!.btnToggleRMCorrection.visibility = View.VISIBLE
this.smartbarView!!.btnToggleENCorrection.visibility = View.VISIBLE
}

if (isComposingEnabled) {
r_2_khmer.customInputMethodView?.visibility = View.GONE;
spellSuggestionManager.spellSuggestionView?.visibility = View.VISIBLE
if (r_2_khmer.currentInputPassword) {
this.smartbarView!!.btnOpenApp.visibility = View.GONE
} else {
this.smartbarView!!.btnOpenApp.visibility = View.VISIBLE
}
}
private fun initToggleButton() {
this.smartbarView!!.btnToggleRMCorrection.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
buttonView.setBackgroundResource(R.drawable.ic_btn_roman)
} else {
buttonView.setBackgroundResource(R.drawable.ic_btn_roman_off)
}
KhmerLangApp.preferences?.putBoolean(KeyboardPreferences.KEY_RM_CORRECTION_MODE, isChecked)
}
val isRMChecked = KhmerLangApp.preferences?.getBoolean(KeyboardPreferences.KEY_RM_CORRECTION_MODE, true)
this.smartbarView!!.btnToggleRMCorrection.isChecked = isRMChecked!!

viewState = SPELLCHECKER.OPEN_TOGGLE
this.smartbarView!!.btnToggleENCorrection.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
buttonView.setBackgroundResource(R.drawable.ic_btn_english)
} else {
buttonView.setBackgroundResource(R.drawable.ic_btn_english_off)
}
KhmerLangApp.preferences?.putBoolean(KeyboardPreferences.KEY_EN_CORRECTION_MODE, isChecked)
}
val isENChecked = KhmerLangApp.preferences?.getBoolean(KeyboardPreferences.KEY_EN_CORRECTION_MODE, false)
this.smartbarView!!.btnToggleENCorrection.isChecked = isENChecked!!

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

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

var currentIcon = R.drawable.ic_btn_khmerlang
when(viewState) {
SPELLCHECKER.TYPING -> {
Expand All @@ -341,6 +355,9 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService, private val spellSu
SPELLCHECKER.INVALID_ERROR -> {
currentIcon = 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
}
Expand All @@ -349,14 +366,7 @@ class SmartbarManager(private val r_2_khmer: R2KhmerService, private val spellSu
}
}

Glide.with(r_2_khmer.context)
.load(currentIcon)
.error(R.drawable.ic_btn_khmerlang)
.into(this.smartbarView!!.btnAppLogo)
}

fun performSpellChecking() {
spellSuggestionManager.performSpellChecking(r_2_khmer.getCurrentText())
this.smartbarView!!.btnAppLogo.setBackgroundResource(currentIcon)
}

// load spell suggestion data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SpellSuggestionManager(private val r_2_khmer: R2KhmerService) {
class SpellSuggestionManager(private val smartBar: SmartbarManager, private val r_2_khmer: R2KhmerService) {
private var spellCheckJob: Job? = null
var currentSentence: String = ""
var spellSuggestionView: LinearLayout? = null
Expand All @@ -35,24 +35,7 @@ class SpellSuggestionManager(private val r_2_khmer: R2KhmerService) {
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))

spellSuggestionAdapter = SpellSuggestionAdapter(r_2_khmer, r_2_khmer.context, spellSuggestionItems)
listView.adapter = spellSuggestionAdapter
val emptyView = spellSuggestionView.noDataText
Expand Down Expand Up @@ -91,6 +74,7 @@ class SpellSuggestionManager(private val r_2_khmer: R2KhmerService) {
return
}

smartBar.setCurrentViewState(SPELLCHECKER.VALIDATION)
currentSentence = sentence
spellCheckJob?.cancel()
spellCheckJob = GlobalScope.launch(Dispatchers.Main) {
Expand All @@ -108,19 +92,25 @@ class SpellSuggestionManager(private val r_2_khmer: R2KhmerService) {
// Handle the retrieved spell check data
val responseBody = response.body()
if (responseBody != null) {
// TODO: update icon status
spellSuggestionAdapter?.suggestionsList = responseBody.results
spellSuggestionAdapter?.notifyDataSetChanged()
if (responseBody.results.size > 0) {
smartBar.setCurrentViewState(SPELLCHECKER.SPELLING_ERROR)
} else {
smartBar.setCurrentViewState(SPELLCHECKER.NORMAL)
}
}
} else {
// Handle error
// TODO: update icon status
smartBar.setCurrentViewState(SPELLCHECKER.REACH_LIMIT_ERROR)
}
}

override fun onFailure(call: Call<SpellCheckRespondDTO>, t: Throwable) {
// Handle failure
// TODO: update icon status
smartBar.setCurrentViewState(SPELLCHECKER.NETWORK_ERROR)
// smartBar.setCurrentViewState(SPELLCHECKER.INVALID_ERROR)
// smartBar.setCurrentViewState(SPELLCHECKER.REACH_LIMIT_ERROR)
}
})
}
Expand Down
Loading

0 comments on commit 8157140

Please sign in to comment.