Skip to content

Commit 14a68f5

Browse files
committed
Printer preferences are now a custom preference type with a more options menu if configured.
Add printer menu option to unset/delete a configured printer
1 parent bf50ccc commit 14a68f5

10 files changed

Lines changed: 255 additions & 13 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package eu.pretix.pretixprint.ui
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import android.view.MenuInflater
6+
import android.view.MenuItem
7+
import android.view.View
8+
import android.view.View.VISIBLE
9+
import android.widget.ImageButton
10+
import androidx.appcompat.widget.PopupMenu
11+
import androidx.preference.Preference
12+
import androidx.preference.PreferenceViewHolder
13+
import eu.pretix.pretixprint.R
14+
15+
class PrinterPreference(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
16+
Preference(context, attrs, defStyleAttr) {
17+
18+
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
19+
20+
private val mLayoutResId: Int = R.layout.preference_two_target
21+
private val mWidgetLayoutResId = R.layout.preference_more_button
22+
23+
private var button: ImageButton? = null
24+
private var divider: View? = null
25+
26+
var setOnMenuItemClickListener = fun(_: MenuItem): Boolean { return false }
27+
var moreVisibility = VISIBLE
28+
set(value) {
29+
field = value
30+
divider?.visibility = moreVisibility
31+
button?.visibility = moreVisibility
32+
}
33+
34+
35+
init {
36+
layoutResource = mLayoutResId
37+
widgetLayoutResource = mWidgetLayoutResId
38+
}
39+
40+
override fun onBindViewHolder(holder: PreferenceViewHolder) {
41+
divider = holder.itemView.findViewById(R.id.two_target_divider)
42+
button = holder.itemView.findViewById(R.id.iBMore)
43+
val popup = PopupMenu(context, button!!)
44+
val inflater: MenuInflater = popup.menuInflater
45+
inflater.inflate(R.menu.menu_printer_operations, popup.menu)
46+
popup.setOnMenuItemClickListener(setOnMenuItemClickListener)
47+
button?.setOnClickListener {
48+
popup.show()
49+
}
50+
divider?.visibility = moreVisibility
51+
button?.visibility = moreVisibility
52+
super.onBindViewHolder(holder)
53+
}
54+
}

pretixprint/app/src/main/java/eu/pretix/pretixprint/ui/Settings.kt

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.pretix.pretixprint.ui
22

3+
import android.annotation.SuppressLint
34
import android.annotation.TargetApi
45
import android.content.Context
56
import android.content.Intent
@@ -8,6 +9,8 @@ import android.content.SharedPreferences
89
import android.os.Build
910
import android.os.Bundle
1011
import android.text.TextUtils
12+
import android.view.View.GONE
13+
import android.view.View.VISIBLE
1114
import android.webkit.WebView
1215
import androidx.annotation.StringRes
1316
import androidx.appcompat.app.AppCompatActivity
@@ -35,11 +38,19 @@ class SettingsFragment : PreferenceFragmentCompat() {
3538
setPreferencesFromResource(R.xml.preferences, rootKey)
3639

3740
for (type in types) {
38-
findPreference<Preference>("hardware_${type}printer_find")?.setOnPreferenceClickListener {
39-
val intent = Intent(activity, PrinterSetupActivity::class.java)
40-
intent.putExtra(PrinterSetupActivity.EXTRA_USECASE, type)
41-
startWithPIN(intent)
42-
return@setOnPreferenceClickListener true
41+
findPreference<PrinterPreference>("hardware_${type}printer_find")?.apply {
42+
setOnPreferenceClickListener {
43+
val intent = Intent(activity, PrinterSetupActivity::class.java)
44+
intent.putExtra(PrinterSetupActivity.EXTRA_USECASE, type)
45+
startWithPIN(intent)
46+
return@setOnPreferenceClickListener true
47+
}
48+
setOnMenuItemClickListener = { menuItem ->
49+
when (menuItem.itemId) {
50+
R.id.remove -> { confirmRemovePrinter(type); true }
51+
else -> false
52+
}
53+
}
4354
}
4455
}
4556

@@ -95,12 +106,13 @@ class SettingsFragment : PreferenceFragmentCompat() {
95106
override fun onResume() {
96107
super.onResume()
97108
for (type in types) {
98-
val pref = findPreference<Preference>("hardware_${type}printer_find")
99-
if (pref != null) {
109+
findPreference<PrinterPreference>("hardware_${type}printer_find")?.apply {
100110
if (!TextUtils.isEmpty(defaultSharedPreferences.getString("hardware_${type}printer_ip", ""))) {
101-
pref.summary = printerSummary(type)
102-
} else {
103-
pref.summary = ""
111+
moreVisibility = VISIBLE
112+
summary = printerSummary(type)
113+
} else {
114+
moreVisibility = GONE
115+
summary = ""
104116
}
105117
}
106118
}
@@ -197,6 +209,33 @@ class SettingsFragment : PreferenceFragmentCompat() {
197209
startActivity(intent)
198210
}
199211
}
212+
213+
fun confirmRemovePrinter(type: String) {
214+
pinProtect {
215+
val typeRef = resources.getIdentifier("settings_label_${type}printer", "string", requireActivity().packageName)
216+
val summary = printerSummary(type)
217+
val alert = MaterialAlertDialogBuilder(requireActivity())
218+
.setTitle(getString(R.string.pref_delete_printer, getString(typeRef)))
219+
.setMessage(summary)
220+
.setPositiveButton(R.string.action_delete) { dialog, _ ->
221+
removePrinter(type)
222+
onResume() // refresh preferences
223+
dialog.dismiss()
224+
}
225+
.setNegativeButton(android.R.string.cancel, null)
226+
.create()
227+
alert.show()
228+
}
229+
}
230+
231+
@SuppressLint("ApplySharedPref")
232+
fun removePrinter(type: String) {
233+
defaultSharedPreferences.edit()
234+
.remove("hardware_${type}printer_ip")
235+
.remove("hardware_${type}printer_printername")
236+
.remove("hardware_${type}printer_connection")
237+
.apply()
238+
}
200239
}
201240

202241
class SettingsActivity : AppCompatActivity() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="?attr/colorControlNormal">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M12,8c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM12,10c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2zM12,16c-1.1,0 -2,0.9 -2,2s0.9,2 2,2 2,-0.9 2,-2 -0.9,-2 -2,-2z"/>
10+
</vector>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<ImageButton
9+
android:id="@+id/iBMore"
10+
android:layout_width="0dp"
11+
android:layout_height="0dp"
12+
android:background="?attr/selectableItemBackgroundBorderless"
13+
app:layout_constraintBottom_toBottomOf="parent"
14+
app:layout_constraintEnd_toEndOf="parent"
15+
app:layout_constraintStart_toStartOf="parent"
16+
app:layout_constraintTop_toTopOf="parent"
17+
app:srcCompat="@drawable/ic_baseline_more_vert_24dp" />
18+
19+
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2017 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!-- Based off preference_two_target.xml from packages/SettingsLib -->
18+
<!-- Based off preference_material_settings.xml except that ripple on only on the left side. -->
19+
<LinearLayout
20+
xmlns:android="http://schemas.android.com/apk/res/android"
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:minHeight="?android:attr/listPreferredItemHeightSmall"
24+
android:gravity="center_vertical"
25+
android:background="@android:color/transparent"
26+
android:clipToPadding="false">
27+
<LinearLayout
28+
android:layout_width="0dp"
29+
android:layout_height="match_parent"
30+
android:layout_weight="1"
31+
android:background="?android:attr/selectableItemBackground"
32+
android:gravity="start|center_vertical"
33+
android:clipToPadding="false"
34+
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
35+
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
36+
<LinearLayout
37+
android:id="@+id/icon_frame"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:gravity="start|center_vertical"
41+
android:minWidth="56dp"
42+
android:orientation="horizontal"
43+
android:clipToPadding="false"
44+
android:paddingTop="4dp"
45+
android:paddingBottom="4dp">
46+
<!--
47+
<androidx.preference.internal.PreferenceImageView
48+
android:id="@android:id/icon"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
settings:maxWidth="48dp"
52+
settings:maxHeight="48dp" />
53+
-->
54+
</LinearLayout>
55+
<RelativeLayout
56+
android:layout_width="wrap_content"
57+
android:layout_height="wrap_content"
58+
android:layout_weight="1"
59+
android:paddingTop="16dp"
60+
android:paddingBottom="16dp">
61+
<TextView
62+
android:id="@android:id/title"
63+
android:layout_width="wrap_content"
64+
android:layout_height="wrap_content"
65+
android:singleLine="true"
66+
android:textAppearance="?android:attr/textAppearanceListItem"
67+
android:ellipsize="marquee" />
68+
<TextView
69+
android:id="@android:id/summary"
70+
android:layout_width="wrap_content"
71+
android:layout_height="wrap_content"
72+
android:layout_below="@android:id/title"
73+
android:layout_alignStart="@android:id/title"
74+
android:textAppearance="?attr/textAppearanceListItemSecondary"
75+
android:textColor="?android:attr/textColorSecondary"
76+
android:maxLines="10" />
77+
</RelativeLayout>
78+
</LinearLayout>
79+
80+
<LinearLayout
81+
android:id="@+id/two_target_divider"
82+
android:layout_width="wrap_content"
83+
android:layout_height="match_parent"
84+
android:gravity="start|center_vertical"
85+
android:orientation="horizontal"
86+
android:paddingTop="16dp"
87+
android:paddingBottom="16dp">
88+
<View
89+
android:layout_width="1dp"
90+
android:layout_height="match_parent"
91+
android:background="?android:attr/listDivider" />
92+
</LinearLayout>
93+
94+
<!-- Preference should place its actual preference widget here. -->
95+
<LinearLayout
96+
android:id="@android:id/widget_frame"
97+
android:layout_width="wrap_content"
98+
android:layout_height="match_parent"
99+
android:minWidth="64dp"
100+
android:gravity="center"
101+
android:orientation="vertical" />
102+
</LinearLayout>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
<!--<item
4+
android:id="@+id/maintenance"
5+
android:title="Maintenance Mode" />-->
6+
<item
7+
android:id="@+id/remove"
8+
android:title="@string/action_delete" />
9+
</menu>

pretixprint/app/src/main/res/values-de/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@
4848
<string name="err_field_required">Dieses Feld ist erforderlich.</string>
4949
<string name="action_cancel">Abbrechen</string>
5050
<string name="action_save">Speichern</string>
51+
<string name="action_delete">Entfernen</string>
5152
<string name="pref_printer_current">Aktuell in Verwendung: %s @ %s (%s)</string>
5253
<string name="pref_printer_current_short">Aktuell in Verwendung: %s</string>
54+
<string name="pref_delete_printer">%s entfernen?</string>
5355
<string name="button_label_test">Testseite drucken</string>
5456
<string name="dismiss">Schließen</string>
5557
<string name="field_label_macaddress">MAC Addresse</string>

pretixprint/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@
5757
<string name="err_field_invalid">This field is invalid.</string>
5858
<string name="action_cancel">Cancel</string>
5959
<string name="action_save">Save</string>
60+
<string name="action_delete">Delete</string>
6061
<string name="pref_printer_current">Currently using printer %s @ %s (%s)</string>
6162
<string name="pref_printer_current_short">Currently using printer %s</string>
63+
<string name="pref_delete_printer">Delete %s?</string>
6264
<string name="button_label_test">Print test page</string>
6365
<string name="bluetooth_printer">Bluetooth Printer</string>
6466
<string name="network_printer">Network Printer</string>

pretixprint/app/src/main/res/values/styles.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<item name="android:windowActionBarOverlay">true</item>
99
<item name="actionBarTheme">@style/ThemeOverlay.App.ActionBar</item>
1010
<item name="preferenceTheme">@style/ThemeOverlay.App.Preference</item>
11+
<!-- natively available on api level 21+ -->
12+
<item name="textAppearanceListItemSecondary">@style/TextAppearance.AppCompat.Body1</item>
1113
</style>
1214

1315
<style name="ThemeOverlay.App.Preference" parent="PreferenceThemeOverlay">

pretixprint/app/src/main/res/xml/preferences.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
xmlns:android="http://schemas.android.com/apk/res/android">
44

55
<PreferenceCategory android:title="@string/settings_label_ticketprinter">
6-
<Preference
6+
<eu.pretix.pretixprint.ui.PrinterPreference
77
android:key="hardware_ticketprinter_find"
88
android:title="@string/settings_label_find"
99
/>
1010
</PreferenceCategory>
1111

1212
<PreferenceCategory android:title="@string/settings_label_badgeprinter">
13-
<Preference
13+
<eu.pretix.pretixprint.ui.PrinterPreference
1414
android:key="hardware_badgeprinter_find"
1515
android:title="@string/settings_label_find"
1616
/>
1717
</PreferenceCategory>
1818

1919
<PreferenceCategory android:title="@string/settings_label_receiptprinter">
20-
<Preference android:key="hardware_receiptprinter_find" android:title="@string/settings_label_find"/>
20+
<eu.pretix.pretixprint.ui.PrinterPreference
21+
android:key="hardware_receiptprinter_find"
22+
android:title="@string/settings_label_find"
23+
/>
2124
<eu.pretix.pretixprint.ui.ProtectedListPreference
2225
android:entries="@array/receipt_width"
2326
android:entryValues="@array/receipt_cpl"

0 commit comments

Comments
 (0)