aboutsummaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2021-02-19 19:46:42 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2021-02-19 19:46:42 +0100
commit00ac626b27076397b79bcd4bdd934ab861ff05a5 (patch)
tree3f8c599746ce3863c4c341d00ef958bf1d42da2e /app/src/main
parentc8a84acbec91678c14e23d8dfed20f26d9d0361e (diff)
Implementing TopAnimefeature/top_anime_handling
Copied the topmanga folder and transformed everything in it into top anime.
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/HomePage.kt4
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt19
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt31
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt72
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeApi.kt11
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimePage.kt93
-rw-r--r--app/src/main/res/layout/home_page.xml11
-rw-r--r--app/src/main/res/layout/top_anime_page.xml31
-rw-r--r--app/src/main/res/navigation/nav_graph.xml14
-rw-r--r--app/src/main/res/values/strings.xml2
10 files changed, 288 insertions, 0 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/HomePage.kt b/app/src/main/java/xyz/adjutor/aniki/HomePage.kt
index b6d45c3..59d7c62 100644
--- a/app/src/main/java/xyz/adjutor/aniki/HomePage.kt
+++ b/app/src/main/java/xyz/adjutor/aniki/HomePage.kt
@@ -20,8 +20,12 @@ class HomePage : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
+
view.findViewById<Button>(R.id.button_top_manga).setOnClickListener {
findNavController().navigate(R.id.action_HomePage_to_TopMangaPage)
}
+ view.findViewById<Button>(R.id.button_top_anime).setOnClickListener {
+ findNavController().navigate(R.id.action_HomePage_to_TopAnimePage)
+ }
}
} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt
new file mode 100644
index 0000000..c711631
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt
@@ -0,0 +1,19 @@
+package xyz.adjutor.aniki.topanime
+
+import com.google.gson.annotations.SerializedName
+
+class RestTopAnimeResponse {
+
+ @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<TopAnime>? = null
+
+ fun getResults(): List<TopAnime> {
+ return top!!
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt
new file mode 100644
index 0000000..a428841
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt
@@ -0,0 +1,31 @@
+package xyz.adjutor.aniki.topanime
+
+import com.google.gson.annotations.SerializedName
+
+//Content of the top field from the api of top manga
+class TopAnime{
+
+ @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("episodes")
+ 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/topanime/TopAnimeAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt
new file mode 100644
index 0000000..a036c15
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt
@@ -0,0 +1,72 @@
+@file:Suppress("DEPRECATION")
+
+package xyz.adjutor.aniki.topanime
+
+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
+import xyz.adjutor.aniki.topanime.TopAnime
+import xyz.adjutor.aniki.topanime.TopAnimeAdapter
+
+class TopAnimeAdapter(val animeList: List<TopAnime>) :
+ RecyclerView.Adapter<TopAnimeAdapter.AnimeViewHolder>() {
+
+ // Describes an item view and its place within the RecyclerView
+ class AnimeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
+ val animeTitle: TextView = itemView.findViewById(R.id.tv_title)
+ val animeRank: TextView = itemView.findViewById(R.id.tv_rank)
+ val animeImage: ImageView = itemView.findViewById(R.id.iv_image)
+
+ }
+
+ // Returns a new ViewHolder
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnimeViewHolder {
+ val view = LayoutInflater.from(parent.context)
+ .inflate(R.layout.item_layout, parent, false)
+
+ return AnimeViewHolder(view)
+ }
+
+ // Returns size of data list
+ override fun getItemCount(): Int {
+ return animeList.size
+ }
+
+ // Displays data at a certain position
+ override fun onBindViewHolder(holder: AnimeViewHolder, position: Int) {
+ val currentAnime: TopAnime = animeList[position]
+ holder.animeTitle.text = currentAnime.title
+ holder.animeRank.text = currentAnime.rank.toString()
+ DownloadImageFromInternet(holder.animeImage).execute(currentAnime.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/topanime/TopAnimeApi.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeApi.kt
new file mode 100644
index 0000000..6d8669c
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeApi.kt
@@ -0,0 +1,11 @@
+package xyz.adjutor.aniki.topanime
+
+import retrofit2.http.GET
+import retrofit2.Call
+
+interface TopAnimeApi {
+
+ @GET("v3/top/anime")
+ fun getTopAnimeData(): Call<RestTopAnimeResponse>
+
+} \ No newline at end of file
diff --git a/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimePage.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimePage.kt
new file mode 100644
index 0000000..9e86191
--- /dev/null
+++ b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimePage.kt
@@ -0,0 +1,93 @@
+package xyz.adjutor.aniki.topanime
+
+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 TopAnimePage : Fragment() {
+
+ override fun onCreateView(
+ inflater: LayoutInflater, container: ViewGroup?,
+ savedInstanceState: Bundle?
+ ): View? {
+ // Inflate the layout for this fragment
+ val view = inflater.inflate(R.layout.top_anime_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_TopAnimePage_to_HomePage)
+ }
+
+ }
+
+ //display the recyclerview
+ fun showList(view: View, animeList: List<TopAnime> ){
+ val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view)
+ recyclerView.setHasFixedSize(true)
+ recyclerView.layoutManager = LinearLayoutManager(view.context)
+ recyclerView.adapter = TopAnimeAdapter(animeList)
+ }
+
+ 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(TopAnimeApi::class.java)
+ val call = service.getTopAnimeData()
+
+ call.enqueue(object : Callback<RestTopAnimeResponse> {
+ override fun onResponse(call: Call<RestTopAnimeResponse>, response: Response<RestTopAnimeResponse>) {
+ if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty
+ val animeList = response.body()!!.getResults() //getting the "top" field containing our list of TopAnimes
+ showList(view, animeList) // calling the method in charge of displaying on the recyclerview
+ } else {
+ showError() //a snackbar
+ }
+ }
+
+ override fun onFailure(call: Call<RestTopAnimeResponse>, 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
diff --git a/app/src/main/res/layout/home_page.xml b/app/src/main/res/layout/home_page.xml
index 68d9f81..564d7da 100644
--- a/app/src/main/res/layout/home_page.xml
+++ b/app/src/main/res/layout/home_page.xml
@@ -30,6 +30,17 @@
app:layout_constraintTop_toBottomOf="@id/tv_title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintEnd_toStartOf="@id/button_top_anime"
+ />
+
+ <Button
+ android:id="@+id/button_top_anime"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/top_anime"
+ app:layout_constraintTop_toBottomOf="@id/tv_title"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintStart_toEndOf="@id/button_top_manga"
app:layout_constraintEnd_toEndOf="parent"
/>
diff --git a/app/src/main/res/layout/top_anime_page.xml b/app/src/main/res/layout/top_anime_page.xml
new file mode 100644
index 0000000..4b6a58f
--- /dev/null
+++ b/app/src/main/res/layout/top_anime_page.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ tools:context=".topanime.TopAnimePage"
+ android:background="@color/very_dark_purple">
+
+ <androidx.recyclerview.widget.RecyclerView
+ android:id="@+id/recycler_view"
+ android:layout_width="0dp"
+ android:layout_height="0dp"
+ app:layout_constraintBottom_toTopOf="@+id/button_home"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toTopOf="parent"
+ tools:listitem="@layout/item_layout" />
+
+ <Button
+ android:id="@+id/button_home"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/home"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ />
+
+</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file
diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml
index 4a0cfa2..132de49 100644
--- a/app/src/main/res/navigation/nav_graph.xml
+++ b/app/src/main/res/navigation/nav_graph.xml
@@ -14,6 +14,10 @@
<action
android:id="@+id/action_HomePage_to_TopMangaPage"
app:destination="@id/TopMangaPage" />
+ <action
+ android:id="@+id/action_HomePage_to_TopAnimePage"
+ app:destination="@id/TopAnimePage" />
+
</fragment>
<fragment
@@ -26,5 +30,15 @@
android:id="@+id/action_TopMangaPage_to_HomePage"
app:destination="@id/HomePage" />
</fragment>
+ <fragment
+ android:id="@+id/TopAnimePage"
+ android:name="xyz.adjutor.aniki.topanime.TopAnimePage"
+ android:label="@string/top_anime_page_label"
+ tools:layout="@layout/top_anime_page">
+
+ <action
+ android:id="@+id/action_TopAnimePage_to_HomePage"
+ app:destination="@id/HomePage" />
+ </fragment>
</navigation> \ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 5301008..403a883 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -16,6 +16,8 @@
<string name="rv_rank">0</string>
<string name="rv_image">image</string>
<string name="manga_image_content_description">Image of manga</string>
+ <string name="top_anime_page_label">Top Anime Page</string>
+ <string name="top_anime">Top Anime</string>
<string-array name="manga_array">
<item>Dragon Ball</item>