Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class AnnotatedVerseActivity<V : VerseAnnotation, VM : AnnotatedVerseVi
title = preview.title,
adapter = previewAdapter,
scrollToPosition = preview.currentPosition,
onDismiss = { viewModel.markPreviewAsClosed() }
onDismiss = viewModel::markPreviewAsClosed,
)
previewAdapter.submitList(preview.items)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ import androidx.annotation.IntDef
import kotlinx.coroutines.flow.Flow
import me.xizzhu.android.joshua.core.repository.SettingsRepository

data class Settings(val keepScreenOn: Boolean, @NightMode val nightMode: Int, val fontSizeScale: Float,
val simpleReadingModeOn: Boolean, val hideSearchButton: Boolean,
val consolidateVersesForSharing: Boolean,
@Highlight.Companion.AvailableColor val defaultHighlightColor: Int) {
data class Settings(
val keepScreenOn: Boolean,
@NightMode val nightMode: Int,
val fontSizeScale: Float,
val simpleReadingModeOn: Boolean,
val hideSearchButton: Boolean,
val consolidateVersesForSharing: Boolean,
@Highlight.Companion.AvailableColor val defaultHighlightColor: Int,
) {
companion object {
const val MIN_FONT_SIZE_SCALE = 0.5F
const val MAX_FONT_SIZE_SCALE = 3.0F

const val NIGHT_MODE_ON = 0
const val NIGHT_MODE_OFF = 1
const val NIGHT_MODE_FOLLOW_SYSTEM = 2
Expand All @@ -34,9 +42,13 @@ data class Settings(val keepScreenOn: Boolean, @NightMode val nightMode: Int, va
annotation class NightMode

val DEFAULT = Settings(
keepScreenOn = true, nightMode = NIGHT_MODE_OFF, fontSizeScale = 1.0F,
simpleReadingModeOn = false, hideSearchButton = false, consolidateVersesForSharing = false,
defaultHighlightColor = Highlight.COLOR_NONE
keepScreenOn = true,
nightMode = NIGHT_MODE_OFF,
fontSizeScale = 1.0F,
simpleReadingModeOn = false,
hideSearchButton = false,
consolidateVersesForSharing = false,
defaultHighlightColor = Highlight.COLOR_NONE,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,12 @@ class ReadingActivity : BaseActivity<ActivityReadingBinding, ReadingViewModel>()
lifecycleScope.launch {
val defaultHighlightColor = readingViewModel.settings().first().defaultHighlightColor
if (Highlight.COLOR_NONE == defaultHighlightColor) {
listDialog(R.string.text_pick_highlight_color,
resources.getStringArray(R.array.text_highlight_colors),
max(0, Highlight.AVAILABLE_COLORS.indexOf(currentHighlightColor))) { dialog, which ->
saveHighlight(verseIndex, Highlight.AVAILABLE_COLORS[which])

dialog.dismiss()
}
listDialog(
title = R.string.text_pick_highlight_color,
items = resources.getStringArray(R.array.text_highlight_colors),
selected = max(0, Highlight.AVAILABLE_COLORS.indexOf(currentHighlightColor)),
onSelected = { which -> saveHighlight(verseIndex, Highlight.AVAILABLE_COLORS[which]) }
)
} else {
saveHighlight(verseIndex, if (currentHighlightColor == Highlight.COLOR_NONE) defaultHighlightColor else Highlight.COLOR_NONE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class SearchActivity : BaseActivityV2<ActivitySearchBinding, SearchViewModel.Vie
title = preview.title,
adapter = previewAdapter,
scrollToPosition = preview.currentPosition,
onDismiss = { viewModel.markPreviewAsClosed() }
onDismiss = viewModel::markPreviewAsClosed,
)
previewAdapter.submitList(preview.items)
}
Expand Down
310 changes: 143 additions & 167 deletions app/src/main/kotlin/me/xizzhu/android/joshua/settings/SettingsActivity.kt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class StrongNumberActivity : BaseActivityV2<ActivityStrongNumberBinding, StrongN
title = preview.title,
adapter = previewAdapter,
scrollToPosition = preview.currentPosition,
onDismiss = { viewModel.markPreviewAsClosed() }
onDismiss = viewModel::markPreviewAsClosed,
)
previewAdapter.submitList(preview.items)
}
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/kotlin/me/xizzhu/android/joshua/ui/Dialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ fun Activity.seekBarDialog(
maxValue: Float,
onValueChanged: (Float) -> Unit,
onPositive: ((Float) -> Unit)? = null,
onNegative: (() -> Unit)? = null
onNegative: (() -> Unit)? = null,
onDismiss: (() -> Unit)? = null,
): AlertDialog? {
if (isDestroyed) return null

Expand All @@ -143,6 +144,7 @@ fun Activity.seekBarDialog(
.setView(viewBinding.root)
.setPositiveButton(android.R.string.ok, onPositive?.let { { _, _ -> it(viewBinding.seekBar.calculateValue(minValue, maxValue)) } })
.setNegativeButton(android.R.string.cancel, onNegative?.let { { _, _ -> it() } })
.setOnDismissListener { onDismiss?.invoke() }
.show()
}

Expand All @@ -157,7 +159,7 @@ fun Activity.listDialog(
title: CharSequence,
adapter: RecyclerView.Adapter<*>,
scrollToPosition: Int,
onDismiss: DialogInterface.OnDismissListener? = null
onDismiss: (() -> Unit)? = null,
): AlertDialog? {
if (isDestroyed) return null

Expand All @@ -167,12 +169,12 @@ fun Activity.listDialog(
scrollToPosition(scrollToPosition)
}
}
return MaterialAlertDialogBuilder(this)
val builder = MaterialAlertDialogBuilder(this)
.setCancelable(true)
.setTitle(title)
.setView(recyclerView)
.setOnDismissListener(onDismiss)
.show()
onDismiss?.let { builder.setOnDismissListener { onDismiss() } }
return builder.show()
}

fun Activity.listDialog(
Expand All @@ -196,12 +198,22 @@ fun Activity.listDialog(
.show()
}

fun Activity.listDialog(@StringRes title: Int, items: Array<String>, selected: Int, onClicked: DialogInterface.OnClickListener) {
fun Activity.listDialog(
@StringRes title: Int,
items: Array<String>,
selected: Int,
onSelected: (which: Int) -> Unit,
onDismiss: (() -> Unit)? = null,
) {
if (isDestroyed) return

MaterialAlertDialogBuilder(this)
val builder = MaterialAlertDialogBuilder(this)
.setCancelable(true)
.setSingleChoiceItems(items, selected, onClicked)
.setSingleChoiceItems(items, selected) { dialog, which ->
dialog.dismiss()
onSelected(which)
}
.setTitle(title)
.show()
onDismiss?.let { builder.setOnDismissListener { onDismiss() } }
builder.show()
}
41 changes: 32 additions & 9 deletions app/src/main/kotlin/me/xizzhu/android/joshua/ui/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.res.TypedArray
import android.os.SystemClock
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.CompoundButton
import android.widget.TextView
import androidx.annotation.StyleableRes
import androidx.viewpager2.widget.ViewPager2
Expand Down Expand Up @@ -62,12 +64,12 @@ fun View.fadeOut() {

alpha = 1.0F
animate().alpha(0.0F).setDuration(ANIMATION_DURATION)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
visibility = View.GONE
alpha = 1.0F
}
})
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
visibility = View.GONE
alpha = 1.0F
}
})
}

fun View.setBackground(resId: Int) {
Expand All @@ -79,7 +81,28 @@ fun View.setBackground(resId: Int) {
fun View.hideKeyboard() {
if (hasFocus()) {
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}

fun View.setOnSingleClickListener(listener: () -> Unit) {
setOnClickListener(object : View.OnClickListener {
private var lastClicked = 0L
override fun onClick(v: View) {
val now = SystemClock.elapsedRealtime()
if (now - lastClicked > 500L) {
lastClicked = now
listener()
}
}
})
}

fun CompoundButton.setOnCheckedChangeByUserListener(listener: (isChecked: Boolean) -> Unit) {
setOnCheckedChangeListener { button, isChecked ->
if (button.isPressed) {
listener(isChecked)
}
}
}

Expand All @@ -98,9 +121,9 @@ fun TextView.setText(a: TypedArray, @StyleableRes index: Int) {
fun ViewPager2.makeLessSensitive() {
try {
val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
.apply { isAccessible = true }
.apply { isAccessible = true }
val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop")
.apply { isAccessible = true }
.apply { isAccessible = true }

val recyclerView = recyclerViewField.get(this)
val touchSlop = touchSlopField.get(recyclerView) as Int
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/kotlin/me/xizzhu/android/joshua/utils/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ import androidx.annotation.VisibleForTesting

val Context.application: Application get() = applicationContext as Application

val Context.appVersionName: String
get() = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
application.packageManager.getPackageInfo(application.packageName, 0).versionName
} else {
application.packageManager.getPackageInfo(application.packageName, PackageManager.PackageInfoFlags.of(0)).versionName
}

// On older devices, this only works on the threads with loopers.
fun Context.copyToClipBoard(label: CharSequence, text: CharSequence) {
(getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
.setPrimaryClip(ClipData.newPlainText(label, text))
.setPrimaryClip(ClipData.newPlainText(label, text))
}

fun Activity.shareToSystem(title: String, text: String) {
Expand Down Expand Up @@ -63,9 +70,9 @@ fun PackageManager.chooserForSharing(packageToExclude: String, title: String, te
if (packageToExclude != packageName) {
val labeledIntent = LabeledIntent(packageName, resolveInfo.loadLabel(this), resolveInfo.iconResource)
labeledIntent.setAction(Intent.ACTION_SEND).setPackage(packageName)
.setComponent(ComponentName(packageName, resolveInfo.activityInfo.name))
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, text)
.setComponent(ComponentName(packageName, resolveInfo.activityInfo.name))
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, text)
filteredIntents.add(labeledIntent)
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
<string name="toast_deleted">Translation deleted</string>
<string name="toast_verses_copied">Copied to clipboard.</string>
<string name="toast_search_result">%1$d result(s) found.</string>
<string name="toast_backed_up">Successfully backed-up.</string>
<string name="toast_restored">Successfully restored.</string>
<string name="toast_backup_restore_succeeded">Successfully finished.</string>
<string name="toast_unknown_error">Unknown error</string>

<string name="action_more_translation">More</string>
Expand Down
Loading