From 00ac626b27076397b79bcd4bdd934ab861ff05a5 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 19 Feb 2021 19:46:42 +0100 Subject: Implementing TopAnime Copied the topmanga folder and transformed everything in it into top anime. --- .../adjutor/aniki/topanime/RestTopAnimeResponse.kt | 19 +++++ .../java/xyz/adjutor/aniki/topanime/TopAnime.kt | 31 ++++++++ .../xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt | 72 +++++++++++++++++ .../java/xyz/adjutor/aniki/topanime/TopAnimeApi.kt | 11 +++ .../xyz/adjutor/aniki/topanime/TopAnimePage.kt | 93 ++++++++++++++++++++++ 5 files changed, 226 insertions(+) create mode 100644 app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeApi.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimePage.kt (limited to 'app/src/main/java/xyz/adjutor/aniki/topanime') 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? = null + + fun getResults(): List { + 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) : + RecyclerView.Adapter() { + + // 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() { + 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 + +} \ 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