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') 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