diff options
author | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-03-02 09:13:54 +0100 |
---|---|---|
committer | Clyhtsuriva <aimeric@adjutor.xyz> | 2021-03-02 09:13:54 +0100 |
commit | f9f079eea806454d49d6088089511d72e1c28f86 (patch) | |
tree | b0eaf1019c094b9857c94a2e245682ebc976f0b0 /app/src/main/java/xyz | |
parent | 9e1f3503c1a80d7e108f60f9065ae08abb7c2b3f (diff) |
Cleaning up the code.
Diffstat (limited to 'app/src/main/java/xyz')
11 files changed, 169 insertions, 122 deletions
diff --git a/app/src/main/java/xyz/adjutor/aniki/HomePage.kt b/app/src/main/java/xyz/adjutor/aniki/HomePage.kt index 734e917..b7c1d0e 100644 --- a/app/src/main/java/xyz/adjutor/aniki/HomePage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/HomePage.kt @@ -11,8 +11,8 @@ import androidx.navigation.fragment.findNavController class HomePage : Fragment() { override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.home_page, container, false) @@ -20,7 +20,7 @@ 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) } diff --git a/app/src/main/java/xyz/adjutor/aniki/anime/RestAnimeResponse.kt b/app/src/main/java/xyz/adjutor/aniki/anime/RestAnimeResponse.kt index aa15dfd..b449b31 100644 --- a/app/src/main/java/xyz/adjutor/aniki/anime/RestAnimeResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/anime/RestAnimeResponse.kt @@ -2,10 +2,11 @@ package xyz.adjutor.aniki.anime import com.google.gson.annotations.SerializedName -class RestAnimeResponse{ //only kept the infos I didn't have and that were interesting to me. +class RestAnimeResponse { //only kept the infos I didn't have and that were interesting to me. @SerializedName("mal_id") var mal_id: Int? = null + @SerializedName("synopsis") var synopsis: String? = null } diff --git a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/DetailTopAnimeActivity.kt b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/DetailTopAnimeActivity.kt index 0db1ca1..acbbd4d 100644 --- a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/DetailTopAnimeActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/DetailTopAnimeActivity.kt @@ -26,8 +26,8 @@ class DetailTopAnimeActivity : AppCompatActivity() { private var baseUrl = "https://api.jikan.moe/" var sharedPreferences: SharedPreferences? = null private val gson = GsonBuilder() - .setLenient() - .create() + .setLenient() + .create() private val intentAnimeId = "theanimeid" private val intentAnimeTitle = "theanimetitle" @@ -79,7 +79,7 @@ class DetailTopAnimeActivity : AppCompatActivity() { .into(ivImage) //using null as a string because it has been converted to a string before - tvEpisodes.text = if (animeEpisodes != "null"){ + tvEpisodes.text = if (animeEpisodes != "null") { animeEpisodes } else { fieldIsNull() @@ -87,7 +87,7 @@ class DetailTopAnimeActivity : AppCompatActivity() { tvStartDate.text = animeStartDate - tvEndDate.text = if (animeEndDate != "null"){ + tvEndDate.text = if (animeEndDate != "null") { animeEndDate } else { fieldIsNull() @@ -96,7 +96,7 @@ class DetailTopAnimeActivity : AppCompatActivity() { tvUrl.text = animeUrl val anime: RestAnimeResponse? = getDataFromCache(animeId.toString()) - if(anime != null ){ + if (anime != null) { showDetail(anime) } else { //taking the API's fields I want and displaying them @@ -106,9 +106,9 @@ class DetailTopAnimeActivity : AppCompatActivity() { } private fun getDataFromCache(animeId: String): RestAnimeResponse? { - val jsonAnime: String?= sharedPreferences?.getString(animeId, null) + val jsonAnime: String? = sharedPreferences?.getString(animeId, null) - return if(jsonAnime == null) { + return if (jsonAnime == null) { null } else { val type: Type = object : TypeToken<RestAnimeResponse>() {}.type @@ -119,16 +119,19 @@ class DetailTopAnimeActivity : AppCompatActivity() { private fun makeApiCall(BASE_URL: String, animeId: String) { val retrofit = Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() val service = retrofit.create(AnimeApi::class.java) val call = service.getAnimeData(animeId) //based on the id call.enqueue(object : Callback<RestAnimeResponse> { - override fun onResponse(call: Call<RestAnimeResponse>, response: Response<RestAnimeResponse>) { - if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty + override fun onResponse( + call: Call<RestAnimeResponse>, + response: Response<RestAnimeResponse> + ) { + if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty val anime = response.body() //getting the RestAnimeResponse fields saveList(anime) @@ -157,7 +160,8 @@ class DetailTopAnimeActivity : AppCompatActivity() { fun showError(text: String) { Toast.makeText(this, text, Toast.LENGTH_LONG).show() } - private fun fieldIsNull(): String{ + + private fun fieldIsNull(): String { return "Unknown" } @@ -165,8 +169,8 @@ class DetailTopAnimeActivity : AppCompatActivity() { val jsonString: String = gson.toJson(anime) sharedPreferences - ?.edit() - ?.putString(anime?.mal_id.toString(), jsonString) - ?.apply() + ?.edit() + ?.putString(anime?.mal_id.toString(), jsonString) + ?.apply() } }
\ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnime.kt b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnime.kt index f8093f6..5202227 100644 --- a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnime.kt +++ b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnime.kt @@ -3,24 +3,32 @@ package xyz.adjutor.aniki.anime.topanime import com.google.gson.annotations.SerializedName //Content of the top field from the api of top anime -class TopAnime{ +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("episodes") var episodes: Int? = null + @SerializedName("start_date") var start_date: String? = null + @SerializedName("end_date") var end_date: String? = 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/anime/topanime/TopAnimeAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimeAdapter.kt index bda6ec5..fa1b5ed 100644 --- a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimeAdapter.kt +++ b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimeAdapter.kt @@ -13,7 +13,7 @@ import com.bumptech.glide.request.RequestOptions import xyz.adjutor.aniki.R class TopAnimeAdapter(private val animeList: List<TopAnime>) : - RecyclerView.Adapter<TopAnimeAdapter.AnimeViewHolder>() { + RecyclerView.Adapter<TopAnimeAdapter.AnimeViewHolder>() { // Describes an item view and its place within the RecyclerView class AnimeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { @@ -27,7 +27,7 @@ class TopAnimeAdapter(private val animeList: List<TopAnime>) : // 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) + .inflate(R.layout.item_layout, parent, false) return AnimeViewHolder(view) } @@ -45,10 +45,10 @@ class TopAnimeAdapter(private val animeList: List<TopAnime>) : holder.animeScore.text = currentAnime.score.toString() val image: String = currentAnime.image_url.toString() Glide - .with(holder.itemView.context) - .load(image) - .apply(RequestOptions().override(400)) - .into(holder.animeImage) + .with(holder.itemView.context) + .load(image) + .apply(RequestOptions().override(400)) + .into(holder.animeImage) //when you click on a selected cardview, some datas are sent to the other activity holder.cardview.setOnClickListener { @@ -62,17 +62,18 @@ class TopAnimeAdapter(private val animeList: List<TopAnime>) : val currentAnimeEndDate = "theanimeenddate" val currentAnimeUrl = "theanimeurl" - val intent: Intent = Intent(holder.itemView.context, DetailTopAnimeActivity::class.java).apply { - putExtra(currentAnimeId, currentAnime.mal_id.toString()) - putExtra(currentAnimeTitle, currentAnime.title) - putExtra(currentAnimeRank, currentAnime.rank.toString()) - putExtra(currentAnimeScore, currentAnime.score.toString()) - putExtra(currentAnimeImageUrl, currentAnime.image_url.toString()) - putExtra(currentAnimeEpisodes, currentAnime.episodes.toString()) - putExtra(currentAnimeStartDate, currentAnime.start_date) - putExtra(currentAnimeEndDate, currentAnime.end_date.toString()) - putExtra(currentAnimeUrl, currentAnime.url.toString()) - } + val intent: Intent = + Intent(holder.itemView.context, DetailTopAnimeActivity::class.java).apply { + putExtra(currentAnimeId, currentAnime.mal_id.toString()) + putExtra(currentAnimeTitle, currentAnime.title) + putExtra(currentAnimeRank, currentAnime.rank.toString()) + putExtra(currentAnimeScore, currentAnime.score.toString()) + putExtra(currentAnimeImageUrl, currentAnime.image_url.toString()) + putExtra(currentAnimeEpisodes, currentAnime.episodes.toString()) + putExtra(currentAnimeStartDate, currentAnime.start_date) + putExtra(currentAnimeEndDate, currentAnime.end_date.toString()) + putExtra(currentAnimeUrl, currentAnime.url.toString()) + } holder.itemView.context.startActivity(intent) } } diff --git a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt index efa45dd..ccae2fa 100644 --- a/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt @@ -27,13 +27,13 @@ class TopAnimePage : Fragment() { var sharedPreferences: SharedPreferences? = null val gson = GsonBuilder() - .setLenient() - .create() + .setLenient() + .create() var base_url = "https://api.jikan.moe/" //the api's base url override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment val view = inflater.inflate(R.layout.top_anime_page, container, false) @@ -41,7 +41,7 @@ class TopAnimePage : Fragment() { sharedPreferences = view.context.getSharedPreferences("sp_anime", Context.MODE_PRIVATE) val animeList: List<TopAnime>? = getDataFromCache() - if(animeList != null ){ + if (animeList != null) { showList(view, animeList) } else { makeApiCall(view, base_url) @@ -55,7 +55,7 @@ class TopAnimePage : Fragment() { val jsonAnime: String? = sharedPreferences?.getString("jsonAnimeList", null) //if it's null, well, return null - if(jsonAnime == null) { + if (jsonAnime == null) { return null } else { //else deserialize the list and return it val listType: Type = object : TypeToken<List<TopAnime>>() {}.type @@ -72,12 +72,13 @@ class TopAnimePage : Fragment() { } fun updateList() { - makeApiCall(view,base_url) + makeApiCall(view, base_url) Snackbar.make(requireView(), "Data refreshed", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } + val swipeRefresh: SwipeRefreshLayout = view.findViewById(R.id.swiperefresh) - swipeRefresh.setOnRefreshListener{ + swipeRefresh.setOnRefreshListener { updateList() swipeRefresh.isRefreshing = false } @@ -85,7 +86,7 @@ class TopAnimePage : Fragment() { } //display the recyclerview - fun showList(view: View, animeList: List<TopAnime> ){ + fun showList(view: View, animeList: List<TopAnime>) { val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) recyclerView.setHasFixedSize(true) recyclerView.layoutManager = LinearLayoutManager(view.context) @@ -95,20 +96,27 @@ class TopAnimePage : Fragment() { private fun makeApiCall(view: View, BASE_URL: String) { val retrofit = Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() + .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: List<TopAnime> = response.body()!!.getResults() //getting the "top" field containing our list of TopAnimes + 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: List<TopAnime> = response.body()!! + .getResults() //getting the "top" field containing our list of TopAnimes saveList(animeList) - showList(view, animeList) //calling the method in charge of displaying on the recyclerview + showList( + view, + animeList + ) //calling the method in charge of displaying on the recyclerview } else { showError() //a snackbar @@ -126,14 +134,14 @@ class TopAnimePage : Fragment() { val jsonString: String = gson.toJson(animeList) sharedPreferences - ?.edit() - ?.putString("jsonAnimeList", jsonString) - ?.apply() + ?.edit() + ?.putString("jsonAnimeList", jsonString) + ?.apply() } private fun showError() { Snackbar.make(requireView(), "API ERROR", Snackbar.LENGTH_LONG) - .setAction("Action", null).show() + .setAction("Action", null).show() } }
\ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt index 00b0dce..10df7a5 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt @@ -2,14 +2,17 @@ package xyz.adjutor.aniki.manga import com.google.gson.annotations.SerializedName -class RestMangaResponse{ //only kept the infos I didn't have and that were interesting to me. +class RestMangaResponse { //only kept the infos I didn't have and that were interesting to me. @SerializedName("mal_id") var mal_id: Int? = null + @SerializedName("chapters") var chapters: Int? = null + @SerializedName("synopsis") var synopsis: String? = null + @SerializedName("background") var background: String? = null //a bit of background story about the manga diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/DetailTopMangaActivity.kt index 0d0d4b5..c7b6317 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/DetailTopMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/DetailTopMangaActivity.kt @@ -26,8 +26,8 @@ class DetailTopMangaActivity : AppCompatActivity() { private var baseUrl = "https://api.jikan.moe/" var sharedPreferences: SharedPreferences? = null private val gson = GsonBuilder() - .setLenient() - .create() + .setLenient() + .create() private val intentMangaId = "themangaid" private val intentMangaTitle = "themangatitle" @@ -79,7 +79,7 @@ class DetailTopMangaActivity : AppCompatActivity() { .into(ivImage) //using null as a string because it has been converted to a string before - tvVolumes.text = if (mangaVolumes != "null"){ + tvVolumes.text = if (mangaVolumes != "null") { mangaVolumes } else { fieldIsNull() @@ -87,7 +87,7 @@ class DetailTopMangaActivity : AppCompatActivity() { tvStartDate.text = mangaStartDate - tvEndDate.text = if (mangaEndDate != "null"){ + tvEndDate.text = if (mangaEndDate != "null") { mangaEndDate } else { fieldIsNull() @@ -96,7 +96,7 @@ class DetailTopMangaActivity : AppCompatActivity() { tvUrl.text = mangaUrl val manga: RestMangaResponse? = getDataFromCache(mangaId.toString()) - if(manga != null ){ + if (manga != null) { showDetail(manga) } else { //taking the API's fields I want and displaying them @@ -106,9 +106,9 @@ class DetailTopMangaActivity : AppCompatActivity() { } private fun getDataFromCache(mangaId: String): RestMangaResponse? { - val jsonManga: String?= sharedPreferences?.getString(mangaId, null) + val jsonManga: String? = sharedPreferences?.getString(mangaId, null) - return if(jsonManga == null) { + return if (jsonManga == null) { null } else { val type: Type = object : TypeToken<RestMangaResponse>() {}.type @@ -119,16 +119,19 @@ class DetailTopMangaActivity : AppCompatActivity() { private fun makeApiCall(BASE_URL: String, mangaId: String) { val retrofit = Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() val service = retrofit.create(MangaApi::class.java) val call = service.getMangaData(mangaId) //based on the id call.enqueue(object : Callback<RestMangaResponse> { - override fun onResponse(call: Call<RestMangaResponse>, response: Response<RestMangaResponse>) { - if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty + override fun onResponse( + call: Call<RestMangaResponse>, + response: Response<RestMangaResponse> + ) { + if (response.isSuccessful && response.body() != null) { //if the code returned is >= 200 and < 300 AND the the body ain't empty val manga = response.body() //getting the RestMangaResponse fields saveList(manga) @@ -152,7 +155,7 @@ class DetailTopMangaActivity : AppCompatActivity() { val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) val tvBackground: TextView = findViewById(R.id.tv_background) - tvChapters.text = if (manga.chapters != null){ + tvChapters.text = if (manga.chapters != null) { manga.chapters.toString() } else { fieldIsNull() @@ -160,7 +163,7 @@ class DetailTopMangaActivity : AppCompatActivity() { tvSynopsis.text = manga.synopsis.toString() - tvBackground.text = if (manga.background != null){ + tvBackground.text = if (manga.background != null) { manga.background.toString() } else { fieldIsNull() @@ -171,7 +174,8 @@ class DetailTopMangaActivity : AppCompatActivity() { fun showError(text: String) { Toast.makeText(this, text, Toast.LENGTH_LONG).show() } - private fun fieldIsNull(): String{ + + private fun fieldIsNull(): String { return "Unknown" } @@ -179,8 +183,8 @@ class DetailTopMangaActivity : AppCompatActivity() { val jsonString: String = gson.toJson(manga) sharedPreferences - ?.edit() - ?.putString(manga?.mal_id.toString(), jsonString) - ?.apply() + ?.edit() + ?.putString(manga?.mal_id.toString(), jsonString) + ?.apply() } }
\ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopManga.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopManga.kt index 8d952b3..2ceceea 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopManga.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopManga.kt @@ -3,24 +3,32 @@ package xyz.adjutor.aniki.manga.topmanga import com.google.gson.annotations.SerializedName //Content of the top field from the api of top manga -class TopManga{ +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("volumes") var volumes: Int? = null + @SerializedName("start_date") var start_date: String? = null + @SerializedName("end_date") var end_date: String? = 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/manga/topmanga/TopMangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaAdapter.kt index a259750..626f34e 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaAdapter.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaAdapter.kt @@ -13,7 +13,7 @@ import com.bumptech.glide.request.RequestOptions import xyz.adjutor.aniki.R class TopMangaAdapter(private val mangaList: List<TopManga>) : - RecyclerView.Adapter<TopMangaAdapter.MangaViewHolder>() { + RecyclerView.Adapter<TopMangaAdapter.MangaViewHolder>() { // Describes an item view and its place within the RecyclerView class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { @@ -27,7 +27,7 @@ class TopMangaAdapter(private val mangaList: List<TopManga>) : // 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) + .inflate(R.layout.item_layout, parent, false) return MangaViewHolder(view) } @@ -45,10 +45,10 @@ class TopMangaAdapter(private val mangaList: List<TopManga>) : holder.mangaScore.text = currentManga.score.toString() val image: String = currentManga.image_url.toString() Glide - .with(holder.itemView.context) - .load(image) - .apply(RequestOptions().override(400)) - .into(holder.mangaImage) + .with(holder.itemView.context) + .load(image) + .apply(RequestOptions().override(400)) + .into(holder.mangaImage) //when you click on a selected cardview, some datas are sent to the other activity holder.cardview.setOnClickListener { @@ -62,17 +62,18 @@ class TopMangaAdapter(private val mangaList: List<TopManga>) : val currentMangaEndDate = "themangaenddate" val currentMangaUrl = "themangaurl" - val intent: Intent = Intent(holder.itemView.context, DetailTopMangaActivity::class.java).apply { - putExtra(currentMangaId, currentManga.mal_id.toString()) - putExtra(currentMangaTitle, currentManga.title) - putExtra(currentMangaRank, currentManga.rank.toString()) - putExtra(currentMangaScore, currentManga.score.toString()) - putExtra(currentMangaImageUrl, currentManga.image_url.toString()) - putExtra(currentMangaVolumes, currentManga.volumes.toString()) - putExtra(currentMangaStartDate, currentManga.start_date) - putExtra(currentMangaEndDate, currentManga.end_date.toString()) - putExtra(currentMangaUrl, currentManga.url.toString()) - } + val intent: Intent = + Intent(holder.itemView.context, DetailTopMangaActivity::class.java).apply { + putExtra(currentMangaId, currentManga.mal_id.toString()) + putExtra(currentMangaTitle, currentManga.title) + putExtra(currentMangaRank, currentManga.rank.toString()) + putExtra(currentMangaScore, currentManga.score.toString()) + putExtra(currentMangaImageUrl, currentManga.image_url.toString()) + putExtra(currentMangaVolumes, currentManga.volumes.toString()) + putExtra(currentMangaStartDate, currentManga.start_date) + putExtra(currentMangaEndDate, currentManga.end_date.toString()) + putExtra(currentMangaUrl, currentManga.url.toString()) + } holder.itemView.context.startActivity(intent) } } diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt index d28161a..ce9b1c6 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/topmanga/TopMangaPage.kt @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.google.android.material.snackbar.Snackbar +import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.reflect.TypeToken import retrofit2.Call @@ -26,14 +27,14 @@ import java.lang.reflect.Type class TopMangaPage : Fragment() { var sharedPreferences: SharedPreferences? = null - val gson = GsonBuilder() - .setLenient() - .create() - var base_url = "https://api.jikan.moe/" //the api's base url + val gson: Gson = GsonBuilder() + .setLenient() + .create() + private var baseUrl = "https://api.jikan.moe/" //the api's base url override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment val view = inflater.inflate(R.layout.top_manga_page, container, false) @@ -41,10 +42,10 @@ class TopMangaPage : Fragment() { sharedPreferences = view.context.getSharedPreferences("sp_manga", Context.MODE_PRIVATE) val mangaList: List<TopManga>? = getDataFromCache() - if(mangaList != null ){ + if (mangaList != null) { showList(view, mangaList) } else { - makeApiCall(view, base_url) + makeApiCall(view, baseUrl) } return view @@ -55,11 +56,11 @@ class TopMangaPage : Fragment() { val jsonManga: String? = sharedPreferences?.getString("jsonMangaList", null) //if it's null, well, return null - if(jsonManga == null) { - return null + return if (jsonManga == null) { + null } else { //else deserialize the list and return it val listType: Type = object : TypeToken<List<TopManga>>() {}.type - return gson.fromJson(jsonManga, listType) + gson.fromJson(jsonManga, listType) } } @@ -72,12 +73,13 @@ class TopMangaPage : Fragment() { } fun updateList() { - makeApiCall(view,base_url) + makeApiCall(view, baseUrl) Snackbar.make(requireView(), "Data refreshed", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } + val swipeRefresh: SwipeRefreshLayout = view.findViewById(R.id.swiperefresh) - swipeRefresh.setOnRefreshListener{ + swipeRefresh.setOnRefreshListener { updateList() swipeRefresh.isRefreshing = false } @@ -85,7 +87,7 @@ class TopMangaPage : Fragment() { } //display the recyclerview - fun showList(view: View, mangaList: List<TopManga> ){ + fun showList(view: View, mangaList: List<TopManga>) { val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view) recyclerView.setHasFixedSize(true) recyclerView.layoutManager = LinearLayoutManager(view.context) @@ -95,20 +97,27 @@ class TopMangaPage : Fragment() { private fun makeApiCall(view: View, BASE_URL: String) { val retrofit = Retrofit.Builder() - .baseUrl(BASE_URL) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build() + .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: List<TopManga> = response.body()!!.getResults() //getting the "top" field containing our list of TopMangas + 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: List<TopManga> = response.body()!! + .getResults() //getting the "top" field containing our list of TopMangas saveList(mangaList) - showList(view, mangaList) //calling the method in charge of displaying on the recyclerview + showList( + view, + mangaList + ) //calling the method in charge of displaying on the recyclerview } else { showError() //a snackbar @@ -126,14 +135,14 @@ class TopMangaPage : Fragment() { val jsonString: String = gson.toJson(mangaList) sharedPreferences - ?.edit() - ?.putString("jsonMangaList", jsonString) - ?.apply() + ?.edit() + ?.putString("jsonMangaList", jsonString) + ?.apply() } private fun showError() { Snackbar.make(requireView(), "API ERROR", Snackbar.LENGTH_LONG) - .setAction("Action", null).show() + .setAction("Action", null).show() } }
\ No newline at end of file |