-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintenance: replace output with recycler
- Loading branch information
Showing
5 changed files
with
179 additions
and
59 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
pretixprint/app/src/main/java/eu/pretix/pretixprint/ui/HexdumpAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package eu.pretix.pretixprint.ui | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import eu.pretix.pretixprint.R | ||
import eu.pretix.pretixprint.util.DirectedHexdumpByteArrayHolder | ||
import eu.pretix.pretixprint.util.DirectedHexdumpCollection | ||
|
||
class HexdumpAdapter: | ||
ListAdapter<DirectedHexdumpByteArrayHolder, HexdumpAdapter.HexdumpViewHolder>(HexdumpDiffCallback) { | ||
|
||
class HexdumpViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
private val tvDirection: TextView = itemView.findViewById(R.id.tvDirection) | ||
private val tvHex: TextView = itemView.findViewById(R.id.tvHex) | ||
private val tvAscii: TextView = itemView.findViewById(R.id.tvAscii) | ||
private var currentHexdump: DirectedHexdumpByteArrayHolder? = null | ||
|
||
|
||
fun bind(item: DirectedHexdumpByteArrayHolder) { | ||
currentHexdump = item | ||
|
||
tvDirection.text = item.direction.toString() | ||
tvHex.text = item.toHex() | ||
tvAscii.text = item.toAscii() | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HexdumpViewHolder { | ||
val view = LayoutInflater.from(parent.context) | ||
.inflate(R.layout.item_maintenance_row, parent, false) | ||
return HexdumpViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: HexdumpViewHolder, position: Int) { | ||
holder.bind(getItem(position)) | ||
} | ||
} | ||
|
||
object HexdumpDiffCallback : DiffUtil.ItemCallback<DirectedHexdumpByteArrayHolder>() { | ||
override fun areItemsTheSame(oldItem: DirectedHexdumpByteArrayHolder, newItem: DirectedHexdumpByteArrayHolder): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: DirectedHexdumpByteArrayHolder, newItem: DirectedHexdumpByteArrayHolder): Boolean { | ||
return oldItem.same(newItem) | ||
} | ||
} | ||
|
||
class AdapterBoundDirectedHexdumpCollection(maxSize: Int = 16, val listAdapter: HexdumpAdapter): DirectedHexdumpCollection(maxSize) { | ||
init { | ||
listAdapter.submitList(collection) | ||
} | ||
|
||
fun pushBytesAndNotify(dir: DirectedHexdumpByteArrayHolder.Direction, ba: ByteArray, forceNew: Boolean = false) { | ||
var fN = forceNew | ||
ba.forEach { | ||
pushByteAndNotify(dir, it, fN) | ||
fN = false | ||
} | ||
} | ||
|
||
fun pushByteAndNotify(dir: DirectedHexdumpByteArrayHolder.Direction, b: Byte, forceNew: Boolean = false) { | ||
var new = false | ||
if (collection.isEmpty() || forceNew) { | ||
collection.add(DirectedHexdumpByteArrayHolder(dir, maxSize)) | ||
new = true | ||
} | ||
if (collection.last().direction != dir) { | ||
collection.add(DirectedHexdumpByteArrayHolder(dir, maxSize)) | ||
new = true | ||
} | ||
if (collection.last().isFull()) { | ||
collection.add(DirectedHexdumpByteArrayHolder(dir, maxSize)) | ||
new = true | ||
} | ||
collection.last().push(b) | ||
if (new) { | ||
listAdapter.notifyItemInserted(collection.size - 1) | ||
} else { | ||
listAdapter.notifyItemChanged(collection.size - 1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
pretixprint/app/src/main/res/layout/item_maintenance_row.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<TextView | ||
android:id="@+id/tvDirection" | ||
android:layout_width="16dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="0" | ||
android:typeface="monospace" | ||
tools:text="->" /> | ||
|
||
<TextView | ||
android:id="@+id/tvHex" | ||
android:ems="48" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="2" | ||
android:typeface="monospace" | ||
tools:text="00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f"/> | ||
|
||
<LinearLayout | ||
android:id="@+id/divider" | ||
android:layout_width="wrap_content" | ||
android:layout_height="match_parent" | ||
android:layout_weight="0" | ||
android:gravity="start|center_vertical" | ||
android:paddingHorizontal="3dp" | ||
android:orientation="horizontal"> | ||
<View | ||
android:layout_width="1dp" | ||
android:layout_height="match_parent" | ||
android:background="?android:attr/listDivider" /> | ||
</LinearLayout> | ||
|
||
<TextView | ||
android:id="@+id/tvAscii" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:ems="16" | ||
android:layout_weight="2" | ||
android:typeface="monospace" | ||
tools:text="................" /> | ||
</LinearLayout> |