Quellcode durchsuchen

Minor cleanup, remove some usages of ArrayList

arkon vor 4 Jahren
Ursprung
Commit
bf5065d16b

+ 5 - 7
app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupManager.kt

@@ -350,7 +350,7 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
      */
     internal fun restoreCategoriesForManga(manga: Manga, categories: List<String>) {
         val dbCategories = databaseHelper.getCategories().executeAsBlocking()
-        val mangaCategoriesToUpdate = ArrayList<MangaCategory>()
+        val mangaCategoriesToUpdate = mutableListOf<MangaCategory>()
         for (backupCategoryStr in categories) {
             for (dbCategory in dbCategories) {
                 if (backupCategoryStr.toLowerCase() == dbCategory.nameLower) {
@@ -362,9 +362,7 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
 
         // Update database
         if (mangaCategoriesToUpdate.isNotEmpty()) {
-            val mangaAsList = ArrayList<Manga>()
-            mangaAsList.add(manga)
-            databaseHelper.deleteOldMangasCategories(mangaAsList).executeAsBlocking()
+            databaseHelper.deleteOldMangasCategories(listOf(manga)).executeAsBlocking()
             databaseHelper.insertMangasCategories(mangaCategoriesToUpdate).executeAsBlocking()
         }
     }
@@ -376,7 +374,7 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
      */
     internal fun restoreHistoryForManga(history: List<DHistory>) {
         // List containing history to be updated
-        val historyToBeUpdated = ArrayList<History>()
+        val historyToBeUpdated = mutableListOf<History>()
         for ((url, lastRead) in history) {
             val dbHistory = databaseHelper.getHistoryByChapterUrl(url).executeAsBlocking()
             // Check if history already in database and update
@@ -410,9 +408,9 @@ class BackupManager(val context: Context, version: Int = CURRENT_VERSION) {
 
         // Get tracks from database
         val dbTracks = databaseHelper.getTracks(manga).executeAsBlocking()
-        val trackToUpdate = ArrayList<Track>()
+        val trackToUpdate = mutableListOf<Track>()
 
-        for (track in tracks) {
+        tracks.forEach { track ->
             val service = trackManager.getService(track.sync_id)
             if (service != null && service.isLogged) {
                 var isInDatabase = false

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt

@@ -207,7 +207,7 @@ class DownloadCache(
     fun removeChapters(chapters: List<Chapter>, manga: Manga) {
         val sourceDir = rootDir.files[manga.source] ?: return
         val mangaDir = sourceDir.files[provider.getMangaDirName(manga)] ?: return
-        for (chapter in chapters) {
+        chapters.forEach { chapter ->
             val chapterDirName = provider.getChapterDirName(chapter)
             if (chapterDirName in mangaDir.files) {
                 mangaDir.files -= chapterDirName

+ 2 - 3
app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt

@@ -28,7 +28,6 @@ import eu.kanade.tachiyomi.util.storage.getUriCompat
 import eu.kanade.tachiyomi.util.system.acquireWakeLock
 import eu.kanade.tachiyomi.util.system.isServiceRunning
 import java.io.File
-import java.util.ArrayList
 import java.util.concurrent.atomic.AtomicInteger
 import rx.Observable
 import rx.Subscription
@@ -254,9 +253,9 @@ class LibraryUpdateService(
         // Initialize the variables holding the progress of the updates.
         val count = AtomicInteger(0)
         // List containing new updates
-        val newUpdates = ArrayList<Pair<LibraryManga, Array<Chapter>>>()
+        val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>()
         // List containing failed updates
-        val failedUpdates = ArrayList<Pair<Manga, String?>>()
+        val failedUpdates = mutableListOf<Pair<Manga, String?>>()
         // Boolean to determine if DownloadManager has downloads
         var hasDownloads = false
 

+ 1 - 3
app/src/main/java/eu/kanade/tachiyomi/network/AndroidCookieJar.kt

@@ -12,9 +12,7 @@ class AndroidCookieJar : CookieJar {
     override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
         val urlString = url.toString()
 
-        for (cookie in cookies) {
-            manager.setCookie(urlString, cookie.toString())
-        }
+        cookies.forEach { manager.setCookie(urlString, it.toString()) }
     }
 
     override fun loadForRequest(url: HttpUrl): List<Cookie> {

+ 3 - 3
app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionPreferencesController.kt

@@ -78,15 +78,15 @@ class ExtensionPreferencesController(bundle: Bundle? = null) :
 
         val multiSource = extension.sources.size > 1
 
-        for (source in extension.sources) {
-            if (source is ConfigurableSource) {
+        extension.sources
+            .filterIsInstance<ConfigurableSource>()
+            .forEach { source ->
                 try {
                     addPreferencesForSource(screen, source, multiSource)
                 } catch (e: AbstractMethodError) {
                     Timber.e("Source did not implement [addPreferencesForSource]: ${source.name}")
                 }
             }
-        }
 
         manager.setPreferences(screen)
 

+ 1 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt

@@ -14,7 +14,6 @@ import eu.kanade.tachiyomi.databinding.DownloadControllerBinding
 import eu.kanade.tachiyomi.source.model.Page
 import eu.kanade.tachiyomi.ui.base.controller.NucleusController
 import eu.kanade.tachiyomi.ui.main.offsetAppbarHeight
-import java.util.HashMap
 import java.util.concurrent.TimeUnit
 import kotlinx.coroutines.flow.launchIn
 import kotlinx.coroutines.flow.onEach
@@ -39,7 +38,7 @@ class DownloadController :
     /**
      * Map of subscriptions for active downloads.
      */
-    private val progressSubscriptions by lazy { HashMap<Download, Subscription>() }
+    private val progressSubscriptions by lazy { mutableMapOf<Download, Subscription>() }
 
     /**
      * Whether the download queue is running or not.

+ 1 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt

@@ -22,7 +22,6 @@ import eu.kanade.tachiyomi.util.lang.isNullOrUnsubscribed
 import eu.kanade.tachiyomi.util.lang.launchIO
 import eu.kanade.tachiyomi.util.removeCovers
 import eu.kanade.tachiyomi.util.updateCoverLastModified
-import java.util.ArrayList
 import java.util.Collections
 import java.util.Comparator
 import rx.Observable
@@ -348,7 +347,7 @@ class LibraryPresenter(
      * @param mangas the list of manga to move.
      */
     fun moveMangasToCategories(categories: List<Category>, mangas: List<Manga>) {
-        val mc = ArrayList<MangaCategory>()
+        val mc = mutableListOf<MangaCategory>()
 
         for (manga in mangas) {
             for (cat in categories) {

+ 7 - 11
app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt

@@ -24,9 +24,6 @@ import timber.log.Timber
 import uy.kohesive.injekt.Injekt
 import uy.kohesive.injekt.api.get
 
-/**
- * Presenter of [ChaptersController].
- */
 class ChaptersPresenter(
     val manga: Manga,
     val source: Source,
@@ -47,8 +44,9 @@ class ChaptersPresenter(
     /**
      * Subject of list of chapters to allow updating the view without going to DB.
      */
-    val chaptersRelay: PublishRelay<List<ChapterItem>>
-    by lazy { PublishRelay.create<List<ChapterItem>>() }
+    private val chaptersRelay: PublishRelay<List<ChapterItem>> by lazy {
+        PublishRelay.create<List<ChapterItem>>()
+    }
 
     /**
      * Whether the chapter list has been requested to the source.
@@ -144,11 +142,9 @@ class ChaptersPresenter(
      * @param chapters the list of chapter from the database.
      */
     private fun setDownloadedChapters(chapters: List<ChapterItem>) {
-        for (chapter in chapters) {
-            if (downloadManager.isChapterDownloaded(chapter, manga)) {
-                chapter.status = Download.DOWNLOADED
-            }
-        }
+        chapters
+            .filter { downloadManager.isChapterDownloaded(it, manga) }
+            .forEach { it.status = Download.DOWNLOADED }
     }
 
     /**
@@ -222,7 +218,7 @@ class ChaptersPresenter(
      * Called when a download for the active manga changes status.
      * @param download the download whose status changed.
      */
-    fun onDownloadStatusChange(download: Download) {
+    private fun onDownloadStatusChange(download: Download) {
         // Assign the download to the model object.
         if (download.status == Download.QUEUE) {
             chapters.find { it.id == download.chapter.id }?.let {

+ 1 - 2
app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSearchAdapter.kt

@@ -10,7 +10,6 @@ import eu.kanade.tachiyomi.data.glide.GlideApp
 import eu.kanade.tachiyomi.data.track.model.TrackSearch
 import eu.kanade.tachiyomi.util.view.gone
 import eu.kanade.tachiyomi.util.view.inflate
-import java.util.ArrayList
 import kotlinx.android.synthetic.main.track_search_item.view.track_search_cover
 import kotlinx.android.synthetic.main.track_search_item.view.track_search_start
 import kotlinx.android.synthetic.main.track_search_item.view.track_search_start_result
@@ -22,7 +21,7 @@ import kotlinx.android.synthetic.main.track_search_item.view.track_search_type
 import kotlinx.android.synthetic.main.track_search_item.view.track_search_type_result
 
 class TrackSearchAdapter(context: Context) :
-    ArrayAdapter<TrackSearch>(context, R.layout.track_search_item, ArrayList<TrackSearch>()) {
+    ArrayAdapter<TrackSearch>(context, R.layout.track_search_item, mutableListOf<TrackSearch>()) {
 
     override fun getView(position: Int, view: View?, parent: ViewGroup): View {
         var v = view

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/util/storage/EpubFile.kt

@@ -150,7 +150,7 @@ class EpubFile(file: File) : Closeable {
      * Returns all the images contained in every page from the epub.
      */
     private fun getImagesFromPages(pages: List<String>, packageHref: String): List<String> {
-        val result = ArrayList<String>()
+        val result = mutableListOf<String>()
         val basePath = getParentDirectory(packageHref)
         pages.forEach { page ->
             val entryPath = resolveZipPath(basePath, page)

+ 3 - 3
app/src/test/java/eu/kanade/tachiyomi/data/backup/BackupTest.kt

@@ -236,7 +236,7 @@ class BackupTest {
         manga.id = backupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
 
         // Create restore list
-        val chapters = ArrayList<Chapter>()
+        val chapters = mutableListOf<Chapter>()
         for (i in 1..8) {
             val chapter = getSingleChapter("Chapter $i")
             chapter.read = true
@@ -249,7 +249,7 @@ class BackupTest {
 
         // Fetch chapters from upstream
         // Create list
-        val chaptersRemote = ArrayList<Chapter>()
+        val chaptersRemote = mutableListOf<Chapter>()
         (1..10).mapTo(chaptersRemote) { getSingleChapter("Chapter $it") }
         `when`(source.fetchChapterList(manga)).thenReturn(Observable.just(chaptersRemote))
 
@@ -284,7 +284,7 @@ class BackupTest {
 
         val historyJson = getSingleHistory(chapter)
 
-        val historyList = ArrayList<DHistory>()
+        val historyList = mutableListOf<DHistory>()
         historyList.add(historyJson)
 
         // Check parser

+ 2 - 3
app/src/test/java/eu/kanade/tachiyomi/data/library/LibraryUpdateServiceTest.kt

@@ -11,7 +11,6 @@ import eu.kanade.tachiyomi.data.database.models.LibraryManga
 import eu.kanade.tachiyomi.source.SourceManager
 import eu.kanade.tachiyomi.source.model.SChapter
 import eu.kanade.tachiyomi.source.online.HttpSource
-import java.util.ArrayList
 import org.assertj.core.api.Assertions.assertThat
 import org.junit.Before
 import org.junit.Test
@@ -107,7 +106,7 @@ class LibraryUpdateServiceTest {
     }
 
     private fun createChapters(vararg urls: String): List<Chapter> {
-        val list = ArrayList<Chapter>()
+        val list = mutableListOf<Chapter>()
         for (url in urls) {
             val c = Chapter.create()
             c.url = url
@@ -118,7 +117,7 @@ class LibraryUpdateServiceTest {
     }
 
     private fun createManga(vararg urls: String): List<LibraryManga> {
-        val list = ArrayList<LibraryManga>()
+        val list = mutableListOf<LibraryManga>()
         for (url in urls) {
             val m = LibraryManga()
             m.url = url