From f4d8a9c6fa8f805a9dedb626ef343aa37943c079 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Thu, 18 Feb 2021 19:42:15 +0100 Subject: Implementing API calls for the top mangas. Test and Debug with snackbar : ok Adding internet access. Removing useless News class. --- app/src/main/java/xyz/adjutor/aniki/News.kt | 7 --- .../java/xyz/adjutor/aniki/RestTopMangaResponse.kt | 15 +++++++ app/src/main/java/xyz/adjutor/aniki/TopManga.kt | 30 +++++++++++++ app/src/main/java/xyz/adjutor/aniki/TopMangaApi.kt | 11 +++++ .../main/java/xyz/adjutor/aniki/TopMangaPage.kt | 51 ++++++++++++++++++++++ 5 files changed, 107 insertions(+), 7 deletions(-) delete mode 100644 app/src/main/java/xyz/adjutor/aniki/News.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/TopManga.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/TopMangaApi.kt (limited to 'app/src/main/java/xyz/adjutor') diff --git a/app/src/main/java/xyz/adjutor/aniki/News.kt b/app/src/main/java/xyz/adjutor/aniki/News.kt deleted file mode 100644 index d38a556..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/News.kt +++ /dev/null @@ -1,7 +0,0 @@ -package xyz.adjutor.aniki - -data class News ( - val id: Int, - val title: String, - val description: String -) \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt new file mode 100644 index 0000000..8cd5f2b --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/RestTopMangaResponse.kt @@ -0,0 +1,15 @@ +package xyz.adjutor.aniki + +import com.google.gson.annotations.SerializedName + +class RestTopMangaResponse { + + @SerializedName("request_hash") + var request_hash: String? = null + @SerializedName("request_cached") + var request_cached: Boolean? = null + @SerializedName("request_cached_expiry") + var request_cached_expiry: Int? = null + @SerializedName("top") + var top = ArrayList() +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/TopManga.kt b/app/src/main/java/xyz/adjutor/aniki/TopManga.kt new file mode 100644 index 0000000..acedf56 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/TopManga.kt @@ -0,0 +1,30 @@ +package xyz.adjutor.aniki + +import com.google.gson.annotations.SerializedName + +class TopManga{ + + @SerializedName("mal_id") + var mal_id: Int? = null + @SerializedName("rank") + var rank: Int? = null + @SerializedName("title") + var title: String? = null + @SerializedName("url") + var url: String? = null + @SerializedName("type") + var type: String? = null + @SerializedName("volumes") + var volumes: Int? = null + @SerializedName("start_date") + var start_date: String? = null + @SerializedName("end_date") + var end_date: String? = null + @SerializedName("members") + var members: Int? = null + @SerializedName("score") + var score: Float? = null + @SerializedName("image_url") + var image_url: String? = null + +} diff --git a/app/src/main/java/xyz/adjutor/aniki/TopMangaApi.kt b/app/src/main/java/xyz/adjutor/aniki/TopMangaApi.kt new file mode 100644 index 0000000..b760233 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/TopMangaApi.kt @@ -0,0 +1,11 @@ +package xyz.adjutor.aniki + +import retrofit2.http.GET +import retrofit2.Call + +interface TopMangaApi { + + @GET("v3/top/manga") + fun getTopMangaData(): Call + +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt index ac585c7..9496053 100644 --- a/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/TopMangaPage.kt @@ -9,6 +9,13 @@ import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.snackbar.Snackbar +import com.google.gson.GsonBuilder +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory class TopMangaPage : Fragment() { @@ -22,6 +29,10 @@ class TopMangaPage : Fragment() { // Retrieves data from mangasource val mangaList = MangaSource(view.context).getMangaList() + + makeApiCall(base_url) + + val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) recyclerView.setHasFixedSize(true) recyclerView.layoutManager = LinearLayoutManager(view.context) @@ -37,4 +48,44 @@ class TopMangaPage : Fragment() { } } + + fun makeApiCall(BASE_URL: String) { + val gson = GsonBuilder() + .setLenient() + .create() + val retrofit = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + val service = retrofit.create(TopMangaApi::class.java) + val call = service.getTopMangaData() + call.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful && response.body() != null){ + val topMangaResponse = response.body() + Snackbar.make(requireView(), "HA? YOU THOUGHT IT WAS AN ERROR !? BUT IT WAS I, API !", Snackbar.LENGTH_LONG) + .setAction("Action", null).show() + } else { + showError() + } + } + + override fun onFailure(call: Call, t: Throwable) { + showError() + } + + + } + ) + } + + private fun showError() { + Snackbar.make(requireView(), "HA? YOU THOUGHT IT WAS AN API !? BUT IT WAS I, ERROR !", Snackbar.LENGTH_LONG) + .setAction("Action", null).show() + } + + companion object { + var base_url = "https://api.jikan.moe/" + } + } \ No newline at end of file -- cgit v1.2.3