summaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/topmanga
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2021-02-19 20:03:36 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2021-02-19 20:03:36 +0100
commit83aca7a0d44ede3960fc5752a205a55f63321832 (patch)
tree3f8c599746ce3863c4c341d00ef958bf1d42da2e /app/src/main/java/xyz/adjutor/aniki/topmanga
parent2aaa50c8303018f89da461750d5577902bc87357 (diff)
parent485d3ca989bd4cce1788eae17bcc7587d5ef2c90 (diff)
Merge branch 'release/1.1'v1.1.0
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/topmanga')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt19
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt31
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt70
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaApi.kt11
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaPage.kt93
5 files changed, 224 insertions, 0 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt
new file mode 100644
index 0000000..897e8a2
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt
@@ -0,0 +1,19 @@
+package xyz.adjutor.aniki.topmanga
+
+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: List<TopManga>? = null
+
+ fun getResults(): List<TopManga> {
+ return top!!
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt
new file mode 100644
index 0000000..edf561d
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt
@@ -0,0 +1,31 @@
+package xyz.adjutor.aniki.topmanga
+
+import com.google.gson.annotations.SerializedName
+
+//Content of the top field from the api of top manga
+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/topmanga/TopMangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt
new file mode 100644
index 0000000..18fd7a0
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt
@@ -0,0 +1,70 @@
+@file:Suppress("DEPRECATION")
+
+package xyz.adjutor.aniki.topmanga
+
+import android.annotation.SuppressLint
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.os.AsyncTask
+import android.util.Log
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.ImageView
+import android.widget.TextView
+import androidx.recyclerview.widget.RecyclerView
+import xyz.adjutor.aniki.R
+
+class TopMangaAdapter(val mangaList: List<TopManga>) :
+ RecyclerView.Adapter<TopMangaAdapter.MangaViewHolder>() {
+
+ // Describes an item view and its place within the RecyclerView
+ class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
+ val mangaTitle: TextView = itemView.findViewById(R.id.tv_title)
+ val mangaRank: TextView = itemView.findViewById(R.id.tv_rank)
+ val mangaImage: ImageView = itemView.findViewById(R.id.iv_image)
+
+ }
+
+ // Returns a new ViewHolder
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MangaViewHolder {
+ val view = LayoutInflater.from(parent.context)
+ .inflate(R.layout.item_layout, parent, false)
+
+ return MangaViewHolder(view)
+ }
+
+ // Returns size of data list
+ override fun getItemCount(): Int {
+ return mangaList.size
+ }
+
+ // Displays data at a certain position
+ override fun onBindViewHolder(holder: MangaViewHolder, position: Int) {
+ val currentManga: TopManga = mangaList[position]
+ holder.mangaTitle.text = currentManga.title
+ holder.mangaRank.text = currentManga.rank.toString()
+ DownloadImageFromInternet(holder.mangaImage).execute(currentManga.image_url)
+ }
+
+ @SuppressLint("StaticFieldLeak")
+ private inner class DownloadImageFromInternet(var imageView: ImageView) : AsyncTask<String, Void, Bitmap?>() {
+ override fun doInBackground(vararg urls: String): Bitmap? {
+ val imageURL = urls[0]
+ var image: Bitmap? = null
+ try {
+ val `in` = java.net.URL(imageURL).openStream()
+ image = BitmapFactory.decodeStream(`in`)
+ }
+ catch (e: Exception) {
+ Log.e("Error Message", e.message.toString())
+ e.printStackTrace()
+ }
+ return image
+ }
+ override fun onPostExecute(result: Bitmap?) {
+ imageView.setImageBitmap(result)
+ }
+ }
+}
+
diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaApi.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaApi.kt
new file mode 100644
index 0000000..69c4cdd
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaApi.kt
@@ -0,0 +1,11 @@
+package xyz.adjutor.aniki.topmanga
+
+import retrofit2.http.GET
+import retrofit2.Call
+
+interface TopMangaApi {
+
+ @GET("v3/top/manga")
+ fun getTopMangaData(): Call<RestTopMangaResponse>
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaPage.kt
new file mode 100644
index 0000000..e4cd20d
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaPage.kt
@@ -0,0 +1,93 @@
+package xyz.adjutor.aniki.topmanga
+
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import android.widget.Button
+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
+import xyz.adjutor.aniki.R
+
+class TopMangaPage : Fragment() {
+
+ override fun onCreateView(
+ inflater: LayoutInflater, container: ViewGroup?,
+ savedInstanceState: Bundle?
+ ): View? {
+ // Inflate the layout for this fragment
+ val view = inflater.inflate(R.layout.top_manga_page, container, false)
+
+ makeApiCall(view, base_url)
+
+ return view
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ super.onViewCreated(view, savedInstanceState)
+
+ //button to return to the home page
+ view.findViewById<Button>(R.id.button_home).setOnClickListener {
+ findNavController().navigate(R.id.action_TopMangaPage_to_HomePage)
+ }
+
+ }
+
+ //display the recyclerview
+ fun showList(view: View, mangaList: List<TopManga> ){
+ val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view)
+ recyclerView.setHasFixedSize(true)
+ recyclerView.layoutManager = LinearLayoutManager(view.context)
+ recyclerView.adapter = TopMangaAdapter(mangaList)
+ }
+
+ fun makeApiCall(view: View, 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<RestTopMangaResponse> {
+ override fun onResponse(call: Call<RestTopMangaResponse>, response: Response<RestTopMangaResponse>) {
+ if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty
+ val mangaList = response.body()!!.getResults() //getting the "top" field containing our list of TopMangas
+ showList(view, mangaList) // calling the method in charge of displaying on the recyclerview
+ } else {
+ showError() //a snackbar
+ }
+ }
+
+ override fun onFailure(call: Call<RestTopMangaResponse>, 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/" //the api's base url
+ }
+
+} \ No newline at end of file