package eu.kanade.tachiyomi.extension.ru.rumix import android.app.Application import android.content.SharedPreferences import android.widget.Toast import androidx.preference.EditTextPreference import eu.kanade.tachiyomi.multisrc.grouple.GroupLe import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.Request import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get class RuMIX : GroupLe("RuMIX", "https://rumix.me", "ru") { private val preferences: SharedPreferences by lazy { Injekt.get().getSharedPreferences("source_$id", 0x0000) } private var domain: String = preferences.getString(DOMAIN_TITLE, DOMAIN_DEFAULT)!! override val baseUrl: String = domain override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { val url = super.searchMangaRequest(page, query, filters).url.newBuilder() (if (filters.isEmpty()) getFilterList().reversed() else filters.reversed()).forEach { filter -> when (filter) { is OrderBy -> { if (url.toString().contains("&") && filter.state < 6) { url.addQueryParameter("sortType", arrayOf("RATING", "POPULARITY", "YEAR", "NAME", "DATE_CREATE", "DATE_UPDATE")[filter.state]) } else { val ord = arrayOf("rate", "popularity", "year", "name", "created", "updated", "votes")[filter.state] val ordUrl = "$baseUrl/list?sortType=$ord&offset=${70 * (page - 1)}".toHttpUrlOrNull()!!.newBuilder() return GET(ordUrl.toString(), headers) } } else -> return@forEach } } return if (url.toString().contains("&")) { GET(url.toString().replace("=%3D", "="), headers) } else { popularMangaRequest(page) } } private class OrderBy : Filter.Select( "Сортировка", arrayOf("По популярности", "Популярно сейчас", "По году", "По имени", "Новинки", "По дате обновления", "По рейтингу"), ) override fun getFilterList() = FilterList( OrderBy(), ) override fun setupPreferenceScreen(screen: androidx.preference.PreferenceScreen) { super.setupPreferenceScreen(screen) EditTextPreference(screen.context).apply { key = DOMAIN_TITLE this.title = DOMAIN_TITLE summary = domain this.setDefaultValue(DOMAIN_DEFAULT) dialogTitle = DOMAIN_TITLE setOnPreferenceChangeListener { _, newValue -> try { val res = preferences.edit().putString(DOMAIN_TITLE, newValue as String).commit() Toast.makeText(screen.context, "Для смены домена необходимо перезапустить приложение с полной остановкой.", Toast.LENGTH_LONG).show() res } catch (e: Exception) { e.printStackTrace() false } } }.let(screen::addPreference) } companion object { private const val DOMAIN_TITLE = "Домен" private const val DOMAIN_DEFAULT = "https://rumix.me" } }