diff options
author | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-02-19 19:46:42 +0100 |
---|---|---|
committer | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-02-19 19:46:42 +0100 |
commit | 00ac626b27076397b79bcd4bdd934ab861ff05a5 (patch) | |
tree | 3f8c599746ce3863c4c341d00ef958bf1d42da2e /app/src/main/java/xyz/adjutor | |
parent | c8a84acbec91678c14e23d8dfed20f26d9d0361e (diff) |
Implementing TopAnimefeature/top_anime_handling
Copied the topmanga folder and transformed everything in it into top anime.
Diffstat (limited to 'app/src/main/java/xyz/adjutor')
6 files changed, 230 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 |