Skip to content

Commit

Permalink
Merge pull request #2169 from gelafin/feature/#1387_add_share_options
Browse files Browse the repository at this point in the history
Prep for Feature/#1387 add verse share option
  • Loading branch information
tuomas2 authored Jun 9, 2022
2 parents d527d1d + 06793be commit ea350e9
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 132 deletions.
105 changes: 71 additions & 34 deletions app/src/main/java/net/bible/android/view/util/widget/ShareWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,62 +39,71 @@ import net.bible.service.common.CommonUtils
import net.bible.service.sword.SwordContentFacade
import java.util.*

class ShareWidget(context: Context, attributeSet: AttributeSet?, val selection: Selection):
class ShareWidget(context: Context, attributeSet: AttributeSet?, val selection: Selection) :
LinearLayout(context, attributeSet) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
private val bindings = ShareVersesBinding.inflate(inflater, this, true)

init {
CommonUtils.buildActivityComponent().inject(this)
bindings.run {
if(!selection.hasRange) {
// handle special cases for selections of only one verse
if (!selection.hasRange) {
toggleShowSelectionOnly.visibility = View.GONE
toggleShowEllipsis.visibility = View.GONE
}

// apply isChecked to toggles based on settings
toggleVersenumbers.isChecked = CommonUtils.settings.getBoolean("share_verse_numbers", true)
advertise.isChecked = CommonUtils.settings.getBoolean("share_show_add", true)
toggleShowReference.isChecked = CommonUtils.settings.getBoolean("share_show_reference", true)
toggleAbbreviateReference.isChecked = CommonUtils.settings.getBoolean("share_abbreviate_reference", true)
toggleShowVersion.isChecked = CommonUtils.settings.getBoolean("share_show_version", true)
toggleShowReferenceAtFront.isChecked = CommonUtils.settings.getBoolean("share_show_reference_at_front", true)
toggleNotes.visibility = if(selection.notes!= null) View.VISIBLE else View.GONE
toggleShowReferenceAtFront.isChecked =
CommonUtils.settings.getBoolean("share_show_reference_at_front", true)
toggleNotes.visibility = if (selection.notes != null) View.VISIBLE else View.GONE
toggleNotes.isChecked = CommonUtils.settings.getBoolean("show_notes", true)
toggleShowSelectionOnly.isChecked = CommonUtils.settings.getBoolean("show_selection_only", true)
toggleShowEllipsis.isChecked = CommonUtils.settings.getBoolean("show_ellipsis", true)
toggleShowReferenceAtFront.isChecked = CommonUtils.settings.getBoolean("share_show_ref_at_front_of_verse", false)
toggleShowReferenceAtFront.isChecked =
CommonUtils.settings.getBoolean("share_show_ref_at_front_of_verse", false)
toggleShowQuotes.isChecked = CommonUtils.settings.getBoolean("share_show_quotes", false)

toggleVersenumbers.setOnClickListener { updateText()}
advertise.setOnClickListener { updateText()}
toggleShowReference.setOnClickListener { updateText()}
toggleAbbreviateReference.setOnClickListener { updateText()}
toggleShowVersion.setOnClickListener { updateText()}
toggleShowReferenceAtFront.setOnClickListener { updateText()}
toggleNotes.setOnClickListener { updateText()}
toggleShowSelectionOnly.setOnClickListener { updateText()}
toggleShowEllipsis.setOnClickListener { updateText()}
toggleShowQuotes.setOnClickListener { updateText()}
// update text when any toggle is clicked
toggleVersenumbers.setOnClickListener { updateWidgetState() }
advertise.setOnClickListener { updateWidgetState() }
toggleShowReference.setOnClickListener { updateWidgetState() }
toggleAbbreviateReference.setOnClickListener { updateWidgetState() }
toggleShowVersion.setOnClickListener { updateWidgetState() }
toggleShowReferenceAtFront.setOnClickListener { updateWidgetState() }
toggleNotes.setOnClickListener { updateWidgetState() }
toggleShowSelectionOnly.setOnClickListener { updateWidgetState() }
toggleShowEllipsis.setOnClickListener { updateWidgetState() }
toggleShowQuotes.setOnClickListener { updateWidgetState() }
}

// update text automatically at end of share widget init
updateWidgetState()
}

/**
* Updates the following:
* - widget text, based on selected text and widget share options
* - available widget selection options, based on dependent widget options
* - CommonUtils counterparts of widget options
*/
private fun updateWidgetState() {
updateText()
updateSelectionOptions()
}

private fun updateText() {
val text = SwordContentFacade.getSelectionText(selection,
showVerseNumbers = bindings.toggleVersenumbers.isChecked,
advertiseApp = bindings.advertise.isChecked,
abbreviateReference = bindings.toggleAbbreviateReference.isChecked,
showNotes = bindings.toggleNotes.isChecked,
showVersion = bindings.toggleShowVersion.isChecked,
showReference = bindings.toggleShowReference.isChecked,
showReferenceAtFront = bindings.toggleShowReferenceAtFront.isChecked,
showSelectionOnly = bindings.toggleShowSelectionOnly.isChecked,
showEllipsis = bindings.toggleShowEllipsis.isChecked,
showQuotes = bindings.toggleShowQuotes.isChecked
)
val isRtl = TextUtils.getLayoutDirectionFromLocale(Locale(selection.book.language.code)) == LayoutDirection.RTL
bindings.preview.textDirection = if(isRtl) View.TEXT_DIRECTION_RTL else View.TEXT_DIRECTION_LTR
bindings.preview.text = text
/**
* Updates the following:
* - available widget selection options, based on dependent widget options
* - Global settings of widget options
*/
private fun updateSelectionOptions() {
// update widget share option settings
CommonUtils.settings.apply {
setBoolean("share_verse_numbers", bindings.toggleVersenumbers.isChecked)
setBoolean("share_show_add", bindings.advertise.isChecked)
Expand All @@ -107,25 +116,52 @@ class ShareWidget(context: Context, attributeSet: AttributeSet?, val selection:
setBoolean("share_show_ref_at_front_of_verse", bindings.toggleShowReferenceAtFront.isChecked)
setBoolean("share_show_quotes", bindings.toggleShowQuotes.isChecked)
}

// disable dependent child checkboxes if the parent is not checked
bindings.toggleAbbreviateReference.isEnabled = bindings.toggleShowReference.isChecked
bindings.toggleShowVersion.isEnabled = bindings.toggleShowReference.isChecked
bindings.toggleShowReferenceAtFront.isEnabled = bindings.toggleShowReference.isChecked
bindings.toggleShowEllipsis.isEnabled = bindings.toggleShowSelectionOnly.isChecked
}

/**
* Updates widget text, based on selected text and widget share options
*/
private fun updateText() {
// get currently selected text with markup, based on widget options
val text = SwordContentFacade.getSelectionText(
selection,
showVerseNumbers = bindings.toggleVersenumbers.isChecked,
advertiseApp = bindings.advertise.isChecked,
abbreviateReference = bindings.toggleAbbreviateReference.isChecked,
showNotes = bindings.toggleNotes.isChecked,
showVersion = bindings.toggleShowVersion.isChecked,
showReference = bindings.toggleShowReference.isChecked,
showReferenceAtFront = bindings.toggleShowReferenceAtFront.isChecked,
showSelectionOnly = bindings.toggleShowSelectionOnly.isChecked,
showEllipsis = bindings.toggleShowEllipsis.isChecked,
showQuotes = bindings.toggleShowQuotes.isChecked
)
val isRtl = TextUtils.getLayoutDirectionFromLocale(Locale(selection.book.language.code)) == LayoutDirection.RTL

// set widget text based on the new text
bindings.preview.textDirection = if (isRtl) View.TEXT_DIRECTION_RTL else View.TEXT_DIRECTION_LTR
bindings.preview.text = text
}

companion object {
fun dialog(context: Context, selection: Selection) {
AlertDialog.Builder(context).apply {
val layout = ShareWidget(context, null, selection)
setView(layout)
setPositiveButton(R.string.backup_share) {
_, _ ->
setPositiveButton(R.string.backup_share) { _, _ ->

val emailIntent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_TEXT, layout.bindings.preview.text)
type = "text/plain"
}
val chooserIntent = Intent.createChooser(emailIntent, context.getString(R.string.share_verse_menu_title))
val chooserIntent =
Intent.createChooser(emailIntent, context.getString(R.string.share_verse_menu_title))
context.startActivity(chooserIntent)

}
Expand All @@ -141,6 +177,7 @@ class ShareWidget(context: Context, attributeSet: AttributeSet?, val selection:
create().show()
}
}

fun dialog(context: Context, bookmark: BookmarkEntities.Bookmark) =
dialog(context, Selection(bookmark))
}
Expand Down
Loading

0 comments on commit ea350e9

Please sign in to comment.