From 7c8476a2128f6c4494eaa2d0a363d8cbe3261ba6 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Sat, 20 Feb 2021 18:56:42 +0100 Subject: Beginning the manga api part. --- app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt | 31 ++++++++++++ .../java/xyz/adjutor/aniki/manga/MangaAdapter.kt | 58 ++++++++++++++++++++++ .../main/java/xyz/adjutor/aniki/manga/MangaApi.kt | 11 ++++ 3 files changed, 100 insertions(+) create mode 100644 app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt (limited to 'app/src/main/java/xyz/adjutor/aniki/manga') diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt b/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt new file mode 100644 index 0000000..43944c3 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt @@ -0,0 +1,31 @@ +package xyz.adjutor.aniki.manga + +import com.google.gson.annotations.SerializedName + +//Content of the top field from the api of manga +class Manga{ + + @SerializedName("url") + var url: String? = null + @SerializedName("title") + var title: String? = null + @SerializedName("rank") + var rank: Int? = null + @SerializedName("volumes") + var volumes: Int? = null + @SerializedName("chapters") + var chapters: 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 + @SerializedName("synopsis") + var synopsis: String? = null + @SerializedName("background") + var background: String? = null + +} diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt new file mode 100644 index 0000000..ad753e2 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt @@ -0,0 +1,58 @@ +@file:Suppress("DEPRECATION") + +package xyz.adjutor.aniki.manga + +import android.content.Intent +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import androidx.cardview.widget.CardView +import androidx.recyclerview.widget.RecyclerView +import com.bumptech.glide.Glide +import com.bumptech.glide.request.RequestOptions +import xyz.adjutor.aniki.R +import xyz.adjutor.aniki.topmanga.DetailTopMangaActivity + +class MangaAdapter(val mangaList: List) : + RecyclerView.Adapter() { + + // Describes an item view and its place within the RecyclerView + class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + val mangaTitle: TextView = itemView.findViewById(R.id.tv_title) + val mangaRank: TextView = itemView.findViewById(R.id.tv_rank) + val mangaScore: TextView = itemView.findViewById(R.id.tv_score) + val mangaImage: ImageView = itemView.findViewById(R.id.iv_image) + val cardview: CardView = itemView.findViewById(R.id.cv_cardView) + + } + + // 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) + + return MangaViewHolder(view) + } + + // Returns size of data list + override fun getItemCount(): Int { + return mangaList.size + } + + // Displays data at a certain position + override fun onBindViewHolder(holder: MangaViewHolder, position: Int) { + val currentManga: Manga = mangaList[position] + holder.mangaTitle.text = currentManga.title + holder.mangaRank.text = currentManga.rank.toString() + 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) + } +} + diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt b/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt new file mode 100644 index 0000000..7692836 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt @@ -0,0 +1,11 @@ +package xyz.adjutor.aniki.manga + +import retrofit2.Call +import retrofit2.http.GET + +interface MangaApi { + + @GET("v3/manga") + fun getMangaData(): Call + +} \ No newline at end of file -- cgit v1.2.3 From 8201603b4f6a7216f466d4a839421415d3e0f6dc Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Sun, 21 Feb 2021 18:05:34 +0100 Subject: Only keeping useful infos from api responses --- app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt | 21 ++------------------- .../adjutor/aniki/topanime/RestTopAnimeResponse.kt | 6 ------ .../java/xyz/adjutor/aniki/topanime/TopAnime.kt | 6 ------ .../adjutor/aniki/topmanga/RestTopMangaResponse.kt | 6 ------ .../java/xyz/adjutor/aniki/topmanga/TopManga.kt | 6 ------ 5 files changed, 2 insertions(+), 43 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki/manga') diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt b/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt index 43944c3..baba9f5 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt @@ -2,30 +2,13 @@ package xyz.adjutor.aniki.manga import com.google.gson.annotations.SerializedName -//Content of the top field from the api of manga -class Manga{ +class Manga{ //only kept the infos I didnt have and that were interresting to me. - @SerializedName("url") - var url: String? = null - @SerializedName("title") - var title: String? = null - @SerializedName("rank") - var rank: Int? = null - @SerializedName("volumes") - var volumes: Int? = null @SerializedName("chapters") var chapters: 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 @SerializedName("synopsis") var synopsis: String? = null @SerializedName("background") - var background: String? = null + var background: String? = null //a bit of background story about the manga } diff --git a/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt index c711631..0bd1354 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topanime/RestTopAnimeResponse.kt @@ -4,12 +4,6 @@ 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 diff --git a/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt index 3576e7b..e063ad1 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnime.kt @@ -11,18 +11,12 @@ class TopAnime{ 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") diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt index 897e8a2..dc7827a 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/RestTopMangaResponse.kt @@ -4,12 +4,6 @@ import com.google.gson.annotations.SerializedName class RestTopMangaResponse { - @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 diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt index edf561d..0f73dd4 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopManga.kt @@ -11,18 +11,12 @@ class TopManga{ var rank: Int? = null @SerializedName("title") var title: String? = null - @SerializedName("url") - var url: String? = null - @SerializedName("type") - var type: 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("members") - var members: Int? = null @SerializedName("score") var score: Float? = null @SerializedName("image_url") -- cgit v1.2.3 From 56f69f774bc2a7ef645169f85b89934255c29cf9 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Mon, 22 Feb 2021 00:00:12 +0100 Subject: API call and display on DetailTopMangaActivity.kt Still need to find a way to call with the right ID. Rn, using the id 1 of monster as a "placeholder" --- app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt | 14 ----- .../java/xyz/adjutor/aniki/manga/MangaAdapter.kt | 58 ----------------- .../main/java/xyz/adjutor/aniki/manga/MangaApi.kt | 4 +- .../xyz/adjutor/aniki/manga/RestMangaResponse.kt | 14 +++++ .../xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt | 2 - .../aniki/topmanga/DetailTopMangaActivity.kt | 72 ++++++++++++++++++++-- .../xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt | 4 +- .../main/res/layout/activity_detail_top_manga.xml | 34 ++++++++-- app/src/main/res/values/strings.xml | 16 ++--- 9 files changed, 120 insertions(+), 98 deletions(-) delete mode 100644 app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt delete mode 100644 app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt (limited to 'app/src/main/java/xyz/adjutor/aniki/manga') diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt b/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt deleted file mode 100644 index baba9f5..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/manga/Manga.kt +++ /dev/null @@ -1,14 +0,0 @@ -package xyz.adjutor.aniki.manga - -import com.google.gson.annotations.SerializedName - -class Manga{ //only kept the infos I didnt have and that were interresting to me. - - @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/MangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt deleted file mode 100644 index ad753e2..0000000 --- a/app/src/main/java/xyz/adjutor/aniki/manga/MangaAdapter.kt +++ /dev/null @@ -1,58 +0,0 @@ -@file:Suppress("DEPRECATION") - -package xyz.adjutor.aniki.manga - -import android.content.Intent -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.ImageView -import android.widget.TextView -import androidx.cardview.widget.CardView -import androidx.recyclerview.widget.RecyclerView -import com.bumptech.glide.Glide -import com.bumptech.glide.request.RequestOptions -import xyz.adjutor.aniki.R -import xyz.adjutor.aniki.topmanga.DetailTopMangaActivity - -class MangaAdapter(val mangaList: List) : - RecyclerView.Adapter() { - - // Describes an item view and its place within the RecyclerView - class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - val mangaTitle: TextView = itemView.findViewById(R.id.tv_title) - val mangaRank: TextView = itemView.findViewById(R.id.tv_rank) - val mangaScore: TextView = itemView.findViewById(R.id.tv_score) - val mangaImage: ImageView = itemView.findViewById(R.id.iv_image) - val cardview: CardView = itemView.findViewById(R.id.cv_cardView) - - } - - // 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) - - return MangaViewHolder(view) - } - - // Returns size of data list - override fun getItemCount(): Int { - return mangaList.size - } - - // Displays data at a certain position - override fun onBindViewHolder(holder: MangaViewHolder, position: Int) { - val currentManga: Manga = mangaList[position] - holder.mangaTitle.text = currentManga.title - holder.mangaRank.text = currentManga.rank.toString() - 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) - } -} - diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt b/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt index 7692836..77304bb 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt @@ -5,7 +5,7 @@ import retrofit2.http.GET interface MangaApi { - @GET("v3/manga") - fun getMangaData(): Call + @GET("v3/manga/1") + fun getMangaData(): Call } \ 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 new file mode 100644 index 0000000..7a4aff9 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt @@ -0,0 +1,14 @@ +package xyz.adjutor.aniki.manga + +import com.google.gson.annotations.SerializedName + +class RestMangaResponse{ //only kept the infos I didnt have and that were interresting to me. + + @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/topanime/TopAnimeAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt index 4953939..d38adb2 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topanime/TopAnimeAdapter.kt @@ -1,5 +1,3 @@ -@file:Suppress("DEPRECATION") - package xyz.adjutor.aniki.topanime import android.view.LayoutInflater diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt index 27f602a..152fe4a 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt @@ -1,16 +1,34 @@ package xyz.adjutor.aniki.topmanga +import android.content.Context import android.os.Bundle +import android.view.View import android.widget.ImageView import android.widget.TextView +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions -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 +import xyz.adjutor.aniki.manga.MangaApi +import xyz.adjutor.aniki.manga.RestMangaResponse class DetailTopMangaActivity : AppCompatActivity() { + var base_url = "https://api.jikan.moe/" + val gson = GsonBuilder() + .setLenient() + .create() + var manga: RestMangaResponse? = null + private val mangaId = "themangaid" private val mangaTitle = "themangatitle" private val mangaRank = "themangarank" @@ -45,11 +63,6 @@ class DetailTopMangaActivity : AppCompatActivity() { val tvStartDate: TextView = findViewById(R.id.tv_start_date) val tvEndDate: TextView = findViewById(R.id.tv_end_date) - //elements from Manga - val tvChapters: TextView = findViewById(R.id.tv_chapters) - val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) - val tvBackground: TextView = findViewById(R.id.tv_background) - tvId.text = mangaId tvTitle.text = mangaTitle tvRank.text = mangaRank @@ -63,5 +76,52 @@ class DetailTopMangaActivity : AppCompatActivity() { tvVolumes.text = mangaVolumes tvStartDate.text = mangaStartDate tvEndDate.text = mangaEndDate + + makeApiCall(base_url) + } + + private fun makeApiCall(BASE_URL: String) { + + val retrofit = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + + val service = retrofit.create(MangaApi::class.java) + val call = service.getMangaData() + + call.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty + + manga = response.body() //getting the RestMangaResponse fields + showDetail(manga!!) + + } else { + showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call, t: Throwable) { + showError("API ERROR : onFailure") + } + + }) + } + + private fun showDetail(manga: RestMangaResponse) { + //elements from RestMangaResponse + val tvChapters: TextView = findViewById(R.id.tv_chapters) + val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) + val tvBackground: TextView = findViewById(R.id.tv_background) + + tvChapters.text = manga.chapters.toString() + tvSynopsis.text = manga.synopsis.toString() + tvBackground.text = manga.background.toString() + + } + + fun showError(text: String) { + Toast.makeText(this, text, Toast.LENGTH_LONG).show() } } \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt index 7a32dd5..c2b3567 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/TopMangaAdapter.kt @@ -1,5 +1,3 @@ -@file:Suppress("DEPRECATION") - package xyz.adjutor.aniki.topmanga import android.content.Intent @@ -53,7 +51,7 @@ class TopMangaAdapter(val mangaList: List) : .apply(RequestOptions().override(400)) .into(holder.mangaImage) - //to go to the detail page of the selected manga + //when you click on a selected cardview, some datas are sent to the other activity holder.cardview.setOnClickListener { val currentMangaId = "themangaid" val currentMangaTitle = "themangatitle" diff --git a/app/src/main/res/layout/activity_detail_top_manga.xml b/app/src/main/res/layout/activity_detail_top_manga.xml index b9a5dd7..ffb9791 100644 --- a/app/src/main/res/layout/activity_detail_top_manga.xml +++ b/app/src/main/res/layout/activity_detail_top_manga.xml @@ -104,9 +104,10 @@ android:id="@+id/cv_more_infos" android:layout_width="match_parent" android:layout_height="wrap_content" - app:layout_constraintTop_toBottomOf="@id/cv_header" android:layout_margin="4sp" - app:cardBackgroundColor="@color/black"> + app:cardBackgroundColor="@color/black" + app:layout_constraintTop_toBottomOf="@id/cv_header"> + + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="@id/tv_volumes" /> + + + + + + app:layout_constraintTop_toBottomOf="@id/tv_text_synopsis"/> @@ -226,6 +239,15 @@ android:layout_height="match_parent" android:padding="10dp"> + + + app:layout_constraintTop_toBottomOf="@id/tv_text_background"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 100335d..311e39b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -17,17 +17,19 @@ 00.00 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. - "MAL ID : " - "Rank : " - "Score : " + "MAL ID " + "Rank " + "Score " 000 - "Volumes : " - "Chapters: " + "Volumes " + "Chapters " 0000 - "Start date : " - "End date : " + "Start date " + "End date " Jan 0000 Dec 0000 Cras molestie turpis at lorem rutrum vehicula. Fusce rutrum, enim sed laoreet semper, tortor quam semper dui, id bibendum lacus quam vitae neque. + Synopsis + Background \ No newline at end of file -- cgit v1.2.3 From a079577ca21e16dd4b424eea28498f7b56a297df Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Mon, 22 Feb 2021 00:47:34 +0100 Subject: API's now being called with an ID as a parameter. --- .../main/java/xyz/adjutor/aniki/manga/MangaApi.kt | 5 ++- .../aniki/topmanga/DetailTopMangaActivity.kt | 47 +++++++++++----------- 2 files changed, 27 insertions(+), 25 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki/manga') diff --git a/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt b/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt index 77304bb..419d510 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/MangaApi.kt @@ -2,10 +2,11 @@ package xyz.adjutor.aniki.manga import retrofit2.Call import retrofit2.http.GET +import retrofit2.http.Path interface MangaApi { - @GET("v3/manga/1") - fun getMangaData(): Call + @GET("v3/manga/{id}") + fun getMangaData(@Path("id") id: String): Call } \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt index 152fe4a..b222a99 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt @@ -23,35 +23,34 @@ import xyz.adjutor.aniki.manga.RestMangaResponse class DetailTopMangaActivity : AppCompatActivity() { - var base_url = "https://api.jikan.moe/" - val gson = GsonBuilder() + private var baseUrl = "https://api.jikan.moe/" + private val gson = GsonBuilder() .setLenient() .create() - var manga: RestMangaResponse? = null - private val mangaId = "themangaid" - private val mangaTitle = "themangatitle" - private val mangaRank = "themangarank" - private val mangaScore = "themangascore" - private val mangaImageUrl = "themangaimageurl" + private val intentMangaId = "themangaid" + private val intentMangaTitle = "themangatitle" + private val intentMangaRank = "themangarank" + private val intentMangaScore = "themangascore" + private val intentMangaImageUrl = "themangaimageurl" - private val mangaVolumes = "themangavolumes" - private val mangaStartDate = "themangastartdate" - private val mangaEndDate = "themangaenddate" + private val intentMangaVolumes = "themangavolumes" + private val intentMangaStartDate = "themangastartdate" + private val intentMangaEndDate = "themangaenddate" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail_top_manga) - val mangaId = intent.getStringExtra(mangaId) - val mangaTitle = intent.getStringExtra(mangaTitle) - val mangaRank = intent.getStringExtra(mangaRank) - val mangaScore = intent.getStringExtra(mangaScore) - val mangaImage = intent.getStringExtra(mangaImageUrl) + val mangaId = intent.getStringExtra(intentMangaId) + val mangaTitle = intent.getStringExtra(intentMangaTitle) + val mangaRank = intent.getStringExtra(intentMangaRank) + val mangaScore = intent.getStringExtra(intentMangaScore) + val mangaImage = intent.getStringExtra(intentMangaImageUrl) - val mangaVolumes = intent.getStringExtra(mangaVolumes) - val mangaStartDate = intent.getStringExtra(mangaStartDate) - val mangaEndDate = intent.getStringExtra(mangaEndDate) + val mangaVolumes = intent.getStringExtra(intentMangaVolumes) + val mangaStartDate = intent.getStringExtra(intentMangaStartDate) + val mangaEndDate = intent.getStringExtra(intentMangaEndDate) val tvId: TextView = findViewById(R.id.tv_detail_id) val tvTitle: TextView = findViewById(R.id.tv_detail_title) @@ -73,14 +72,16 @@ class DetailTopMangaActivity : AppCompatActivity() { .apply(RequestOptions().override(400)) .into(ivImage) + //not used in the recyclerview tvVolumes.text = mangaVolumes tvStartDate.text = mangaStartDate tvEndDate.text = mangaEndDate - makeApiCall(base_url) + //taking the API's fields I want and displaying them + makeApiCall(baseUrl, mangaId.toString()) } - private fun makeApiCall(BASE_URL: String) { + private fun makeApiCall(BASE_URL: String, mangaId: String) { val retrofit = Retrofit.Builder() .baseUrl(BASE_URL) @@ -88,13 +89,13 @@ class DetailTopMangaActivity : AppCompatActivity() { .build() val service = retrofit.create(MangaApi::class.java) - val call = service.getMangaData() + val call = service.getMangaData(mangaId) call.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if(response.isSuccessful && response.body() != null){ //if the code returned is >= 200 and < 300 AND the the body ain't empty - manga = response.body() //getting the RestMangaResponse fields + val manga = response.body() //getting the RestMangaResponse fields showDetail(manga!!) } else { -- cgit v1.2.3 From 9ee5918fa86da6c160145498b1a95f4f4a083fdc Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Mon, 22 Feb 2021 14:25:15 +0100 Subject: Sharedpreference now working for the DetailTopMangaActivity.kt --- app/build.gradle | 1 - .../xyz/adjutor/aniki/manga/RestMangaResponse.kt | 4 ++- .../aniki/topmanga/DetailTopMangaActivity.kt | 41 ++++++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki/manga') diff --git a/app/build.gradle b/app/build.gradle index 1e612ce..d1d101c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -34,7 +34,6 @@ android { dependencies { - //noinspection GradleDependency implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' 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 7a4aff9..00b0dce 100644 --- a/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt +++ b/app/src/main/java/xyz/adjutor/aniki/manga/RestMangaResponse.kt @@ -2,8 +2,10 @@ package xyz.adjutor.aniki.manga import com.google.gson.annotations.SerializedName -class RestMangaResponse{ //only kept the infos I didnt have and that were interresting 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") diff --git a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt index 1863815..32e3457 100644 --- a/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt +++ b/app/src/main/java/xyz/adjutor/aniki/topmanga/DetailTopMangaActivity.kt @@ -1,5 +1,7 @@ package xyz.adjutor.aniki.topmanga +import android.content.Context +import android.content.SharedPreferences import android.os.Bundle import android.widget.ImageView import android.widget.TextView @@ -8,6 +10,7 @@ import androidx.appcompat.app.AppCompatActivity import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -16,10 +19,12 @@ import retrofit2.converter.gson.GsonConverterFactory import xyz.adjutor.aniki.R import xyz.adjutor.aniki.manga.MangaApi import xyz.adjutor.aniki.manga.RestMangaResponse +import java.lang.reflect.Type class DetailTopMangaActivity : AppCompatActivity() { private var baseUrl = "https://api.jikan.moe/" + var sharedPreferences: SharedPreferences? = null private val gson = GsonBuilder() .setLenient() .create() @@ -38,6 +43,8 @@ class DetailTopMangaActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail_top_manga) + sharedPreferences = this.getSharedPreferences("app_aniki", Context.MODE_PRIVATE) + val mangaId = intent.getStringExtra(intentMangaId) val mangaTitle = intent.getStringExtra(intentMangaTitle) val mangaRank = intent.getStringExtra(intentMangaRank) @@ -83,8 +90,26 @@ class DetailTopMangaActivity : AppCompatActivity() { fieldIsNull() } - //taking the API's fields I want and displaying them - makeApiCall(baseUrl, mangaId.toString()) + val manga: RestMangaResponse? = getDataFromCache(mangaId.toString()) + if(manga != null ){ + showDetail(manga) + } else { + //taking the API's fields I want and displaying them + makeApiCall(baseUrl, mangaId.toString()) + } + + + } + + private fun getDataFromCache(mangaId: String): RestMangaResponse? { + val jsonManga: String?= sharedPreferences?.getString(mangaId, null) + + if(jsonManga == null) { + return null + } else { + val type: Type = object : TypeToken() {}.type + return gson.fromJson(jsonManga, type) + } } private fun makeApiCall(BASE_URL: String, mangaId: String) { @@ -95,13 +120,14 @@ class DetailTopMangaActivity : AppCompatActivity() { .build() val service = retrofit.create(MangaApi::class.java) - val call = service.getMangaData(mangaId) + val call = service.getMangaData(mangaId) //based on the id call.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { 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) showDetail(manga!!) } else { @@ -144,4 +170,13 @@ class DetailTopMangaActivity : AppCompatActivity() { fun fieldIsNull(): String{ return "Unknown" } + + fun saveList(manga: RestMangaResponse?) { + val jsonString: String = gson.toJson(manga) + + sharedPreferences + ?.edit() + ?.putString(manga?.mal_id.toString(), jsonString) + ?.apply() + } } \ No newline at end of file -- cgit v1.2.3