-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the amount of calls to exchange APIs to cut down on rate limit…
…ing. Add a new indicator for when an exchange has rate limited a widget. Space out updates for widgets when refreshing to avoid rate limiting. Renamed BitClude to Egera. Fixed a bug where widget prices were not updating in certain scenarios. Stop using Coingecko as the default for newly created widgets.
- Loading branch information
Showing
29 changed files
with
248 additions
and
190 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 37 additions & 20 deletions
57
bitcoin/src/main/java/com/brentpanther/bitcoinwidget/WidgetUpdater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
package com.brentpanther.bitcoinwidget | ||
|
||
import android.content.Context | ||
import com.brentpanther.bitcoinwidget.db.WidgetDatabase | ||
import com.brentpanther.bitcoinwidget.strategy.data.WidgetDataStrategy | ||
import com.brentpanther.bitcoinwidget.strategy.display.WidgetDisplayStrategy | ||
import com.brentpanther.bitcoinwidget.strategy.presenter.RemoteWidgetPresenter | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
object WidgetUpdater { | ||
|
||
suspend fun update(context: Context, widgetIds: IntArray, manual: Boolean) = coroutineScope { | ||
|
||
fun update(context: Context, widgetIds: IntArray, manual: Boolean) = CoroutineScope(Dispatchers.IO).launch { | ||
val dataStrategies = widgetIds.map { WidgetDataStrategy.getStrategy(it) } | ||
|
||
// update display immediately to avoid looking bad | ||
for (strategy in dataStrategies) { | ||
val widget = strategy.widget ?: continue | ||
dataStrategies.forEachIndexed { index, strategy -> | ||
val widget = strategy.widget ?: return@forEachIndexed | ||
val widgetPresenter = RemoteWidgetPresenter(context, widget) | ||
val displayStrategy = WidgetDisplayStrategy.getStrategy(context, widget, widgetPresenter) | ||
displayStrategy.refresh() | ||
} | ||
|
||
// update data | ||
for (strategy in dataStrategies) { | ||
strategy.loadData(manual) | ||
val success = strategy.loadData(manual) | ||
strategy.save() | ||
if (success) { | ||
val displayStrategy = WidgetDisplayStrategy.getStrategy(context, widget, widgetPresenter) | ||
displayStrategy.refresh() | ||
displayStrategy.save() | ||
// wait before refreshing the next widget to avoid rate limiting issues | ||
if (index != dataStrategies.lastIndex) { | ||
delay(20.seconds) | ||
} | ||
} | ||
} | ||
|
||
// data may cause display to need refreshed | ||
for (strategy in dataStrategies) { | ||
val widget = strategy.widget ?: continue | ||
val widgetPresenter = RemoteWidgetPresenter(context, widget) | ||
val displayStrategy = WidgetDisplayStrategy.getStrategy(context, widget, widgetPresenter) | ||
displayStrategy.refresh() | ||
displayStrategy.save() | ||
updateDisplays(context) | ||
} | ||
|
||
fun updateDisplays(context: Context) = CoroutineScope(Dispatchers.IO).launch { | ||
// change of data for different widgets may cause us to need to refresh all widgets | ||
val dao = WidgetDatabase.getInstance(context).widgetDao() | ||
val widgetIds = dao.getAll().map { it.widgetId }.toIntArray() | ||
val dataStrategies = widgetIds.map { WidgetDataStrategy.getStrategy(it) } | ||
(1..2).forEachIndexed { _, _ -> | ||
// refresh twice in case the size changed on the last widget | ||
dataStrategies.forEach { strategy -> | ||
val widget = strategy.widget ?: return@forEach | ||
val widgetPresenter = RemoteWidgetPresenter(context, widget) | ||
val displayStrategy = WidgetDisplayStrategy.getStrategy(context, widget, widgetPresenter) | ||
displayStrategy.refresh() | ||
displayStrategy.save() | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
bitcoin/src/main/java/com/brentpanther/bitcoinwidget/exchange/RateLimitedException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.brentpanther.bitcoinwidget.exchange | ||
|
||
class RateLimitedException : Exception() { | ||
} |
Oops, something went wrong.