@@ -20,6 +20,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
2020import kotlinx.coroutines.flow.*
2121import kotlinx.coroutines.launch
2222import retrofit2.HttpException
23+ import systems.lupine.sheaf.util.toUserMessage
2324import javax.inject.Inject
2425
2526data class AdminPanelUiState (
@@ -62,7 +63,7 @@ class AdminPanelViewModel @Inject constructor(
6263 if (status.verified) loadAll()
6364 }
6465 .onFailure { e ->
65- _state .update { it.copy(isLoading = false , error = e.message ? : " Failed to check admin status" ) }
66+ _state .update { it.copy(isLoading = false , error = e.toUserMessage( " Failed to check admin status" ) ) }
6667 }
6768 }
6869 }
@@ -82,7 +83,7 @@ class AdminPanelViewModel @Inject constructor(
8283 }
8384 .onFailure { e ->
8485 val msg = if (e is HttpException && e.code() == 401 ) " Invalid credentials"
85- else e.message ? : " Step-up authentication failed"
86+ else e.toUserMessage( " Step-up authentication failed" )
8687 _state .update { it.copy(isSteppingUp = false , stepUpError = msg) }
8788 }
8889 }
@@ -119,7 +120,7 @@ class AdminPanelViewModel @Inject constructor(
119120 }
120121 }
121122 .onFailure { e ->
122- _state .update { it.copy(error = e.message ? : " Failed to load admin data" ) }
123+ _state .update { it.copy(error = e.toUserMessage( " Failed to load admin data" ) ) }
123124 }
124125 }
125126 }
@@ -134,7 +135,7 @@ class AdminPanelViewModel @Inject constructor(
134135 _state .update { it.copy(isCreatingInvite = false , invites = it.invites + invite) }
135136 }
136137 .onFailure { e ->
137- _state .update { it.copy(isCreatingInvite = false , createInviteError = e.message ? : " Failed to create invite" ) }
138+ _state .update { it.copy(isCreatingInvite = false , createInviteError = e.toUserMessage( " Failed to create invite" ) ) }
138139 }
139140 }
140141 }
@@ -143,7 +144,7 @@ class AdminPanelViewModel @Inject constructor(
143144 viewModelScope.launch {
144145 runCatching { api.deleteInvite(id) }
145146 .onSuccess { _state .update { it.copy(invites = it.invites.filter { i -> i.id != id }) } }
146- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to delete invite" ) } }
147+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to delete invite" ) ) } }
147148 }
148149 }
149150
@@ -163,47 +164,47 @@ class AdminPanelViewModel @Inject constructor(
163164 s.copy(users = s.users.map { if (it.id == id) updated else it })
164165 }
165166 }
166- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to update user" ) } }
167+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to update user" ) ) } }
167168 }
168169 }
169170
170171 fun approveUser (id : String ) {
171172 viewModelScope.launch {
172173 runCatching { api.approveUser(id) }
173174 .onSuccess { _state .update { it.copy(approvals = it.approvals.filter { u -> u.id != id }) } }
174- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to approve user" ) } }
175+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to approve user" ) ) } }
175176 }
176177 }
177178
178179 fun rejectUser (id : String ) {
179180 viewModelScope.launch {
180181 runCatching { api.rejectUser(id) }
181182 .onSuccess { _state .update { it.copy(approvals = it.approvals.filter { u -> u.id != id }) } }
182- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to reject user" ) } }
183+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to reject user" ) ) } }
183184 }
184185 }
185186
186187 fun runRetention () {
187188 viewModelScope.launch {
188189 runCatching { api.runRetention() }
189190 .onSuccess { _state .update { it.copy(maintenanceMessage = " Retention job started" ) } }
190- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed " ) } }
191+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage() ) } }
191192 }
192193 }
193194
194195 fun runCleanup () {
195196 viewModelScope.launch {
196197 runCatching { api.runCleanup() }
197198 .onSuccess { _state .update { it.copy(maintenanceMessage = " Cleanup job started" ) } }
198- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed " ) } }
199+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage() ) } }
199200 }
200201 }
201202
202203 fun runStorageAudit () {
203204 viewModelScope.launch {
204205 runCatching { api.getStorageStats() }
205206 .onSuccess { _state .update { it.copy(maintenanceMessage = " Storage stats refreshed" ) } }
206- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed " ) } }
207+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage() ) } }
207208 }
208209 }
209210
@@ -213,7 +214,7 @@ class AdminPanelViewModel @Inject constructor(
213214 .onSuccess { _state .update { it.copy(recoveryMessage = " Password reset successfully" ) } }
214215 .onFailure { e ->
215216 val msg = if (e is HttpException && e.code() == 403 ) " Insufficient permissions"
216- else e.message ? : " Failed to reset password"
217+ else e.toUserMessage( " Failed to reset password" )
217218 _state .update { it.copy(error = msg) }
218219 }
219220 }
@@ -232,7 +233,7 @@ class AdminPanelViewModel @Inject constructor(
232233 }
233234 .onFailure { e ->
234235 val msg = if (e is HttpException && e.code() == 409 ) " Email already in use"
235- else e.message ? : " Failed to change email"
236+ else e.toUserMessage( " Failed to change email" )
236237 _state .update { it.copy(error = msg) }
237238 }
238239 }
@@ -249,7 +250,7 @@ class AdminPanelViewModel @Inject constructor(
249250 )
250251 }
251252 }
252- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to disable TOTP" ) } }
253+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to disable TOTP" ) ) } }
253254 }
254255 }
255256
@@ -264,7 +265,7 @@ class AdminPanelViewModel @Inject constructor(
264265 )
265266 }
266267 }
267- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to verify email" ) } }
268+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to verify email" ) ) } }
268269 }
269270 }
270271
@@ -279,7 +280,7 @@ class AdminPanelViewModel @Inject constructor(
279280 )
280281 }
281282 }
282- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to cancel deletion" ) } }
283+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to cancel deletion" ) ) } }
283284 }
284285 }
285286
@@ -295,7 +296,7 @@ class AdminPanelViewModel @Inject constructor(
295296 ) }
296297 }
297298 .onFailure { e ->
298- _state .update { it.copy(isSavingAnnouncement = false , announcementError = e.message ? : " Failed to create announcement" ) }
299+ _state .update { it.copy(isSavingAnnouncement = false , announcementError = e.toUserMessage( " Failed to create announcement" ) ) }
299300 }
300301 }
301302 }
@@ -314,7 +315,7 @@ class AdminPanelViewModel @Inject constructor(
314315 }
315316 }
316317 .onFailure { e ->
317- _state .update { it.copy(isSavingAnnouncement = false , announcementError = e.message ? : " Failed to update announcement" ) }
318+ _state .update { it.copy(isSavingAnnouncement = false , announcementError = e.toUserMessage( " Failed to update announcement" ) ) }
318319 }
319320 }
320321 }
@@ -323,7 +324,7 @@ class AdminPanelViewModel @Inject constructor(
323324 viewModelScope.launch {
324325 runCatching { api.deleteAnnouncement(id) }
325326 .onSuccess { _state .update { it.copy(announcements = it.announcements.filter { a -> a.id != id }) } }
326- .onFailure { e -> _state .update { it.copy(error = e.message ? : " Failed to delete announcement" ) } }
327+ .onFailure { e -> _state .update { it.copy(error = e.toUserMessage( " Failed to delete announcement" ) ) } }
327328 }
328329 }
329330
0 commit comments