Skip to content

Commit 4751f70

Browse files
committed
android: add hearing aid adjustments
1 parent ce229be commit 4751f70

7 files changed

Lines changed: 501 additions & 320 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package me.kavishdevar.librepods.composables
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.layout.Arrangement
5+
import androidx.compose.foundation.layout.Box
6+
import androidx.compose.foundation.layout.Column
7+
import androidx.compose.foundation.layout.Row
8+
import androidx.compose.foundation.layout.fillMaxHeight
9+
import androidx.compose.foundation.layout.fillMaxWidth
10+
import androidx.compose.foundation.layout.height
11+
import androidx.compose.foundation.layout.padding
12+
import androidx.compose.foundation.layout.width
13+
import androidx.compose.foundation.shape.RoundedCornerShape
14+
import androidx.compose.material3.HorizontalDivider
15+
import androidx.compose.material3.Text
16+
import androidx.compose.runtime.Composable
17+
import androidx.compose.runtime.MutableState
18+
import androidx.compose.runtime.getValue
19+
import androidx.compose.runtime.mutableStateOf
20+
import androidx.compose.runtime.remember
21+
import androidx.compose.runtime.setValue
22+
import androidx.compose.ui.Alignment
23+
import androidx.compose.ui.Modifier
24+
import androidx.compose.ui.draw.clip
25+
import androidx.compose.ui.graphics.Color
26+
import androidx.compose.ui.input.pointer.PointerEventType
27+
import androidx.compose.ui.input.pointer.pointerInput
28+
import androidx.compose.ui.platform.LocalContext
29+
import androidx.compose.ui.text.TextStyle
30+
import androidx.compose.ui.text.font.Font
31+
import androidx.compose.ui.text.font.FontFamily
32+
import androidx.compose.ui.text.font.FontWeight
33+
import androidx.compose.ui.text.style.TextAlign
34+
import androidx.compose.ui.unit.dp
35+
import androidx.compose.ui.unit.sp
36+
import androidx.compose.ui.window.Dialog
37+
import dev.chrisbanes.haze.HazeState
38+
import dev.chrisbanes.haze.hazeEffect
39+
import dev.chrisbanes.haze.materials.CupertinoMaterials
40+
import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi
41+
import me.kavishdevar.librepods.R
42+
43+
@ExperimentalHazeMaterialsApi
44+
@Composable
45+
fun ConfirmationDialog(
46+
showDialog: MutableState<Boolean>,
47+
title: String,
48+
message: String,
49+
confirmText: String = "Enable",
50+
dismissText: String = "Cancel",
51+
onConfirm: () -> Unit,
52+
onDismiss: () -> Unit = { showDialog.value = false },
53+
hazeState: HazeState,
54+
isDarkTheme: Boolean,
55+
textColor: Color,
56+
activeTrackColor: Color
57+
) {
58+
if (showDialog.value) {
59+
Dialog(onDismissRequest = { showDialog.value = false }) {
60+
Box(
61+
modifier = Modifier
62+
.fillMaxWidth()
63+
.background(if (isDarkTheme) Color(0xFF1C1C1E).copy(alpha = 0.95f) else Color.White.copy(alpha = 0.95f), RoundedCornerShape(14.dp))
64+
.clip(RoundedCornerShape(14.dp))
65+
.hazeEffect(hazeState, CupertinoMaterials.regular())
66+
) {
67+
Column(horizontalAlignment = Alignment.CenterHorizontally) {
68+
androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(24.dp))
69+
Text(
70+
title,
71+
style = TextStyle(
72+
fontSize = 16.sp,
73+
fontWeight = FontWeight.Bold,
74+
color = textColor,
75+
fontFamily = FontFamily(Font(R.font.sf_pro))
76+
),
77+
textAlign = TextAlign.Center,
78+
modifier = Modifier.padding(horizontal = 16.dp)
79+
)
80+
androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(12.dp))
81+
Text(
82+
message,
83+
style = TextStyle(
84+
fontSize = 14.sp,
85+
color = textColor.copy(alpha = 0.8f),
86+
fontFamily = FontFamily(Font(R.font.sf_pro))
87+
),
88+
textAlign = TextAlign.Center,
89+
modifier = Modifier.padding(horizontal = 16.dp)
90+
)
91+
androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(16.dp))
92+
HorizontalDivider(
93+
thickness = 1.dp,
94+
color = Color(0x40888888),
95+
modifier = Modifier.fillMaxWidth()
96+
)
97+
var leftPressed by remember { mutableStateOf(false) }
98+
var rightPressed by remember { mutableStateOf(false) }
99+
val pressedColor = if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9)
100+
Row(
101+
modifier = Modifier
102+
.fillMaxWidth()
103+
.height(48.dp)
104+
.pointerInput(Unit) {
105+
awaitPointerEventScope {
106+
while (true) {
107+
val event = awaitPointerEvent()
108+
val position = event.changes.first().position
109+
val width = size.width.toFloat()
110+
val height = size.height.toFloat()
111+
val isWithinBounds = position.y >= 0 && position.y <= height
112+
val isLeft = position.x < width / 2
113+
event.changes.first().consume()
114+
when (event.type) {
115+
PointerEventType.Press -> {
116+
if (isWithinBounds) {
117+
leftPressed = isLeft
118+
rightPressed = !isLeft
119+
} else {
120+
leftPressed = false
121+
rightPressed = false
122+
}
123+
}
124+
PointerEventType.Move -> {
125+
if (isWithinBounds) {
126+
leftPressed = isLeft
127+
rightPressed = !isLeft
128+
} else {
129+
leftPressed = false
130+
rightPressed = false
131+
}
132+
}
133+
PointerEventType.Release -> {
134+
if (isWithinBounds) {
135+
if (leftPressed) {
136+
onDismiss()
137+
} else if (rightPressed) {
138+
onConfirm()
139+
}
140+
}
141+
leftPressed = false
142+
rightPressed = false
143+
}
144+
}
145+
}
146+
}
147+
},
148+
horizontalArrangement = Arrangement.Start,
149+
verticalAlignment = Alignment.CenterVertically
150+
) {
151+
Box(
152+
modifier = Modifier
153+
.weight(1f)
154+
.fillMaxHeight()
155+
.background(if (leftPressed) pressedColor else Color.Transparent),
156+
contentAlignment = Alignment.Center
157+
) {
158+
Text(dismissText, color = activeTrackColor)
159+
}
160+
Box(
161+
modifier = Modifier
162+
.width(1.dp)
163+
.fillMaxHeight()
164+
.background(Color(0x40888888))
165+
)
166+
Box(
167+
modifier = Modifier
168+
.weight(1f)
169+
.fillMaxHeight()
170+
.background(if (rightPressed) pressedColor else Color.Transparent),
171+
contentAlignment = Alignment.Center
172+
) {
173+
Text(confirmText, color = activeTrackColor)
174+
}
175+
}
176+
}
177+
}
178+
}
179+
}
180+
}

android/app/src/main/java/me/kavishdevar/librepods/composables/LoudSoundReductionSwitch.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,10 @@ fun LoudSoundReductionSwitch() {
7070
for (attempt in 1..3) {
7171
try {
7272
val data = attManager.read(ATTHandles.LOUD_SOUND_REDUCTION)
73-
if (data.size == 2) {
74-
loudSoundReductionEnabled = data[1].toInt() != 0
75-
Log.d("LoudSoundReduction", "Read attempt $attempt: enabled=${loudSoundReductionEnabled}")
76-
parsed = true
77-
break
78-
} else {
79-
Log.d("LoudSoundReduction", "Read attempt $attempt returned empty data")
80-
}
73+
loudSoundReductionEnabled = data[0].toInt() != 0
74+
Log.d("LoudSoundReduction", "Read attempt $attempt: enabled=${loudSoundReductionEnabled}")
75+
parsed = true
76+
break
8177
} catch (e: Exception) {
8278
Log.w("LoudSoundReduction", "Read attempt $attempt failed: ${e.message}")
8379
}

0 commit comments

Comments
 (0)