Skip to content

Commit

Permalink
Implement button to enable keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Aug 29, 2024
1 parent 067ceaf commit 292965c
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,11 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
val userLexiconMark = userLexiconSuggestions.firstOrNull()?.mark
if (userLexiconMark != null) {
userLexiconMark
} else if (processingText.any { it.isSeparatorOrTone() }){
processingText.formattedForMark()
} else {
val separatorOrTone = processingText.firstOrNull { it.isSeparatorOrTone() }
if (separatorOrTone != null) {
processingText.formattedForMark()
} else {
val firstCandidate = suggestions.firstOrNull()
if (firstCandidate != null && firstCandidate.input.length == processingText.length) firstCandidate.mark else processingText
}
val firstCandidate = suggestions.firstOrNull()
if (firstCandidate != null && firstCandidate.input.length == processingText.length) firstCandidate.mark else processingText
}
}
currentInputConnection.setComposingText(mark, mark.length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package org.jyutping.jyutping.presets

object PresetConstant {
const val ToolBarHeight: Float = 60f
const val KEYBOARD_ID: String = "org.jyutping.jyutping"
}
35 changes: 34 additions & 1 deletion app/src/main/java/org/jyutping/jyutping/ui/home/HomeScreen.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package org.jyutping.jyutping.ui.home

import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.view.inputmethod.InputMethodManager
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import org.jyutping.jyutping.R
import org.jyutping.jyutping.extensions.convertedS2T
import org.jyutping.jyutping.presets.PresetConstant
import org.jyutping.jyutping.search.CantoneseLexicon
import org.jyutping.jyutping.search.CantoneseLexiconView
import org.jyutping.jyutping.search.ChoHokView
Expand All @@ -30,7 +42,7 @@ import org.jyutping.jyutping.utilities.DatabasePreparer

@Composable
fun HomeScreen(navController: NavHostController) {
val textState = remember { mutableStateOf<String>("") }
val textState: MutableState<String> = remember { mutableStateOf("") }
val lexiconState = remember { mutableStateOf<CantoneseLexicon?>(null) }
val unihanDefinition = remember { mutableStateOf<UnihanDefinition?>(null) }
val yingWaaEntries = remember { mutableStateOf<List<YingWaaFanWan>>(listOf()) }
Expand Down Expand Up @@ -67,6 +79,11 @@ fun HomeScreen(navController: NavHostController) {
val traditionalChar = text.convertedS2T().firstOrNull() ?: char
return helper.gwongWanMatch(traditionalChar)
}
val isKeyboardEnabled: MutableState<Boolean> = remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
val manager = navController.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
isKeyboardEnabled.value = manager.enabledInputMethodList.any { it.packageName == PresetConstant.KEYBOARD_ID }
}
SelectionContainer {
LazyColumn(
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
Expand Down Expand Up @@ -109,6 +126,22 @@ fun HomeScreen(navController: NavHostController) {
GwongWanView(gwongWanEntries.value)
}
}
if (isKeyboardEnabled.value.not()) {
item {
Button(
onClick = {
val intent = Intent(Settings.ACTION_INPUT_METHOD_SETTINGS).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }
navController.context.startActivity(intent)
},
shape = RoundedCornerShape(8.dp),
contentPadding = PaddingValues(horizontal = 4.dp)
) {
Spacer(modifier = Modifier.weight(1f))
Text(text = stringResource(id = R.string.home_button_enable_keyboard))
Spacer(modifier = Modifier.weight(1f))
}
}
}
item {
TextCard(
heading = stringResource(id = R.string.home_heading_tone_input),
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+yue+Hant+CN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+yue+Hant+HK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+yue+Hant+MO/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+yue+Hant/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+yue/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hans+CN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hans/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hant+CN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hant+HK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hant+MO/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hant+TW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh+Hant/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-b+zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">粵語</string>
<string name="screen_title_about">關於</string>
<string name="search_field_placeholder">查字音</string>
<string name="home_button_enable_keyboard">啓用鍵盤</string>
<string name="home_heading_tone_input">聲調輸入</string>
<string name="home_content_tones_input">v = 1 陰平, vv = 4 陽平\nx = 2 陰上, xx = 5 陽上\nq = 3 陰去, qq = 6 陽去</string>
<string name="home_subcontent_tones_input_examples">例: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="screen_title_cantonese">Cantonese</string>
<string name="screen_title_about">About</string>
<string name="search_field_placeholder">Search Pronunciation</string>
<string name="home_button_enable_keyboard">Enable this keyboard</string>
<string name="home_heading_tone_input">Tone Input</string>
<string name="home_content_tones_input">v = 1, High Level (陰平)\nx = 2, Medium Rising (陰上)\nq = 3, Medium Level (陰去)\nvv = 4, Low Falling (陽平)\nxx = 5, Low Rising (陽上)\nqq = 6, Low Level (陽去)</string>
<string name="home_subcontent_tones_input_examples">Examples: sinq = sin3(線), sinqq = sin6(羨)</string>
Expand Down

0 comments on commit 292965c

Please sign in to comment.