2017-05-05 05:38:17 +02:00
package exh.ui.migration
import android.app.Activity
import android.content.pm.ActivityInfo
2019-04-07 10:43:10 +02:00
import android.os.Build
2017-05-05 05:38:17 +02:00
import android.text.Html
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.source.SourceManager
import exh.isExSource
import exh.isLewdSource
import timber.log.Timber
import uy.kohesive.injekt.injectLazy
import kotlin.concurrent.thread
class MetadataFetchDialog {
val db : DatabaseHelper by injectLazy ( )
val sourceManager : SourceManager by injectLazy ( )
val preferenceHelper : PreferencesHelper by injectLazy ( )
fun show ( context : Activity ) {
//Too lazy to actually deal with orientation changes
context . requestedOrientation = ActivityInfo . SCREEN _ORIENTATION _NOSENSOR
2019-04-06 13:35:36 +02:00
var running = true
2017-05-05 05:38:17 +02:00
val progressDialog = MaterialDialog . Builder ( context )
. title ( " Fetching library metadata " )
. content ( " Preparing library " )
. progress ( false , 0 , true )
2019-04-06 13:35:36 +02:00
. negativeText ( " Stop " )
. onNegative { dialog , which ->
running = false
dialog . dismiss ( )
notifyMigrationStopped ( context )
}
2017-05-05 05:38:17 +02:00
. cancelable ( false )
. canceledOnTouchOutside ( false )
. show ( )
thread {
2019-04-06 13:35:36 +02:00
val libraryMangas = db . getLibraryMangas ( ) . executeAsBlocking ( )
. filter { isLewdSource ( it . source ) }
. distinctBy { it . id }
context . runOnUiThread {
progressDialog . maxProgress = libraryMangas . size
}
val mangaWithMissingMetadata = libraryMangas
. filterIndexed { index , libraryManga ->
if ( index % 100 == 0 ) {
context . runOnUiThread {
progressDialog . setContent ( " [Stage 1/2] Scanning for missing metadata... " )
progressDialog . setProgress ( index + 1 )
}
2017-08-25 23:31:38 +02:00
}
2019-04-06 13:35:36 +02:00
db . getSearchMetadataForManga ( libraryManga . id !! ) . executeAsBlocking ( ) == null
}
. toList ( )
2017-05-05 05:38:17 +02:00
2019-04-06 13:35:36 +02:00
context . runOnUiThread {
progressDialog . maxProgress = mangaWithMissingMetadata . size
}
//Actual metadata fetch code
for ( ( i , manga ) in mangaWithMissingMetadata . withIndex ( ) ) {
if ( ! running ) break
2017-05-05 05:38:17 +02:00
context . runOnUiThread {
2019-04-06 13:35:36 +02:00
progressDialog . setContent ( " [Stage 2/2] Processing: ${manga.title} " )
progressDialog . setProgress ( i + 1 )
2017-05-05 05:38:17 +02:00
}
2019-04-06 13:35:36 +02:00
try {
val source = sourceManager . get ( manga . source )
source ?. let {
manga . copyFrom ( it . fetchMangaDetails ( manga ) . toBlocking ( ) . first ( ) )
2017-05-05 05:38:17 +02:00
}
2019-04-06 13:35:36 +02:00
} catch ( t : Throwable ) {
Timber . e ( t , " Could not migrate manga! " )
2017-05-05 05:38:17 +02:00
}
2019-04-06 13:35:36 +02:00
}
2017-05-05 05:38:17 +02:00
2019-04-06 13:35:36 +02:00
context . runOnUiThread {
2019-04-07 10:43:10 +02:00
// Ensure activity still exists before we do anything to the activity
if ( Build . VERSION . SDK _INT < Build . VERSION_CODES . JELLY _BEAN _MR1 || ! context . isDestroyed ) {
progressDialog . dismiss ( )
2017-05-05 05:38:17 +02:00
2019-04-07 10:43:10 +02:00
//Enable orientation changes again
context . requestedOrientation = ActivityInfo . SCREEN _ORIENTATION _NOSENSOR
2017-05-05 05:38:17 +02:00
2019-04-07 10:43:10 +02:00
if ( running ) displayMigrationComplete ( context )
}
2017-05-05 05:38:17 +02:00
}
}
}
2017-08-26 04:26:57 +02:00
fun askMigration ( activity : Activity , explicit : Boolean ) {
2017-05-05 05:38:17 +02:00
var extra = " "
db . getLibraryMangas ( ) . asRxSingle ( ) . subscribe {
2018-07-08 23:53:03 +02:00
if ( ! explicit && it . none { isLewdSource ( it . source ) } ) {
2019-04-06 13:35:36 +02:00
// Do not open dialog on startup if no manga
// Also do not check again
preferenceHelper . migrateLibraryAsked ( ) . set ( true )
2017-08-26 04:26:57 +02:00
} else {
//Not logged in but have ExHentai galleries
if ( ! preferenceHelper . enableExhentai ( ) . getOrDefault ( ) ) {
it . find { isExSource ( it . source ) } ?. let {
extra = " <b><font color='red'>If you use ExHentai, please log in first before fetching your library metadata!</font></b><br><br> "
}
}
activity . runOnUiThread {
MaterialDialog . Builder ( activity )
. title ( " Fetch library metadata " )
. content ( Html . fromHtml ( " You need to fetch your library's metadata before tag searching in the library will function.<br><br> " +
2019-04-06 13:35:36 +02:00
" This process may take a long time depending on your library size and will also use up a significant amount of internet bandwidth but can be stopped and started whenever you wish.<br><br> " +
2017-08-26 04:26:57 +02:00
extra +
" This process can be done later if required. " ) )
. positiveText ( " Migrate " )
. negativeText ( " Later " )
. onPositive { _ , _ -> show ( activity ) }
2019-04-06 13:35:36 +02:00
. onNegative { _ , _ -> adviseMigrationLater ( activity ) }
. onAny { _ , _ -> preferenceHelper . migrateLibraryAsked ( ) . set ( true ) }
2017-08-26 04:26:57 +02:00
. cancelable ( false )
. canceledOnTouchOutside ( false )
2017-08-26 09:40:59 +02:00
. show ( )
2017-05-05 05:38:17 +02:00
}
}
}
}
fun adviseMigrationLater ( activity : Activity ) {
MaterialDialog . Builder ( activity )
. title ( " Metadata fetch canceled " )
. content ( " Library metadata fetch has been canceled. \n \n " +
2017-12-01 03:07:26 +01:00
" You can run this operation later by going to: Settings > Advanced > Migrate library metadata " )
2017-05-05 05:38:17 +02:00
. positiveText ( " Ok " )
. cancelable ( true )
. canceledOnTouchOutside ( true )
. show ( )
}
2019-04-06 13:35:36 +02:00
fun notifyMigrationStopped ( activity : Activity ) {
MaterialDialog . Builder ( activity )
. title ( " Metadata fetch stopped " )
. content ( " Library metadata fetch has been stopped. \n \n " +
" You can continue this operation later by going to: Settings > Advanced > Migrate library metadata " )
. positiveText ( " Ok " )
. cancelable ( true )
. canceledOnTouchOutside ( true )
. show ( )
}
2017-05-05 05:38:17 +02:00
fun displayMigrationComplete ( activity : Activity ) {
MaterialDialog . Builder ( activity )
. title ( " Migration complete " )
. content ( " ${activity.getString(R.string.app_name)} is now ready for use! " )
. positiveText ( " Ok " )
. cancelable ( true )
. canceledOnTouchOutside ( true )
. show ( )
}
}