|
| 1 | +package com.simplemobiletools.notes.pro.adapters |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Color |
| 5 | +import android.text.SpannableString |
| 6 | +import android.text.style.StrikethroughSpan |
| 7 | +import android.view.Menu |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup |
| 10 | +import com.google.gson.Gson |
| 11 | +import com.google.gson.reflect.TypeToken |
| 12 | +import com.simplemobiletools.commons.activities.BaseSimpleActivity |
| 13 | +import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter |
| 14 | +import com.simplemobiletools.commons.extensions.beGoneIf |
| 15 | +import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor |
| 16 | +import com.simplemobiletools.commons.extensions.isBlackAndWhiteTheme |
| 17 | +import com.simplemobiletools.commons.helpers.LOWER_ALPHA_INT |
| 18 | +import com.simplemobiletools.commons.helpers.SORT_BY_CUSTOM |
| 19 | +import com.simplemobiletools.commons.views.MyRecyclerView |
| 20 | +import com.simplemobiletools.notes.pro.R |
| 21 | +import com.simplemobiletools.notes.pro.extensions.config |
| 22 | +import com.simplemobiletools.notes.pro.models.ChecklistItem |
| 23 | +import com.simplemobiletools.notes.pro.models.Note |
| 24 | +import com.simplemobiletools.notes.pro.models.NoteType |
| 25 | +import kotlinx.android.synthetic.main.open_note_item.view.open_note_item_holder |
| 26 | +import kotlinx.android.synthetic.main.open_note_item.view.open_note_item_text |
| 27 | +import kotlinx.android.synthetic.main.open_note_item.view.open_note_item_title |
| 28 | + |
| 29 | +class OpenNoteAdapter( |
| 30 | + activity: BaseSimpleActivity, var items: List<Note>, |
| 31 | + recyclerView: MyRecyclerView, itemClick: (Any) -> Unit |
| 32 | +) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { |
| 33 | + override fun getActionMenuId() = 0 |
| 34 | + |
| 35 | + override fun actionItemPressed(id: Int) {} |
| 36 | + |
| 37 | + override fun getSelectableItemCount() = itemCount |
| 38 | + |
| 39 | + override fun getIsItemSelectable(position: Int) = false |
| 40 | + |
| 41 | + override fun getItemSelectionKey(position: Int) = items.getOrNull(position)?.id?.toInt() |
| 42 | + |
| 43 | + override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id?.toInt() == key } |
| 44 | + |
| 45 | + override fun onActionModeCreated() {} |
| 46 | + |
| 47 | + override fun onActionModeDestroyed() {} |
| 48 | + |
| 49 | + override fun prepareActionMode(menu: Menu) {} |
| 50 | + |
| 51 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { |
| 52 | + return createViewHolder(R.layout.open_note_item, parent) |
| 53 | + } |
| 54 | + |
| 55 | + override fun onBindViewHolder(holder: ViewHolder, position: Int) { |
| 56 | + val item = items[position] |
| 57 | + holder.bindView(item, true, false) { itemView, layoutPosition -> |
| 58 | + setupView(itemView, item) |
| 59 | + } |
| 60 | + bindViewHolder(holder) |
| 61 | + } |
| 62 | + |
| 63 | + override fun getItemCount() = items.size |
| 64 | + |
| 65 | + private fun setupView(view: View, note: Note) { |
| 66 | + view.apply { |
| 67 | + setupCard(open_note_item_holder) |
| 68 | + open_note_item_title.apply { |
| 69 | + text = note.title |
| 70 | + setTextColor(properPrimaryColor) |
| 71 | + } |
| 72 | + val formattedText = note.getFormattedValue(context) |
| 73 | + open_note_item_text.beGoneIf(formattedText.isNullOrBlank()) |
| 74 | + open_note_item_text.apply { |
| 75 | + text = formattedText |
| 76 | + setTextColor(textColor) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private fun View.setupCard(holder: View) { |
| 82 | + if (context.isBlackAndWhiteTheme()) { |
| 83 | + holder.setBackgroundResource(R.drawable.black_dialog_background) |
| 84 | + } else { |
| 85 | + val cardBackgroundColor = if (backgroundColor == Color.BLACK) { |
| 86 | + Color.WHITE |
| 87 | + } else { |
| 88 | + Color.BLACK |
| 89 | + } |
| 90 | + val cardBackground = if (context.config.isUsingSystemTheme) { |
| 91 | + R.drawable.dialog_you_background |
| 92 | + } else { |
| 93 | + R.drawable.dialog_bg |
| 94 | + } |
| 95 | + holder.background = |
| 96 | + activity.resources.getColoredDrawableWithColor(cardBackground, cardBackgroundColor, LOWER_ALPHA_INT) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private fun Note.getFormattedValue(context: Context): CharSequence? { |
| 101 | + return when (type) { |
| 102 | + NoteType.TYPE_TEXT -> getNoteStoredValue(context) |
| 103 | + NoteType.TYPE_CHECKLIST -> { |
| 104 | + val checklistItemType = object : TypeToken<List<ChecklistItem>>() {}.type |
| 105 | + var items = Gson().fromJson<List<ChecklistItem>>(getNoteStoredValue(context), checklistItemType) ?: listOf() |
| 106 | + items = items.filter { it.title != null }.let { |
| 107 | + val sorting = context.config.sorting |
| 108 | + ChecklistItem.sorting = sorting |
| 109 | + if (ChecklistItem.sorting and SORT_BY_CUSTOM == 0) { |
| 110 | + it.sorted().let { |
| 111 | + if (context.config.moveDoneChecklistItems) { |
| 112 | + it.sortedBy { it.isDone } |
| 113 | + } else { |
| 114 | + it |
| 115 | + } |
| 116 | + } |
| 117 | + } else { |
| 118 | + it |
| 119 | + } |
| 120 | + } |
| 121 | + val linePrefix = "• " |
| 122 | + val stringifiedItems = items.joinToString(separator = System.lineSeparator()) { |
| 123 | + "${linePrefix}${it.title}" |
| 124 | + } |
| 125 | + |
| 126 | + val formattedText = SpannableString(stringifiedItems) |
| 127 | + var currentPos = 0 |
| 128 | + items.forEach { item -> |
| 129 | + currentPos += linePrefix.length |
| 130 | + if (item.isDone) { |
| 131 | + formattedText.setSpan(StrikethroughSpan(), currentPos, currentPos + item.title.length, 0) |
| 132 | + } |
| 133 | + currentPos += item.title.length |
| 134 | + currentPos += System.lineSeparator().length |
| 135 | + } |
| 136 | + formattedText |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments