@@ -269,17 +269,15 @@ fun Context.showAddDialog(
269269 loadingLayout?.visibility = View .VISIBLE
270270 scope.launch {
271271 try {
272- val result = withContext(Dispatchers .IO ) {
273- downloadImage(Remote (normalizedUrl))
272+ val error = updateWallpaper(normalizedUrl)
273+ if (error == null ) {
274+ onSuccess()
275+ dialog.dismiss()
276+ showSnackbar(" Done." )
277+ } else {
278+ sendButton.isEnabled = true
279+ input.error = error
274280 }
275- addHistory(normalizedUrl, result)
276- val timestamp =
277- ZonedDateTime .now().format(DateTimeFormatter .ISO_ZONED_DATE_TIME )
278- PreferenceManager .getDefaultSharedPreferences(this @showAddDialog)
279- .edit { putString(" last_update" , timestamp) }
280- onSuccess()
281- dialog.dismiss()
282- showSnackbar(" Done." )
283281 } catch (e: CancellationException ) {
284282 throw e
285283 } catch (e: Exception ) {
@@ -322,45 +320,50 @@ sealed class DownloadResult {
322320 data object NotModified : DownloadResult ()
323321}
324322
323+ private val updateGate = java.util.concurrent.atomic.AtomicBoolean (false )
324+
325325// TODO: updateWallpaper is used globally to update the wallpaper and should be a package
326326// The rest of the functions are only used by updateWallpaper and are internal to updateWallpaper
327- suspend fun Context.updateWallpaper (): String? {
327+ suspend fun Context.updateWallpaper (url : String? = null ): String? {
328328 val historyDao = HistoryDatabase .getInstance(this ).historyDao()
329329 val history = HistoryItem ()
330+ if (! updateGate.compareAndSet(false , true )) {
331+ Log .i(" updateWallpaper" , " update already in progress, skipping" )
332+ return " Update Already Running."
333+ }
330334 try {
335+ val normalizedUrl = url?.let { normalizeUrl(it) }
336+ if (url != null && normalizedUrl == null ) {
337+ Log .d(" updateWallpaper" , " invalid url: $url " )
338+ return " Invalid URL."
339+ }
331340 val dao = RemoteDatabase .getInstance(this ).remoteDao()
332- val remote = withContext(Dispatchers .IO ) { dao.getActive() }
341+ val remote = withContext(Dispatchers .IO ) {
342+ if (normalizedUrl != null ) Remote (normalizedUrl) else dao.getActive()
343+ }
333344 Log .d(" updateWallpaper" , " remote: $remote " )
334- if (remote != null ) {
335- history.remote = remote.url
336- val result = withContext(Dispatchers .IO ) { downloadImage(remote) }
337- when (result) {
338- is DownloadResult .Downloaded -> {
339- history.status = result.response.code
340- history.url = result.response.request.url.toString()
341- Log .d(" updateWallpaper" , " response: ${result.response} " )
342- AppLogs .d(this , " updateWallpaper: ${result.response.code} for ${remote.url} " )
343- }
344-
345- is DownloadResult .NotModified -> {
346- history.status = 304
347- history.url = remote.url
348- Log .i(" updateWallpaper" , " Image not modified, skipping wallpaper update" )
349- AppLogs .d(this , " updateWallpaper: 304 for ${remote.url} " )
350- }
345+ if (remote == null ) {
346+ AppLogs .w(this , " updateWallpaper: No Active Remote" )
347+ return " No Remotes."
348+ }
349+ val result = withContext(Dispatchers .IO ) { downloadImage(remote) }
350+ addHistory(remote.url, result)
351+ when (result) {
352+ is DownloadResult .Downloaded ->
353+ AppLogs .d(this , " updateWallpaper: ${result.response.code} for ${remote.url} " )
354+
355+ is DownloadResult .NotModified -> {
356+ Log .i(" updateWallpaper" , " Image not modified, skipping wallpaper update" )
357+ AppLogs .d(this , " updateWallpaper: 304 for ${remote.url} " )
351358 }
352- // TODO: Replace timestamp with history.timestamp
353- val timestamp: String =
354- ZonedDateTime .now().format(DateTimeFormatter .ISO_ZONED_DATE_TIME )
355- Log .d(" updateWallpaper" , " timestamp: $timestamp " )
356- val preferences = PreferenceManager .getDefaultSharedPreferences(this )
357- preferences.edit { putString(" last_update" , timestamp) }
358- Log .d(" updateWallpaper" , " history: $history " )
359- withContext(Dispatchers .IO ) { historyDao.add(history) }
360- return if (result is DownloadResult .NotModified ) " Image Not Modified." else null
361359 }
362- AppLogs .w(this , " updateWallpaper: No Active Remote" )
363- return " No Remotes."
360+ // TODO: Replace timestamp with history.timestamp
361+ val timestamp: String =
362+ ZonedDateTime .now().format(DateTimeFormatter .ISO_ZONED_DATE_TIME )
363+ Log .d(" updateWallpaper" , " timestamp: $timestamp " )
364+ val preferences = PreferenceManager .getDefaultSharedPreferences(this )
365+ preferences.edit { putString(" last_update" , timestamp) }
366+ return if (result is DownloadResult .NotModified ) " Image Not Modified." else null
364367 } catch (e: CancellationException ) {
365368 throw e
366369 } catch (e: Exception ) {
@@ -370,6 +373,8 @@ suspend fun Context.updateWallpaper(): String? {
370373 withContext(Dispatchers .IO ) { historyDao.add(history) }
371374 AppLogs .e(this , " updateWallpaper: Exception: ${e.message} " )
372375 return e.message ? : " Unknown Error"
376+ } finally {
377+ updateGate.set(false )
373378 }
374379}
375380
0 commit comments