mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	Finish batch add screen.
This commit is contained in:
		| @@ -105,6 +105,34 @@ | ||||
|             android:parentActivityName=".ui.setting.SettingsActivity" > | ||||
|         </activity> | ||||
|  | ||||
|         <activity | ||||
|             android:name="exh.ui.intercept.InterceptActivity" | ||||
|             android:label="TachiyomiEH"> | ||||
|             <intent-filter> | ||||
|                 <action android:name="android.intent.action.VIEW"/> | ||||
|  | ||||
|                 <category android:name="android.intent.category.DEFAULT"/> | ||||
|                 <category android:name="android.intent.category.BROWSABLE"/> | ||||
|  | ||||
|                 <data | ||||
|                     android:host="g.e-hentai.org" | ||||
|                     android:pathPrefix="/g/" | ||||
|                     android:scheme="http"/> | ||||
|                 <data | ||||
|                     android:host="g.e-hentai.org" | ||||
|                     android:pathPrefix="/g/" | ||||
|                     android:scheme="https"/> | ||||
|                 <data | ||||
|                     android:host="exhentai.org" | ||||
|                     android:pathPrefix="/g/" | ||||
|                     android:scheme="http"/> | ||||
|                 <data | ||||
|                     android:host="exhentai.org" | ||||
|                     android:pathPrefix="/g/" | ||||
|                     android:scheme="https"/> | ||||
|             </intent-filter> | ||||
|         </activity> | ||||
|  | ||||
|     </application> | ||||
|  | ||||
| </manifest> | ||||
|   | ||||
| @@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.ui.library.LibraryFragment | ||||
| import eu.kanade.tachiyomi.ui.recent_updates.RecentChaptersFragment | ||||
| import eu.kanade.tachiyomi.ui.recently_read.RecentlyReadFragment | ||||
| import eu.kanade.tachiyomi.ui.setting.SettingsActivity | ||||
| import exh.ui.batchadd.BatchAddFragment | ||||
| import kotlinx.android.synthetic.main.activity_main.* | ||||
| import kotlinx.android.synthetic.main.toolbar.* | ||||
| import uy.kohesive.injekt.injectLazy | ||||
| @@ -63,6 +64,7 @@ class MainActivity : BaseActivity() { | ||||
|                 R.id.nav_drawer_recently_read -> setFragment(RecentlyReadFragment.newInstance(), id) | ||||
|                 R.id.nav_drawer_catalogues -> setFragment(CatalogueFragment.newInstance(), id) | ||||
|                 R.id.nav_drawer_latest_updates -> setFragment(LatestUpdatesFragment.newInstance(), id) | ||||
|                 R.id.nav_drawer_batch_add -> setFragment(BatchAddFragment.newInstance(), id) | ||||
|                 R.id.nav_drawer_downloads -> setFragment(DownloadFragment.newInstance(), id) | ||||
|                 R.id.nav_drawer_settings -> { | ||||
|                     val intent = Intent(this, SettingsActivity::class.java) | ||||
|   | ||||
							
								
								
									
										54
									
								
								app/src/main/java/exh/GalleryAdder.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								app/src/main/java/exh/GalleryAdder.kt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| package exh | ||||
|  | ||||
| import eu.kanade.tachiyomi.data.database.DatabaseHelper | ||||
| import eu.kanade.tachiyomi.data.database.models.Manga | ||||
| import eu.kanade.tachiyomi.data.source.SourceManager | ||||
| import eu.kanade.tachiyomi.util.UrlUtil | ||||
| import exh.metadata.MetadataHelper | ||||
| import exh.metadata.copyTo | ||||
| import uy.kohesive.injekt.injectLazy | ||||
| import java.net.MalformedURLException | ||||
| import java.net.URL | ||||
|  | ||||
| class GalleryAdder { | ||||
|  | ||||
|     private val db: DatabaseHelper by injectLazy() | ||||
|  | ||||
|     private val sourceManager: SourceManager by injectLazy() | ||||
|  | ||||
|     private val metadataHelper = MetadataHelper() | ||||
|  | ||||
|     fun addGallery(url: String, fav: Boolean = false): Manga { | ||||
|         val source = when(URL(url).host) { | ||||
|             "g.e-hentai.org" -> 1 | ||||
|             "exhentai.org" -> 2 | ||||
|             else -> throw MalformedURLException("Not a valid gallery URL!") | ||||
|         } | ||||
|  | ||||
|         val sourceObj = sourceManager.get(source) | ||||
|  | ||||
|         val pathOnlyUrl = UrlUtil.getPath(url) | ||||
|  | ||||
|         //Use manga in DB if possible, otherwise, make a new manga | ||||
|         val manga = db.getManga(pathOnlyUrl, source).executeAsBlocking() | ||||
|                 ?: Manga.create(pathOnlyUrl, source).apply { | ||||
|             title = url | ||||
|         } | ||||
|  | ||||
|         sourceObj?.let { | ||||
|             //Copy basics | ||||
|             manga.copyFrom(sourceObj.fetchMangaDetails(manga).toBlocking().first()) | ||||
|  | ||||
|             //Apply metadata | ||||
|             metadataHelper.fetchMetadata(url, source == 2)?.copyTo(manga) | ||||
|         } | ||||
|  | ||||
|         if(fav) manga.favorite = true | ||||
|  | ||||
|         db.insertManga(manga).executeAsBlocking().insertedId()?.let { | ||||
|             manga.id = it | ||||
|         } | ||||
|  | ||||
|         return manga | ||||
|     } | ||||
| } | ||||
							
								
								
									
										126
									
								
								app/src/main/java/exh/ui/batchadd/BatchAddFragment.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								app/src/main/java/exh/ui/batchadd/BatchAddFragment.kt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,126 @@ | ||||
| package exh.ui.batchadd | ||||
|  | ||||
| import android.content.pm.ActivityInfo | ||||
| import android.os.Bundle | ||||
| import android.view.LayoutInflater | ||||
| import android.view.View | ||||
| import android.view.ViewGroup | ||||
| import com.afollestad.materialdialogs.MaterialDialog | ||||
| import eu.kanade.tachiyomi.R | ||||
| import eu.kanade.tachiyomi.ui.base.fragment.BaseFragment | ||||
| import exh.GalleryAdder | ||||
| import exh.metadata.nullIfBlank | ||||
| import kotlinx.android.synthetic.main.eh_fragment_batch_add.* | ||||
| import timber.log.Timber | ||||
| import kotlin.concurrent.thread | ||||
|  | ||||
| /** | ||||
|  * LoginActivity | ||||
|  */ | ||||
|  | ||||
| class BatchAddFragment : BaseFragment() { | ||||
|  | ||||
|     private val galleryAdder by lazy { GalleryAdder() } | ||||
|  | ||||
|     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?) | ||||
|         = inflater.inflate(R.layout.eh_fragment_batch_add, container, false)!! | ||||
|  | ||||
|     override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { | ||||
|         setToolbarTitle("Batch Add") | ||||
|  | ||||
|         setup() | ||||
|     } | ||||
|  | ||||
|     fun setup() { | ||||
|         btn_add_galleries.setOnClickListener { | ||||
|             val galleries = galleries_box.text.toString() | ||||
|             //Check text box has content | ||||
|             if(galleries.isNullOrBlank()) | ||||
|                 noGalleriesSpecified() | ||||
|  | ||||
|             //Too lazy to actually deal with orientation changes | ||||
|             activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR | ||||
|  | ||||
|             val splitGalleries = galleries.split("\n").map { | ||||
|                 it.trim().nullIfBlank() | ||||
|             }.filterNotNull() | ||||
|  | ||||
|             val dialog = MaterialDialog.Builder(context) | ||||
|                     .title("Adding galleries...") | ||||
|                     .progress(false, splitGalleries.size, true) | ||||
|                     .cancelable(false) | ||||
|                     .canceledOnTouchOutside(false) | ||||
|                     .show() | ||||
|  | ||||
|             val succeeded = mutableListOf<String>() | ||||
|             val failed = mutableListOf<String>() | ||||
|  | ||||
|             thread { | ||||
|                 splitGalleries.forEachIndexed { i, s -> | ||||
|                     activity.runOnUiThread { | ||||
|                         dialog.setContent("Processing: $s") | ||||
|                     } | ||||
|                     if(addGallery(s)) { | ||||
|                         succeeded.add(s) | ||||
|                     } else { | ||||
|                         failed.add(s) | ||||
|                     } | ||||
|                     activity.runOnUiThread { | ||||
|                         dialog.setProgress(i + 1) | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 //Show report | ||||
|                 if(succeeded.isEmpty()) succeeded += "None" | ||||
|                 if(failed.isEmpty()) failed += "None" | ||||
|                 val succeededReport = succeeded.joinToString(separator = "\n", prefix = "Added:\n") | ||||
|                 val failedReport = failed.joinToString(separator = "\n", prefix = "Failed:\n") | ||||
|  | ||||
|                 val summary = "Summary:\nAdded: ${succeeded.size} gallerie(s)\nFailed: ${failed.size} gallerie(s)" | ||||
|  | ||||
|                 val report = listOf(succeededReport, failedReport, summary).joinToString(separator = "\n\n") | ||||
|  | ||||
|                 activity.runOnUiThread { | ||||
|                     //Enable orientation changes again | ||||
|                     activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR | ||||
|  | ||||
|                     dialog.dismiss() | ||||
|  | ||||
|                     MaterialDialog.Builder(context) | ||||
|                             .title("Batch add report") | ||||
|                             .content(report) | ||||
|                             .positiveText("Ok") | ||||
|                             .cancelable(true) | ||||
|                             .canceledOnTouchOutside(true) | ||||
|                             .show() | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun addGallery(url: String): Boolean { | ||||
|         try { | ||||
|             galleryAdder.addGallery(url, true) | ||||
|         } catch(t: Throwable) { | ||||
|             Timber.e(t, "Could not add gallery!") | ||||
|             return false | ||||
|         } | ||||
|         return true | ||||
|     } | ||||
|  | ||||
|     fun noGalleriesSpecified() { | ||||
|         MaterialDialog.Builder(context) | ||||
|                 .title("No galleries to add!") | ||||
|                 .content("You must specify at least one gallery to add!") | ||||
|                 .positiveText("Ok") | ||||
|                 .onPositive { materialDialog, dialogAction -> materialDialog.dismiss() } | ||||
|                 .cancelable(true) | ||||
|                 .canceledOnTouchOutside(true) | ||||
|                 .show() | ||||
|     } | ||||
|  | ||||
|     companion object { | ||||
|         fun newInstance() = BatchAddFragment() | ||||
|     } | ||||
| } | ||||
							
								
								
									
										82
									
								
								app/src/main/java/exh/ui/intercept/InterceptActivity.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								app/src/main/java/exh/ui/intercept/InterceptActivity.kt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,82 @@ | ||||
| package exh.ui.intercept | ||||
|  | ||||
| import android.content.Intent | ||||
| import android.os.Bundle | ||||
| import android.view.MenuItem | ||||
| import com.afollestad.materialdialogs.MaterialDialog | ||||
| import eu.kanade.tachiyomi.R | ||||
| import eu.kanade.tachiyomi.ui.base.activity.BaseActivity | ||||
| import eu.kanade.tachiyomi.ui.manga.MangaActivity | ||||
| import exh.GalleryAdder | ||||
| import kotlinx.android.synthetic.main.toolbar.* | ||||
| import timber.log.Timber | ||||
| import kotlin.concurrent.thread | ||||
|  | ||||
| class InterceptActivity : BaseActivity() { | ||||
|  | ||||
|     private val galleryAdder = GalleryAdder() | ||||
|  | ||||
|     var finished = false | ||||
|  | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
|         setAppTheme() | ||||
|         super.onCreate(savedInstanceState) | ||||
|         setContentView(R.layout.eh_activity_intercept) | ||||
|  | ||||
|         setupToolbar(toolbar, backNavigation = false) | ||||
|  | ||||
|         if(savedInstanceState == null) | ||||
|             thread { setup() } | ||||
|     } | ||||
|  | ||||
|     fun setup() { | ||||
|         try { | ||||
|             processLink() | ||||
|         } catch(t: Throwable) { | ||||
|             Timber.e(t, "Could not intercept link!") | ||||
|             if(!finished) | ||||
|                 runOnUiThread { | ||||
|                     MaterialDialog.Builder(this) | ||||
|                             .title("Error") | ||||
|                             .content("Could not load this gallery!") | ||||
|                             .cancelable(true) | ||||
|                             .canceledOnTouchOutside(true) | ||||
|                             .cancelListener { onBackPressed() } | ||||
|                             .positiveText("Ok") | ||||
|                             .onPositive { materialDialog, dialogAction -> onBackPressed() } | ||||
|                             .dismissListener { onBackPressed() } | ||||
|                             .show() | ||||
|                 } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun processLink() { | ||||
|         if(Intent.ACTION_VIEW == intent.action) { | ||||
|             val manga = galleryAdder.addGallery(intent.dataString) | ||||
|  | ||||
|             if(!finished) | ||||
|                 startActivity(MangaActivity.newIntent(this, manga, true)) | ||||
|             onBackPressed() | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||||
|         when (item.itemId) { | ||||
|             android.R.id.home -> onBackPressed() | ||||
|             else -> return super.onOptionsItemSelected(item) | ||||
|         } | ||||
|         return true | ||||
|     } | ||||
|  | ||||
|     override fun onBackPressed() { | ||||
|         if(!finished) | ||||
|             runOnUiThread { | ||||
|                 super.onBackPressed() | ||||
|             } | ||||
|     } | ||||
|  | ||||
|     override fun onStop() { | ||||
|         super.onStop() | ||||
|         finished = true | ||||
|     } | ||||
| } | ||||
							
								
								
									
										9
									
								
								app/src/main/res/drawable/ic_playlist_add_black_24dp.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								app/src/main/res/drawable/ic_playlist_add_black_24dp.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|         android:width="24dp" | ||||
|         android:height="24dp" | ||||
|         android:viewportWidth="24.0" | ||||
|         android:viewportHeight="24.0"> | ||||
|     <path | ||||
|         android:fillColor="#FF000000" | ||||
|         android:pathData="M14,10H2v2h12v-2zm0,-4H2v2h12V6zm4,8v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zM2,16h8v-2H2v2z"/> | ||||
| </vector> | ||||
							
								
								
									
										48
									
								
								app/src/main/res/layout/eh_activity_intercept.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								app/src/main/res/layout/eh_activity_intercept.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <android.support.design.widget.CoordinatorLayout | ||||
|     xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent" | ||||
|     android:fitsSystemWindows="true"> | ||||
|  | ||||
|     <LinearLayout | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:orientation="vertical"> | ||||
|  | ||||
|         <android.support.design.widget.AppBarLayout | ||||
|             android:id="@+id/appbar" | ||||
|             android:layout_width="match_parent" | ||||
|             android:layout_height="wrap_content"> | ||||
|  | ||||
|             <include layout="@layout/toolbar"/> | ||||
|  | ||||
|         </android.support.design.widget.AppBarLayout> | ||||
|  | ||||
|         <FrameLayout | ||||
|             android:layout_width="match_parent" | ||||
|             android:layout_height="match_parent"> | ||||
|             <LinearLayout | ||||
|                 android:layout_height="wrap_content" | ||||
|                 android:layout_width="wrap_content" | ||||
|                 android:orientation="vertical" | ||||
|                 android:layout_gravity="center"> | ||||
|  | ||||
|                 <TextView | ||||
|                     android:text="Loading gallery..." | ||||
|                     android:layout_width="wrap_content" | ||||
|                     android:layout_height="wrap_content" | ||||
|                     android:textAppearance="@style/TextAppearance.Medium.Title" /> | ||||
|  | ||||
|                 <ProgressBar | ||||
|                     style="?android:attr/progressBarStyleLarge" | ||||
|                     android:layout_width="wrap_content" | ||||
|                     android:layout_height="wrap_content" | ||||
|                     android:layout_gravity="center" /> | ||||
|             </LinearLayout> | ||||
|         </FrameLayout> | ||||
|     </LinearLayout> | ||||
|  | ||||
| </android.support.design.widget.CoordinatorLayout> | ||||
							
								
								
									
										54
									
								
								app/src/main/res/layout/eh_fragment_batch_add.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								app/src/main/res/layout/eh_fragment_batch_add.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <android.support.design.widget.CoordinatorLayout | ||||
|     xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent" | ||||
|     android:fitsSystemWindows="true"> | ||||
|  | ||||
|     <android.support.constraint.ConstraintLayout | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent"> | ||||
|  | ||||
|         <TextView | ||||
|             android:text="Enter the galleries to add (separated by a new line):" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:id="@+id/textView" | ||||
|             android:textAppearance="@style/TextAppearance.Medium.Title" | ||||
|             android:layout_marginTop="16dp" | ||||
|             app:layout_constraintTop_toTopOf="parent" | ||||
|             android:layout_marginStart="16dp" | ||||
|             app:layout_constraintLeft_toLeftOf="parent" | ||||
|             android:layout_marginLeft="16dp" | ||||
|             android:layout_marginEnd="16dp" | ||||
|             app:layout_constraintRight_toRightOf="parent" | ||||
|             android:layout_marginRight="16dp" /> | ||||
|  | ||||
|         <EditText | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="0dp" | ||||
|             android:inputType="textMultiLine" | ||||
|             android:ems="10" | ||||
|             android:id="@+id/galleries_box" | ||||
|             android:layout_marginTop="8dp" | ||||
|             app:layout_constraintTop_toBottomOf="@+id/textView" | ||||
|             app:layout_constraintLeft_toLeftOf="@+id/textView" | ||||
|             app:layout_constraintRight_toRightOf="@+id/textView" | ||||
|             app:layout_constraintBottom_toTopOf="@+id/btn_add_galleries" | ||||
|             android:layout_marginBottom="8dp" | ||||
|             android:hint="Example:\n\nhttp://e-hentai.org/g/12345/1a2b3c4e\nhttp://e-hentai.org/g/67890/6f7g8h9i\nhttp://exhentai.org/g/13579/1a3b5c7e\nhttp://exhentai.org/g/24680/2f4g6h8i\n" /> | ||||
|  | ||||
|         <Button | ||||
|             android:text="Add Galleries" | ||||
|             android:layout_width="0dp" | ||||
|             android:layout_height="wrap_content" | ||||
|             android:id="@+id/btn_add_galleries" | ||||
|             app:layout_constraintBottom_toBottomOf="parent" | ||||
|             android:layout_marginBottom="16dp" | ||||
|             app:layout_constraintLeft_toLeftOf="@+id/galleries_box" | ||||
|             app:layout_constraintRight_toRightOf="@+id/galleries_box" /> | ||||
|     </android.support.constraint.ConstraintLayout> | ||||
|  | ||||
| </android.support.design.widget.CoordinatorLayout> | ||||
| @@ -23,6 +23,10 @@ | ||||
|             android:id="@+id/nav_drawer_latest_updates" | ||||
|             android:icon="@drawable/ic_watch_later_black_24dp" | ||||
|             android:title="@string/label_latest_updates" /> | ||||
|         <item | ||||
|             android:id="@+id/nav_drawer_batch_add" | ||||
|             android:icon="@drawable/ic_playlist_add_black_24dp" | ||||
|             android:title="Batch Add" /> | ||||
|         <item | ||||
|             android:id="@+id/nav_drawer_downloads" | ||||
|             android:icon="@drawable/ic_file_download_black_24dp" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user