Finish favorites sync

This commit is contained in:
NerdNumber9
2018-02-01 13:46:33 -05:00
parent 126da3979c
commit ded22f1717
10 changed files with 174 additions and 12 deletions

View File

@ -0,0 +1,33 @@
package exh.favorites
import android.content.Context
import android.text.Html
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import uy.kohesive.injekt.injectLazy
class FavoritesIntroDialog {
private val prefs: PreferencesHelper by injectLazy()
fun show(context: Context) = MaterialDialog.Builder(context)
.title("IMPORTANT FAVORITES SYNC NOTES")
.content(Html.fromHtml(FAVORITES_INTRO_TEXT))
.positiveText("Ok")
.onPositive { _, _ ->
prefs.eh_showSyncIntro().set(false)
}
.cancelable(false)
.show()
private val FAVORITES_INTRO_TEXT = """
1. Changes to category names in the app are <b>NOT</b> synced! Please <i>change the category names on ExHentai instead</i>. The category names will be copied from the ExHentai servers every sync.
<br><br>
2. The favorite categories on ExHentai correspond to the <b>first 10 categories in the app</b> (excluding the 'Default' category). <i>Galleries in other categories will <b>NOT</b> be synced!</i>
<br><br>
3. <font color='red'><b>ENSURE YOU HAVE A STABLE INTERNET CONNECTION WHEN SYNC IS IN PROGRESS!</b></font> If the internet disconnects while the app is syncing, your favorites may be left in a <i>partially-synced state</i>. This could be disastrous and can cause you to lose favorites. Backup regularly and be aware that <i>I will not be responsible for any loss of data</i>...
<br><br>
4. Keep the app open while favorites are syncing. Android will close apps that are in the background sometimes and that could be bad if it happens while the app is syncing.
<br><br>
This dialog will only popup once. You can read these notes again by going to 'Settings > E-Hentai > Show favorites sync notes'.
""".trimIndent()
}

View File

@ -36,6 +36,9 @@ class FavoritesSyncHelper(context: Context) {
private val galleryAdder = GalleryAdder()
private var lastThrottleTime: Long = 0
private var throttleTime: Long = 0
val status = BehaviorSubject.create<FavoritesSyncStatus>(FavoritesSyncStatus.Idle())
@Synchronized
@ -70,16 +73,23 @@ class FavoritesSyncHelper(context: Context) {
try {
db.inTransaction {
status.onNext(FavoritesSyncStatus.Processing("Calculating remote changes"))
val remoteChanges = storage.getChangedRemoteEntries(favorites.first)
val localChanges = storage.getChangedDbEntries()
val localChanges = if(prefs.eh_readOnlySync().getOrDefault()) {
null //Do not build local changes if they are not going to be applied
} else {
status.onNext(FavoritesSyncStatus.Processing("Calculating local changes"))
storage.getChangedDbEntries()
}
//Apply remote categories
status.onNext(FavoritesSyncStatus.Processing("Updating category names"))
applyRemoteCategories(favorites.second)
//Apply ChangeSets
//Apply change sets
applyChangeSetToLocal(remoteChanges, errors)
applyChangeSetToRemote(localChanges, errors)
if(localChanges != null)
applyChangeSetToRemote(localChanges, errors)
status.onNext(FavoritesSyncStatus.Processing("Cleaning up"))
storage.snapshotEntries()
@ -157,7 +167,7 @@ class FavoritesSyncHelper(context: Context) {
}
}
private fun explicitlyRetryExhRequest(retryCount: Int, request: Request): Boolean {
private fun explicitlyRetryExhRequest(retryCount: Int, request: Request, minDelay: Int = 0): Boolean {
var success = false
for(i in 1 .. retryCount) {
@ -204,8 +214,12 @@ class FavoritesSyncHelper(context: Context) {
}
//Apply additions
resetThrottle()
changeSet.added.forEachIndexed { index, it ->
status.onNext(FavoritesSyncStatus.Processing("Adding gallery ${index + 1} of ${changeSet.added.size} to remote server"))
status.onNext(FavoritesSyncStatus.Processing("Adding gallery ${index + 1} of ${changeSet.added.size} to remote server",
needWarnThrottle()))
throttle()
addGalleryRemote(it, errors)
}
@ -239,8 +253,12 @@ class FavoritesSyncHelper(context: Context) {
val categories = db.getCategories().executeAsBlocking()
//Apply additions
resetThrottle()
changeSet.added.forEachIndexed { index, it ->
status.onNext(FavoritesSyncStatus.Processing("Adding gallery ${index + 1} of ${changeSet.added.size} to local library"))
status.onNext(FavoritesSyncStatus.Processing("Adding gallery ${index + 1} of ${changeSet.added.size} to local library",
needWarnThrottle()))
throttle()
//Import using gallery adder
val result = galleryAdder.addGallery("${exh.baseUrl}${it.getUrl()}",
@ -262,13 +280,43 @@ class FavoritesSyncHelper(context: Context) {
db.setMangaCategories(insertedMangaCategories, insertedMangaCategoriesMangas)
}
fun throttle() {
//Throttle requests if necessary
val now = System.currentTimeMillis()
val timeDiff = now - lastThrottleTime
if(timeDiff < throttleTime)
Thread.sleep(throttleTime - timeDiff)
if(throttleTime < THROTTLE_MAX)
throttleTime += THROTTLE_INC
lastThrottleTime = System.currentTimeMillis()
}
fun resetThrottle() {
lastThrottleTime = 0
throttleTime = 0
}
fun needWarnThrottle()
= throttleTime >= THROTTLE_WARN
class IgnoredException : RuntimeException()
companion object {
private const val THROTTLE_MAX = 5000
private const val THROTTLE_INC = 10
private const val THROTTLE_WARN = 1000
}
}
sealed class FavoritesSyncStatus(val message: String) {
class Error(message: String) : FavoritesSyncStatus(message)
class Idle : FavoritesSyncStatus("Waiting for sync to start")
class Initializing : FavoritesSyncStatus("Initializing sync")
class Processing(message: String) : FavoritesSyncStatus(message)
class Processing(message: String, isThrottle: Boolean = false) : FavoritesSyncStatus(if(isThrottle)
(message + "\n\nSync is currently throttling (to avoid being banned from ExHentai) and may take a long to complete.")
else
message)
class Complete(val errors: List<String>) : FavoritesSyncStatus("Sync complete!")
}

View File

@ -64,6 +64,14 @@ class LocalFavoritesStorage {
}
}
fun clearSnapshots() {
realm.use {
it.trans {
it.delete(FavoriteEntry::class.java)
}
}
}
private fun getChangedEntries(entries: Sequence<FavoriteEntry>): ChangeSet {
return realm.use { realm ->
val terminated = entries.toList()