Skip to content

Commit 367e8a7

Browse files
committed
Simplify toastAsync
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
1 parent 62f8860 commit 367e8a7

3 files changed

Lines changed: 13 additions & 39 deletions

File tree

app/src/main/java/at/bitfire/icsdroid/model/AddSubscriptionModel.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,10 @@ class AddSubscriptionModel @AssistedInject constructor(
151151
// sync the subscription to reflect the changes in the calendar provider
152152
SyncWorker.run(context)
153153
}
154-
toastAsync(
155-
context,
156-
messageResId = R.string.add_calendar_created
157-
)
154+
toastAsync(context) { context.getString(R.string.add_calendar_created) }
158155
} catch (e: Exception) {
159156
Log.e(Constants.TAG, "Couldn't create calendar", e)
160-
toastAsync(
161-
context,
162-
message = { e.localizedMessage ?: e.message }
163-
)
157+
toastAsync(context) { e.localizedMessage ?: e.message }
164158
} finally {
165159
uiState = uiState.copy(isCreating = false)
166160
}

app/src/main/java/at/bitfire/icsdroid/model/SubscriptionsModel.kt

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@ class SubscriptionsModel @Inject constructor(
182182

183183
fun onBackupExportRequested(uri: Uri) {
184184
viewModelScope.launch(Dispatchers.IO) {
185-
val toast = toastAsync(
186-
context,
187-
messageResId = R.string.backup_exporting
188-
)
185+
val toast = toastAsync(context) { context.getString(R.string.backup_exporting) }
189186

190187
val subscriptions = subscriptions.value
191188
Log.i(TAG, "Exporting ${subscriptions.size} subscriptions...")
@@ -204,26 +201,19 @@ class SubscriptionsModel @Inject constructor(
204201

205202
toastAsync(
206203
context,
207-
messageResId = R.string.backup_exported,
208204
cancelToast = toast,
209205
duration = Toast.LENGTH_SHORT
210-
)
206+
) { context.getString(R.string.backup_exported) }
211207
} catch (e: IOException) {
212208
Log.e(TAG, "Could not write export file.", e)
213-
toastAsync(
214-
context,
215-
messageResId = R.string.backup_export_error_io
216-
)
209+
toastAsync(context) { context.getString(R.string.backup_export_error_io) }
217210
}
218211
}
219212
}
220213

221214
fun onBackupImportRequested(uri: Uri) {
222215
viewModelScope.launch(Dispatchers.IO) {
223-
val toast = toastAsync(
224-
context,
225-
messageResId = R.string.backup_importing
226-
)
216+
val toast = toastAsync(context) { context.getString(R.string.backup_importing) }
227217

228218
try {
229219
val jsonString = context.contentResolver.openFileDescriptor(uri, "r")?.use { fd ->
@@ -234,9 +224,8 @@ class SubscriptionsModel @Inject constructor(
234224
if (jsonString == null) {
235225
toastAsync(
236226
context,
237-
messageResId = R.string.backup_import_error_io,
238227
cancelToast = toast
239-
)
228+
) { context.getString(R.string.backup_import_error_io) }
240229
return@launch
241230
}
242231

@@ -278,23 +267,20 @@ class SubscriptionsModel @Inject constructor(
278267
Log.e(TAG, "Could not load JSON: $e")
279268
toastAsync(
280269
context,
281-
messageResId = R.string.backup_import_error_json,
282270
cancelToast = toast
283-
)
271+
) { context.getString(R.string.backup_import_error_json) }
284272
} catch (e: SecurityException) {
285273
Log.e(TAG, "Could not load JSON: $e")
286274
toastAsync(
287275
context,
288-
messageResId = R.string.backup_import_error_security,
289276
cancelToast = toast
290-
)
277+
) { context.getString(R.string.backup_import_error_security) }
291278
} catch (e: IOException) {
292279
Log.e(TAG, "Could not load JSON: $e")
293280
toastAsync(
294281
context,
295-
messageResId = R.string.backup_import_error_io,
296282
cancelToast = toast
297-
)
283+
) { context.getString(R.string.backup_import_error_io) }
298284
}
299285
}
300286
}

app/src/main/java/at/bitfire/icsdroid/model/ViewModelUtils.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@ package at.bitfire.icsdroid.model
22

33
import android.content.Context
44
import android.widget.Toast
5-
import androidx.annotation.StringRes
65
import kotlinx.coroutines.Dispatchers
76
import kotlinx.coroutines.withContext
87

98
suspend fun toastAsync(
109
context: Context,
11-
message: Context.() -> String? = { null },
12-
@StringRes messageResId: Int? = null,
1310
cancelToast: Toast? = null,
14-
duration: Int = Toast.LENGTH_LONG
11+
duration: Int = Toast.LENGTH_LONG,
12+
message: Context.() -> String?
1513
): Toast? = withContext(Dispatchers.Main) {
1614
cancelToast?.cancel()
1715

1816
val msg = message(context)
19-
when {
20-
msg != null -> Toast.makeText(context, msg, duration)
21-
messageResId != null -> Toast.makeText(context, messageResId, duration)
22-
else -> return@withContext null
23-
}.also { it.show() }
17+
Toast.makeText(context, msg, duration).also { it.show() }
2418
}

0 commit comments

Comments
 (0)