Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Reduce Return Statements Across Multiple Functions #191

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f5da3d1
Update detekt.yml remove return count
KesharwaniArpita Oct 13, 2024
708eb08
Update Activity.kt to remove return statement >2
KesharwaniArpita Oct 13, 2024
dfa59e1
Update CommonsContext.kt to resolve the return statement lint error
KesharwaniArpita Oct 13, 2024
c81646c
Update ContextStorage.kt
KesharwaniArpita Oct 13, 2024
99934c2
Update String.kt
KesharwaniArpita Oct 13, 2024
724c2e6
Update LanguageSettingsFragment.kt
KesharwaniArpita Oct 13, 2024
fa4ab51
Update AlphanumericComparator.kt
KesharwaniArpita Oct 13, 2024
f9a912c
Update ExternalStorageProviderHack.kt
KesharwaniArpita Oct 13, 2024
058f414
Update SimpleKeyboardIME.kt
KesharwaniArpita Oct 13, 2024
ebe4395
Update MyKeyboardView.kt
KesharwaniArpita Oct 13, 2024
e05376d
Update BaseSimpleActivity.kt
KesharwaniArpita Oct 13, 2024
5b18e59
Merge branch 'main' into AK-return-statement-lint-error
KesharwaniArpita Oct 14, 2024
eb81b04
Merge branch 'scribe-org:main' into AK-return-statement-lint-error
KesharwaniArpita Oct 15, 2024
ec4d7d4
Update BaseSimpleActivity.kt
KesharwaniArpita Oct 15, 2024
b38fc71
Update ContextStorage.kt
KesharwaniArpita Oct 15, 2024
b74957c
Update Activity.kt
KesharwaniArpita Oct 15, 2024
1673d15
Delete app/src/main/java/be/scri/activities directory
KesharwaniArpita Oct 19, 2024
f097cd0
Create MainActivity.kt
KesharwaniArpita Oct 19, 2024
8765001
Delete app/src/main/java/be/scri/extensions/Activity.kt
KesharwaniArpita Oct 19, 2024
2902980
Delete app/src/main/java/be/scri/extensions directory
KesharwaniArpita Oct 19, 2024
de5d41f
Create CommonsContext.kt
KesharwaniArpita Oct 19, 2024
f642bc2
Create Context.kt
KesharwaniArpita Oct 19, 2024
6562407
Create ContextStyling.kt
KesharwaniArpita Oct 19, 2024
b6b22c5
Create Drawable.kt
KesharwaniArpita Oct 19, 2024
0dd7d59
Create Int.kt
KesharwaniArpita Oct 19, 2024
6ae90c8
Create View.kt
KesharwaniArpita Oct 19, 2024
6a0c879
Delete app/src/main/java/be/scri/helpers/ExternalStorageProviderHack.kt
KesharwaniArpita Oct 19, 2024
469c4ef
Merge branch 'scribe-org:main' into AK-return-statement-lint-error
KesharwaniArpita Oct 19, 2024
ec10daa
Update AlphanumericComparator.kt
KesharwaniArpita Oct 19, 2024
16be181
Merge branch 'scribe-org:main' into AK-return-statement-lint-error
KesharwaniArpita Oct 19, 2024
e0d368a
Update LanguageSettingsFragment.kt
KesharwaniArpita Oct 19, 2024
3bd1bb1
Update AlphanumericComparator.kt Modified Compare function to reduce …
KesharwaniArpita Oct 19, 2024
e18a51b
Update SimpleKeyboardIME.kt fun hastextbeforecursor
KesharwaniArpita Oct 19, 2024
59011cc
Update MyKeyboardView.kt
KesharwaniArpita Oct 19, 2024
c9b24c0
Merge branch 'main' into AK-return-statement-lint-error
KesharwaniArpita Oct 20, 2024
7c27fbd
Updating the detekt.yml as the current code doesn't have the return s…
KesharwaniArpita Oct 20, 2024
be4f564
Merge branch 'main' into AK-return-statement-lint-error
andrewtavis Oct 20, 2024
25eb8c1
Run ktlint formatting
andrewtavis Oct 20, 2024
b642a7f
Remove unneeded added spacing
andrewtavis Oct 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions app/src/main/java/be/scri/fragments/LanguageSettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,16 @@ class LanguageSettingsFragment : Fragment() {
}

fun getLanguageStringFromi18n(language: String): Int {
when (language) {
"German" -> return R.string.app__global_german
"French" -> return R.string.app__global_french
"Spanish" -> return R.string.app__global_spanish
"Italian" -> return R.string.app__global_italian
"Russian" -> return R.string.app__global_russian
"Portuguese" -> return R.string.app__global_portuguese
"Swedish" -> return R.string.app__global_swedish
else -> return R.string.app__global_english
}
val languageMap =
mapOf(
"German" to R.string.app__global_german,
"French" to R.string.app__global_french,
"Spanish" to R.string.app__global_spanish,
"Italian" to R.string.app__global_italian,
"Russian" to R.string.app__global_russian,
"Portuguese" to R.string.app__global_portuguese,
"Swedish" to R.string.app__global_swedish,
)
return languageMap[language] ?: R.string.app__global_english
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/be/scri/helpers/AlphanumericComparator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ class AlphanumericComparator {
if (result == 0) {
for (i in 0 until thisChunkLength) {
result = thisChunk[i] - thatChunk[i]
if (result != 0) {
return result
}
if (result != 0) break
}
}
} else {
Expand Down
9 changes: 2 additions & 7 deletions app/src/main/java/be/scri/services/SimpleKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,8 @@ abstract class SimpleKeyboardIME :

override fun hasTextBeforeCursor(): Boolean {
val inputConnection = currentInputConnection ?: return false
val textBeforeCursor = inputConnection.getTextBeforeCursor(Int.MAX_VALUE, 0)
if (textBeforeCursor.isNullOrBlank()) {
return false
}
val trimmedText = textBeforeCursor.trim()
val lastChar = trimmedText.lastOrNull()
return lastChar != '.'
val textBeforeCursor = inputConnection.getTextBeforeCursor(Int.MAX_VALUE, 0).orEmpty().trim()
Copy link
Member

@angrezichatterbox angrezichatterbox Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using isEmpty(), I recommend performing a simple null check in the return statement. This adjustment should help avoid any errors.

Additionally, I suggest running the application in Android Studio or following the steps outlined in the new PR template, specifically ./gradlew lintKotlin detekt test. This could help catch potential issues early on. If you are facing any difficulties in running the application do tell. Happy to help :)

Thank you!

return textBeforeCursor.isNotEmpty() && textBeforeCursor.lastOrNull() != '.'
}

override fun commitPeriodAfterSpace() {
Expand Down
119 changes: 51 additions & 68 deletions app/src/main/java/be/scri/views/MyKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -967,29 +967,18 @@ class MyKeyboardView
)
}

private fun openPopupIfRequired(me: MotionEvent): Boolean {
// Check if we have a popup layout specified first.
if (mPopupLayout == 0) {
return false
}

if (mCurrentKey < 0 || mCurrentKey >= mKeys.size) {
return false
}

val popupKey = mKeys[mCurrentKey]
val result = onLongPress(popupKey, me)
if (result) {
mAbortKey = true
showPreview(NOT_A_KEY)
}
private fun openPopupIfRequired(me: MotionEvent): Boolean {
if (mPopupLayout == 0 || mCurrentKey !in mKeys.indices) {
return false
}

return result
val popupKey = mKeys[mCurrentKey]
val result = onLongPress(popupKey, me)
if (result) {
mAbortKey = true
showPreview(NOT_A_KEY)
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the comment be opened and closed properly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this and the above and we'll see what the state of it all is :)

* Called when a key is long pressed.
* By default this will open any popup keyboard associated with this key through the attributes
* popupLayout and popupCharacters.
* @param popupKey the key that was long pressed
* @return true if the long press is handled, false otherwise.
Expand Down Expand Up @@ -1197,36 +1186,37 @@ class MyKeyboardView
return onModifiedTouchEvent(me)
}

private fun onModifiedTouchEvent(me: MotionEvent): Boolean {
var touchX = me.x.toInt()
var touchY = me.y.toInt()
if (touchY >= -mVerticalCorrection) {
touchY += mVerticalCorrection
}
@Suppress("ReturnCount")
private fun onModifiedTouchEvent(me: MotionEvent): Boolean {
var touchX = me.x.toInt()
var touchY = me.y.toInt()
if (touchY >= -mVerticalCorrection) {
touchY += mVerticalCorrection
}

val action = me.actionMasked
val eventTime = me.eventTime
val keyIndex = getPressedKeyIndex(touchX, touchY)
val action = me.actionMasked
val eventTime = me.eventTime
val keyIndex = getPressedKeyIndex(touchX, touchY)

// Ignore all motion events until a DOWN.
if (mAbortKey && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
return true
}
var handled = false

// Needs to be called after the gesture detector gets a turn, as it may have displayed the mini keyboard
if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) {
return true
}
// Ignore all motion events until a DOWN.
if (mAbortKey && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
handled = true
}

// Handle mini keyboard case
if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) {
handled = true
}

if (!handled) {
when (action) {
MotionEvent.ACTION_POINTER_DOWN -> {
// if the user presses a key while still holding down the previous,
// type in both chars and ignore the later gestures
// can happen at fast typing, easier to reproduce by increasing LONGPRESS_TIMEOUT
ignoreTouches = true
mHandler!!.removeMessages(MSG_LONGPRESS)
dismissPopupKeyboard()
detectAndSendKey(keyIndex, me.x.toInt(), me.y.toInt(), eventTime)
detectAndSendKey(keyIndex, touchX, touchY, eventTime)

val newPointerX = me.getX(1).toInt()
val newPointerY = me.getY(1).toInt()
Expand All @@ -1235,13 +1225,11 @@ class MyKeyboardView
detectAndSendKey(secondKeyIndex, newPointerX, newPointerY, eventTime)

val secondKeyCode = mKeys.getOrNull(secondKeyIndex)?.code
if (secondKeyCode != null) {
mOnKeyboardActionListener!!.onPress(secondKeyCode)
}
secondKeyCode?.let { mOnKeyboardActionListener!!.onPress(it) }

showPreview(NOT_A_KEY)
invalidateKey(mCurrentKey)
return true
handled = true
}
MotionEvent.ACTION_DOWN -> {
mAbortKey = false
Expand All @@ -1251,39 +1239,30 @@ class MyKeyboardView
mCurrentKeyTime = 0
mLastKey = NOT_A_KEY
mCurrentKey = keyIndex
mDownTime = me.eventTime
mLastMoveTime = mDownTime

val onPressKey =
if (keyIndex != NOT_A_KEY) {
mKeys[keyIndex].code
} else {
0
}
mDownTime = eventTime
mLastMoveTime = eventTime

val onPressKey = if (keyIndex != NOT_A_KEY) mKeys[keyIndex].code else 0
mOnKeyboardActionListener!!.onPress(onPressKey)

var wasHandled = false
if (mCurrentKey >= 0 && mKeys[mCurrentKey].repeatable) {
mRepeatKeyIndex = mCurrentKey

val msg = mHandler!!.obtainMessage(MSG_REPEAT)
mHandler!!.sendMessageDelayed(msg, REPEAT_START_DELAY.toLong())
// if the user long presses Space, move the cursor after swipine left/right

if (mKeys[mCurrentKey].code == KEYCODE_SPACE) {
mLastSpaceMoveX = -1
} else {
repeatKey(true)
}

// Delivering the key could have caused an abort
if (mAbortKey) {
mRepeatKeyIndex = NOT_A_KEY
wasHandled = true
handled = true
}
}

if (!wasHandled && mCurrentKey != NOT_A_KEY) {
if (!handled && mCurrentKey != NOT_A_KEY) {
val msg = mHandler!!.obtainMessage(MSG_LONGPRESS, me)
mHandler!!.sendMessageDelayed(msg, LONGPRESS_TIMEOUT.toLong())
}
Expand Down Expand Up @@ -1331,9 +1310,7 @@ class MyKeyboardView
mLastSpaceMoveX = mLastX
}
} else if (!continueLongPress) {
// Cancel old longpress
mHandler!!.removeMessages(MSG_LONGPRESS)
// Start new longpress if key has changed
if (keyIndex != NOT_A_KEY) {
val msg = mHandler!!.obtainMessage(MSG_LONGPRESS, me)
mHandler!!.sendMessageDelayed(msg, LONGPRESS_TIMEOUT.toLong())
Expand Down Expand Up @@ -1362,9 +1339,10 @@ class MyKeyboardView
touchX = mLastCodeX
touchY = mLastCodeY
}

showPreview(NOT_A_KEY)
Arrays.fill(mKeyIndices, NOT_A_KEY)
// If we're not on a repeating key (which sends on a DOWN event)

if (mRepeatKeyIndex == NOT_A_KEY && !mMiniKeyboardOnScreen && !mAbortKey) {
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime)
}
Expand All @@ -1381,6 +1359,7 @@ class MyKeyboardView
}
lastSpaceBarTapTime = currentTime
}

invalidateKey(keyIndex)
mRepeatKeyIndex = NOT_A_KEY
mOnKeyboardActionListener!!.onActionUp()
Expand All @@ -1396,13 +1375,17 @@ class MyKeyboardView
invalidateKey(mCurrentKey)
}
}

mLastX = touchX
mLastY = touchY
return true
}

private fun repeatKey(initialCall: Boolean): Boolean {
mLastX = touchX
mLastY = touchY

return handled || true
}



private fun repeatKey(initialCall: Boolean): Boolean {
val key = mKeys[mRepeatKeyIndex]
if (!initialCall && key.code == KEYCODE_SPACE) {
if (!mIsLongPressingSpace) {
Expand Down
2 changes: 1 addition & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ style:
WildcardImport:
active: false
ReturnCount:
active: false
active: true
UnusedPrivateProperty:
active: true
UnusedParameter:
Expand Down
Loading