Skip to content

Commit

Permalink
custom patches done
Browse files Browse the repository at this point in the history
  • Loading branch information
DerTyp7214 committed Dec 25, 2022
1 parent 35284bc commit 07998be
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ android {
applicationId "de.dertyp7214.rboardpatcher"
minSdk 26
targetSdk 33
versionCode 2300
versionName "2.3"
versionCode 2400
versionName "2.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.card.MaterialCardView
Expand Down Expand Up @@ -51,21 +52,29 @@ class PatchAdapter(
}
}

fun select(vararg name: String) = select(name.toList())
fun select(vararg name: String, internal: Boolean = true) = select(name.toList(), internal)

fun select(list: List<String>) {
@SuppressLint("NotifyDataSetChanged")
fun select(list: List<String>, internal: Boolean = true) {
val tmp = selected.map { Pair(it.key.getSafeName(), it.key) }
list.forEach { patchName ->
tmp[patchName]?.let { selected.set(it, true, internal = true) }
tmp[patchName]?.let {
selected.set(it, true, internal)
if (!internal) notifyItemChanged(this@PatchAdapter.list.indexOf(it))
}
}
}

fun unselect(vararg name: String) = unselect(name.toList())
fun unselect(vararg name: String, internal: Boolean = true) = unselect(name.toList(), internal)

fun unselect(list: List<String>) {
@SuppressLint("NotifyDataSetChanged")
fun unselect(list: List<String>, internal: Boolean = true) {
val tmp = selected.map { Pair(it.key.getSafeName(), it.key) }
list.forEach { patchName ->
tmp[patchName]?.let { selected.set(it, false, internal = true) }
tmp[patchName]?.let {
selected.set(it, false, internal)
if (!internal) notifyItemChanged(this@PatchAdapter.list.indexOf(it))
}
}
}

Expand All @@ -80,6 +89,7 @@ class PatchAdapter(
val title: TextView = v.findViewById(R.id.title)
val author: TextView = v.findViewById(R.id.author)
val newTag: TextView = v.findViewById(R.id.newTag)
val image: ImageView = v.findViewById(R.id.imageView)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -101,6 +111,8 @@ class PatchAdapter(

holder.newTag.visibility = if (patchMeta.date > previousVisit) View.VISIBLE else View.GONE

holder.image.setImageResource(if (patchMeta.customName != null) R.drawable.ic_patch_filled else R.drawable.ic_patch)

holder.root.setOnLongClickListener {
onLongPress(patchMeta)
true
Expand All @@ -126,8 +138,7 @@ class PatchAdapter(
map: List<PatchMeta>,
onSet: (items: HashMap<PatchMeta, Boolean>, PatchMeta, selected: Boolean) -> Unit
) : this(
HashMap(map.associateWith { false }),
onSet
HashMap(map.associateWith { false }), onSet
)

fun setItems(map: List<PatchMeta>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun Activity.openUrl(url: String) {
fun Activity.openDialog(
@LayoutRes layout: Int,
cancelable: Boolean = true,
onDismiss: (DialogInterface) -> Unit = {},
onCancel: (DialogInterface) -> Unit = {},
block: View.(DialogInterface) -> Unit
): AlertDialog {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) content.setRenderEffect(
Expand All @@ -59,8 +59,10 @@ fun Activity.openDialog(
return MaterialAlertDialogBuilder(this)
.setCancelable(cancelable)
.setView(view)
.setOnCancelListener {
onCancel(it)
}
.setOnDismissListener {
onDismiss(it)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) content.setRenderEffect(null)
}
.create().also { dialog ->
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/de/dertyp7214/rboardpatcher/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ class Patcher(private val context: Context) {
): Pair<File, File?> {
val patchFiles = arrayListOf<File>()
val fileMap = FileMap(arrayListOf(), hashMapOf())
val customValuesCss = StringBuilder()
patches.forEach { patch ->
val customValue = patch.patchMeta.customValue
progress((patches.indexOf(patch) + 1f) / patches.size * 100f, patch.patchMeta.name)

if (customValue != null) customValuesCss.appendLine("@def ${customValue.first} ${customValue.second};")

patch.getPatches(context, patcherPath)
.listFiles { file -> !file.name.endsWith(".meta") }?.apply {
val customImage = patch.patchMeta.customImage
val customValue = patch.patchMeta.customValue
forEach(patchFiles::add)
fileMap.patches.add(patch.patchMeta.getSafeName())
fileMap.patchFiles[patch.patchMeta.getSafeName()] = this.map {
Expand All @@ -63,6 +67,11 @@ class Patcher(private val context: Context) {
}
}
}
if (customValuesCss.isNotEmpty()) {
val customValuesFile = File(patcherPath, "custom_values.css")
customValuesFile.writeText(customValuesCss.toString())
patchFiles.add(customValuesFile)
}
val borderCssFiles = arrayListOf<File>()
val cssFiles = patchFiles.filter { it.name.endsWith(".css") }.filterNot {
it.name.endsWith("_border.css").also { border -> if (border) borderCssFiles.add(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.text.Editable
import android.text.TextWatcher
import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.result.PickVisualMediaRequest
Expand Down Expand Up @@ -50,7 +53,7 @@ import kotlin.math.roundToInt
@SuppressLint("NotifyDataSetChanged", "SetTextI18n")
class PatchActivity : BaseActivity() {

private val mutableLiveBitmap = MutableLiveData<Bitmap>()
private val mutableLiveBitmap = MutableLiveData<Bitmap?>()

private val imagePickerResultLauncher =
registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
Expand Down Expand Up @@ -144,9 +147,7 @@ class PatchActivity : BaseActivity() {
patchMeta.tags.any { it.equals("custom", true) }
&& patchMeta.tags.any { it.equals("image", true) }
) {
openDialog(R.layout.custom_image_patch_layout, true, {
unselect(patchMeta.name)
}) { dialog ->
openDialog(R.layout.custom_image_patch_layout, false) { dialog ->
val title = findViewById<TextView>(R.id.title)
val tags = findViewById<TextView>(R.id.tags)
val message = findViewById<TextView>(R.id.message)
Expand All @@ -156,17 +157,18 @@ class PatchActivity : BaseActivity() {
val replaceImageButton = findViewById<MaterialButton>(R.id.replaceImageButton)
val positiveButton = findViewById<Button>(R.id.ok)

positiveButton.isEnabled = false
replaceImageButton.isEnabled = false

val patch = Patch(patchMeta)

var customImage: Bitmap? = null

val observer = Observe { bitmap ->
if (bitmap != null) {
mutableLiveBitmap.value = null
mutableLiveBitmap.removeObserver(this)
image.setImageBitmap(bitmap)

positiveButton.isEnabled = true
customImage = bitmap
}
}

Expand All @@ -180,13 +182,9 @@ class PatchActivity : BaseActivity() {
}

positiveButton.setOnClickListener {
patchMeta.customImage =
mutableLiveBitmap.value?.let {
KeyValue(
patchMeta.customName ?: "",
it
)
}
patchMeta.customImage = customImage?.let {
KeyValue(patchMeta.customName ?: "", it)
}

dialog.dismiss()
}
Expand Down Expand Up @@ -222,7 +220,49 @@ class PatchActivity : BaseActivity() {
patchMeta.tags.any { it.equals("custom", true) }
&& patchMeta.tags.any { it.equals("value", true) }
) {
TODO("Implement Custom Value Patch")
openDialog(R.layout.custom_value_patch_layout, false) { dialog ->
val title = findViewById<TextView>(R.id.title)
val tags = findViewById<TextView>(R.id.tags)
val message = findViewById<TextView>(R.id.message)
val patchValue = findViewById<EditText>(R.id.patchValue)
val positiveButton = findViewById<Button>(R.id.ok)

positiveButton.isEnabled = false

patchValue.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
positiveButton.isEnabled = s?.isNotEmpty() == true
}

override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}

override fun onTextChanged(
s: CharSequence?,
start: Int,
before: Int,
count: Int
) {
}
})

positiveButton.setOnClickListener {
patchMeta.customValue = patchMeta.customName?.let { s ->
KeyValue(s, patchValue.text.toString())
}

dialog.dismiss()
}

title.text = patchMeta.name
tags.text = patchMeta.tags.joinToString(",")
message.text = patchMeta.description ?: "No Description!"
}
}
}
}
Expand Down Expand Up @@ -273,9 +313,14 @@ class PatchActivity : BaseActivity() {
}) { (patches, theme, filesMap) ->
unfiltered.clear()
unfiltered.addAll(patches.sortedWith { a, b ->
if (a.date > lastVisit && b.date > lastVisit) a.name.compareTo(b.name, true)
if (a.date > lastVisit && b.date > lastVisit) a.name.compareTo(
b.name,
true
)
else if (a.date > lastVisit) -1
else if (b.date > lastVisit) 1
else if (a.tags.contains("custom") && !b.tags.contains("custom")) -1
else if (!a.tags.contains("custom") && b.tags.contains("custom")) 1
else a.name.compareTo(b.name, true)
})
list.clear()
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_patch_filled.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000"
android:pathData="M20.5,11H19V7C19,5.89 18.1,5 17,5H13V3.5A2.5,2.5 0 0,0 10.5,1A2.5,2.5 0 0,0 8,3.5V5H4A2,2 0 0,0 2,7V10.8H3.5C5,10.8 6.2,12 6.2,13.5C6.2,15 5,16.2 3.5,16.2H2V20A2,2 0 0,0 4,22H7.8V20.5C7.8,19 9,17.8 10.5,17.8C12,17.8 13.2,19 13.2,20.5V22H17A2,2 0 0,0 19,20V16H20.5A2.5,2.5 0 0,0 23,13.5A2.5,2.5 0 0,0 20.5,11Z" />
</vector>
102 changes: 102 additions & 0 deletions app/src/main/res/layout/custom_value_patch_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.Material3.TitleLarge"
tools:text="TextView" />

<TextView
android:id="@+id/tags"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:ellipsize="marquee"
android:maxLines="1"
android:textAppearance="@style/TextAppearance.Material3.BodySmall"
tools:text="TextView" />

<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textAppearance="@style/TextAppearance.Material3.BodyMedium"
tools:text="TextView" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
tools:ignore="UseCompoundDrawables">

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:boxCornerRadiusBottomEnd="15dp"
app:boxCornerRadiusBottomStart="15dp"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/patchValue"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:hint="Hint" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="8dp"
android:gravity="end"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout3">

<com.google.android.material.button.MaterialButton
android:id="@+id/ok"
style="@style/Widget.Material3.Button.TextButton.Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@android:string/ok"
app:cornerRadius="@dimen/roundCorners" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 07998be

Please sign in to comment.