2020-04-21 23:37:03 +02:00
|
|
|
package exh.ui.smartsearch
|
2019-07-29 08:12:30 +02:00
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
2020-06-19 21:44:41 +02:00
|
|
|
import eu.kanade.tachiyomi.databinding.EhSmartSearchBinding
|
2019-07-29 08:12:30 +02:00
|
|
|
import eu.kanade.tachiyomi.source.CatalogueSource
|
|
|
|
|
import eu.kanade.tachiyomi.source.SourceManager
|
|
|
|
|
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
|
|
|
|
|
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
|
2020-05-04 00:34:46 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.SourceController
|
|
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceController
|
2019-07-29 08:12:30 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.manga.MangaController
|
2021-01-11 01:36:24 +01:00
|
|
|
import eu.kanade.tachiyomi.util.lang.withUIContext
|
2020-03-31 22:38:40 +02:00
|
|
|
import eu.kanade.tachiyomi.util.system.toast
|
2020-04-04 22:30:05 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
2020-12-26 06:06:52 +01:00
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
|
import kotlinx.coroutines.plus
|
2019-07-29 08:12:30 +02:00
|
|
|
import uy.kohesive.injekt.injectLazy
|
|
|
|
|
|
2020-08-05 01:32:36 +02:00
|
|
|
class SmartSearchController(bundle: Bundle? = null) : NucleusController<EhSmartSearchBinding, SmartSearchPresenter>() {
|
2019-07-29 08:12:30 +02:00
|
|
|
private val sourceManager: SourceManager by injectLazy()
|
|
|
|
|
|
|
|
|
|
private val source = sourceManager.get(bundle?.getLong(ARG_SOURCE_ID, -1) ?: -1) as? CatalogueSource
|
2020-04-17 02:56:52 +02:00
|
|
|
private val smartSearchConfig: SourceController.SmartSearchConfig? = bundle?.getParcelable(
|
|
|
|
|
ARG_SMART_SEARCH_CONFIG
|
|
|
|
|
)
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2020-04-21 03:03:28 +02:00
|
|
|
override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View {
|
2020-06-19 21:44:41 +02:00
|
|
|
binding = EhSmartSearchBinding.inflate(inflater)
|
2020-04-21 03:03:28 +02:00
|
|
|
return binding.root
|
|
|
|
|
}
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2020-11-05 04:10:13 +01:00
|
|
|
override fun getTitle() = source?.name.orEmpty()
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2020-12-26 06:06:52 +01:00
|
|
|
override fun createPresenter() = SmartSearchPresenter(source!!, smartSearchConfig!!)
|
2019-07-29 08:12:30 +02:00
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View) {
|
|
|
|
|
super.onViewCreated(view)
|
|
|
|
|
|
2020-04-21 03:03:28 +02:00
|
|
|
binding.appbar.bringToFront()
|
2019-07-29 08:12:30 +02:00
|
|
|
|
2020-04-04 22:30:05 +02:00
|
|
|
if (source == null || smartSearchConfig == null) {
|
2019-07-29 08:12:30 +02:00
|
|
|
router.popCurrentController()
|
|
|
|
|
applicationContext?.toast("Missing data!")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 06:06:52 +01:00
|
|
|
presenter.smartSearchFlow
|
|
|
|
|
.onEach { results ->
|
|
|
|
|
if (results is SmartSearchPresenter.SearchResults.Found) {
|
|
|
|
|
val transaction = MangaController(results.manga, true, smartSearchConfig).withFadeTransaction()
|
2021-01-11 01:36:24 +01:00
|
|
|
withUIContext {
|
2020-05-24 19:37:43 +02:00
|
|
|
router.replaceTopController(transaction)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-02-24 23:26:08 +01:00
|
|
|
withUIContext {
|
|
|
|
|
if (results is SmartSearchPresenter.SearchResults.NotFound) {
|
|
|
|
|
applicationContext?.toast("Couldn't find the manga in the source!")
|
|
|
|
|
} else {
|
|
|
|
|
applicationContext?.toast("Error performing automatic search!")
|
|
|
|
|
}
|
2020-05-24 19:37:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val transaction = BrowseSourceController(source, smartSearchConfig.origTitle, smartSearchConfig).withFadeTransaction()
|
2021-01-11 01:36:24 +01:00
|
|
|
withUIContext {
|
2020-05-24 19:37:43 +02:00
|
|
|
router.replaceTopController(transaction)
|
2019-07-29 08:12:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-07 15:19:00 +01:00
|
|
|
.launchIn(viewScope + Dispatchers.IO)
|
2019-07-29 19:27:33 +02:00
|
|
|
}
|
2019-07-29 08:12:30 +02:00
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
const val ARG_SOURCE_ID = "SOURCE_ID"
|
|
|
|
|
const val ARG_SMART_SEARCH_CONFIG = "SMART_SEARCH_CONFIG"
|
|
|
|
|
}
|
2020-04-04 22:30:05 +02:00
|
|
|
}
|