package xyz.adjutor.aniki.presentation.controller.manga import com.google.gson.Gson import com.google.gson.GsonBuilder import retrofit2.Call import retrofit2.Callback import retrofit2.Response import xyz.adjutor.aniki.presentation.Singletons import xyz.adjutor.aniki.presentation.model.manga.SearchManga import xyz.adjutor.aniki.presentation.model.manga.SearchMangaResponse import xyz.adjutor.aniki.presentation.view.manga.SearchMangaPage class SearchMangaController { lateinit var gson: Gson lateinit var baseUrl: String //the api's base url lateinit var view: SearchMangaPage fun onStart(searchMangaPage: SearchMangaPage) { view = searchMangaPage baseUrl = "https://api.jikan.moe/" //the api's base url gson = GsonBuilder() .setLenient() .create() } //call the API and show the list private fun makeApiCall(view: SearchMangaPage, BASE_URL: String, query: String) { Singletons .searchMangaApi .getSearchMangaData(q = query) .enqueue(object : Callback { override fun onResponse( call: Call, response: Response ) { if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty val mangaList: List = response.body()!! .getResults() //getting the "search" field containing our list of SearchMangas view.showList( view.requireView(), mangaList ) //calling the method in charge of displaying on the recyclerview } else { view.showError() //a snackbar } } override fun onFailure(call: Call, t: Throwable) { view.showError() } }) } fun updateList(userInput: String) { makeApiCall(view, baseUrl, userInput) } }