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
|
2021-12-13 02:40:11 +01:00
|
|
|
import eu.kanade.tachiyomi.R
|
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
|
2022-05-15 23:03:57 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.SourcesController
|
2020-05-04 00:34:46 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourceController
|
2019-07-29 08:12:30 +02:00
|
|
|
import eu.kanade.tachiyomi.ui.manga.MangaController
|
2020-03-31 22:38:40 +02:00
|
|
|
import eu.kanade.tachiyomi.util.system.toast
|
2020-12-26 06:06:52 +01:00
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
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
|
2022-05-15 23:03:57 +02:00
|
|
|
private val smartSearchConfig: SourcesController.SmartSearchConfig? = bundle?.getParcelable(
|
2022-04-08 21:30:30 +02:00
|
|
|
ARG_SMART_SEARCH_CONFIG,
|
2020-04-17 02:56:52 +02:00
|
|
|
)
|
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
|
|
|
|
2021-04-18 18:54:51 +02:00
|
|
|
override fun createBinding(inflater: LayoutInflater) = EhSmartSearchBinding.inflate(inflater)
|
|
|
|
|
|
2019-07-29 08:12:30 +02:00
|
|
|
override fun onViewCreated(view: View) {
|
|
|
|
|
super.onViewCreated(view)
|
|
|
|
|
|
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) {
|
2022-06-19 23:32:31 +02:00
|
|
|
val transaction = MangaController(results.manga.id!!, true, smartSearchConfig).withFadeTransaction()
|
2021-12-12 23:59:29 +01:00
|
|
|
router.replaceTopController(transaction)
|
2020-05-24 19:37:43 +02:00
|
|
|
} else {
|
2021-12-12 23:59:29 +01:00
|
|
|
if (results is SmartSearchPresenter.SearchResults.NotFound) {
|
2021-12-13 02:40:11 +01:00
|
|
|
applicationContext?.toast(R.string.could_not_find_manga)
|
2021-12-12 23:59:29 +01:00
|
|
|
} else {
|
2021-12-13 02:40:11 +01:00
|
|
|
applicationContext?.toast(R.string.automatic_search_error)
|
2019-07-29 08:12:30 +02:00
|
|
|
}
|
2021-12-12 23:59:29 +01:00
|
|
|
val transaction = BrowseSourceController(
|
|
|
|
|
source,
|
|
|
|
|
smartSearchConfig.origTitle,
|
2022-04-08 21:30:30 +02:00
|
|
|
smartSearchConfig,
|
2021-12-12 23:59:29 +01:00
|
|
|
).withFadeTransaction()
|
|
|
|
|
router.replaceTopController(transaction)
|
2019-07-29 08:12:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-12 23:59:29 +01:00
|
|
|
.launchIn(viewScope)
|
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
|
|
|
}
|