-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSubscriptionSettingsUseCase.kt
More file actions
134 lines (111 loc) · 4.36 KB
/
SubscriptionSettingsUseCase.kt
File metadata and controls
134 lines (111 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package at.bitfire.icsdroid.model
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.net.toUri
import at.bitfire.icsdroid.HttpUtils
import at.bitfire.icsdroid.db.entity.Credential
import at.bitfire.icsdroid.db.entity.Subscription
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class SubscriptionSettingsUseCase @Inject constructor() {
data class UiState(
val url: String? = null,
val fileName: String? = null,
val urlError: String? = null,
val title: String? = null,
val color: Int? = null,
val customUserAgent: String? = null,
val ignoreAlerts: Boolean = false,
val defaultAlarmMinutes: Long? = null,
val defaultAllDayAlarmMinutes: Long? = null,
// advanced settings
val ignoreDescription: Boolean = false,
// credentials
val requiresAuth: Boolean = false,
val username: String? = null,
val password: String? = null,
val isInsecure: Boolean = false
) {
// computed settings
val validUrlInput: Boolean = url?.let { url ->
HttpUtils.acceptedProtocol(url.toUri())
} ?: false
}
var uiState by mutableStateOf(UiState())
private set
fun setUrl(value: String?) {
uiState = uiState.copy(url = value)
}
fun setFileName(value: String?) {
uiState = uiState.copy(fileName = value)
}
fun setUrlError(value: String?) {
uiState = uiState.copy(urlError = value)
}
fun setTitle(value: String) {
uiState = uiState.copy(title = value)
}
fun setColor(value: Int?) {
uiState = uiState.copy(color = value)
}
fun setCustomUserAgent(value: String?) {
// Update with NULL if text field is empty and do not allow whitespace
uiState = uiState.copy(customUserAgent = value?.takeIf { it.isNotBlank() })
}
fun setIgnoreAlerts(value: Boolean) {
uiState = uiState.copy(ignoreAlerts = value)
}
fun setDefaultAlarmMinutes(value: String?) {
uiState = uiState.copy(defaultAlarmMinutes = value?.toLongOrNull())
}
fun setDefaultAllDayAlarmMinutes(value: String?) {
uiState = uiState.copy(defaultAllDayAlarmMinutes = value?.toLongOrNull())
}
fun setIgnoreDescription(value: Boolean) {
uiState = uiState.copy(ignoreDescription = value)
}
fun update(subscription: Subscription, credential: Credential?) {
uiState = uiState.copy(
url = subscription.url.toString(),
title = subscription.displayName,
color = subscription.color,
customUserAgent = subscription.customUserAgent,
ignoreAlerts = subscription.ignoreEmbeddedAlerts,
defaultAlarmMinutes = subscription.defaultAlarmMinutes,
defaultAllDayAlarmMinutes = subscription.defaultAllDayAlarmMinutes,
ignoreDescription = subscription.ignoreDescription,
requiresAuth = credential != null,
username = credential?.username,
password = credential?.password
)
}
fun equalsSubscription(subscription: Subscription) =
uiState.url == subscription.url.toString()
&& uiState.title == subscription.displayName
&& uiState.color == subscription.color
&& uiState.customUserAgent == subscription.customUserAgent
&& uiState.ignoreAlerts == subscription.ignoreEmbeddedAlerts
&& uiState.defaultAlarmMinutes == subscription.defaultAlarmMinutes
&& uiState.defaultAllDayAlarmMinutes == subscription.defaultAllDayAlarmMinutes
&& uiState.ignoreDescription == subscription.ignoreDescription
fun setRequiresAuth(value: Boolean) {
uiState = uiState.copy(requiresAuth = value)
}
fun setUsername(value: String?) {
uiState = uiState.copy(username = value)
}
fun setPassword(value: String?) {
uiState = uiState.copy(password = value)
}
fun clearCredentials() {
uiState = uiState.copy(username = null, password = null)
}
fun setIsInsecure(value: Boolean) {
uiState = uiState.copy(isInsecure = value)
}
fun equalsCredential(credential: Credential) =
uiState.username == credential.username
&& uiState.password == credential.password
}