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/adjutor/aniki/manga/topmanga | |
parent | 9e1f3503c1a80d7e108f60f9065ae08abb7c2b3f (diff) |
Cleaning up the code.
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/manga/topmanga')
4 files changed, 84 insertions, 62 deletions
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 |