From ac929460d07e0d0ef8c6b4ea569a2b2c6daa3b13 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Tue, 23 Mar 2021 11:47:53 +0100 Subject: Refactoring into MVC --- .../aniki/presentation/model/AnimeResponse.kt | 16 ++ .../aniki/presentation/model/MangaResponse.kt | 22 +++ .../aniki/presentation/model/SearchAnime.kt | 31 ++++ .../presentation/model/SearchAnimeResponse.kt | 13 ++ .../aniki/presentation/model/SearchManga.kt | 34 ++++ .../presentation/model/SearchMangaResponse.kt | 13 ++ .../adjutor/aniki/presentation/model/TopAnime.kt | 35 ++++ .../aniki/presentation/model/TopAnimeResponse.kt | 13 ++ .../adjutor/aniki/presentation/model/TopManga.kt | 35 ++++ .../aniki/presentation/model/TopMangaResponse.kt | 13 ++ .../presentation/view/DetailSearchAnimeActivity.kt | 156 +++++++++++++++++ .../presentation/view/DetailSearchMangaActivity.kt | 169 ++++++++++++++++++ .../presentation/view/DetailTopAnimeActivity.kt | 176 +++++++++++++++++++ .../presentation/view/DetailTopMangaActivity.kt | 193 +++++++++++++++++++++ .../adjutor/aniki/presentation/view/HomePage.kt | 38 ++++ .../aniki/presentation/view/MainActivity.kt | 14 ++ .../aniki/presentation/view/SearchAnimeAdapter.kt | 82 +++++++++ .../aniki/presentation/view/SearchAnimePage.kt | 136 +++++++++++++++ .../aniki/presentation/view/SearchMangaAdapter.kt | 84 +++++++++ .../aniki/presentation/view/SearchMangaPage.kt | 144 +++++++++++++++ .../aniki/presentation/view/TopAnimeAdapter.kt | 81 +++++++++ .../aniki/presentation/view/TopAnimePage.kt | 176 +++++++++++++++++++ .../aniki/presentation/view/TopMangaAdapter.kt | 82 +++++++++ .../aniki/presentation/view/TopMangaPage.kt | 177 +++++++++++++++++++ 24 files changed, 1933 insertions(+) create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/AnimeResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/MangaResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnime.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnimeResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchManga.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchMangaResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnime.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnimeResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/TopManga.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/model/TopMangaResponse.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchAnimeActivity.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchMangaActivity.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopAnimeActivity.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopMangaActivity.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/HomePage.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/MainActivity.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/SearchAnimeAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/SearchAnimePage.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/SearchMangaAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/SearchMangaPage.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/TopAnimeAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/TopAnimePage.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/TopMangaAdapter.kt create mode 100644 app/src/main/java/xyz/adjutor/aniki/presentation/view/TopMangaPage.kt (limited to 'app/src/main/java/xyz/adjutor/aniki/presentation') diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/AnimeResponse.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/AnimeResponse.kt new file mode 100644 index 0000000..d682e3c --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/AnimeResponse.kt @@ -0,0 +1,16 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class AnimeResponse { //only kept the infos I didn't have and that were interesting to me. + + @SerializedName("mal_id") + var mal_id: Int? = null + + @SerializedName("rank") + var rank: Int? = null //added for the search feature (detail) + + @SerializedName("synopsis") + var synopsis: String? = null + +} diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/MangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/MangaResponse.kt new file mode 100644 index 0000000..4bd7f2f --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/MangaResponse.kt @@ -0,0 +1,22 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class MangaResponse { //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("rank") + var rank: Int? = null //added for the search feature (detail) + + @SerializedName("background") + var background: String? = null //a bit of background story about the manga + +} diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnime.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnime.kt new file mode 100644 index 0000000..df5189d --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnime.kt @@ -0,0 +1,31 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class SearchAnime { + + @SerializedName("mal_id") + var mal_id: Int? = null + + @SerializedName("url") + var url: String? = null + + @SerializedName("image_url") + var image_url: String? = null + + @SerializedName("title") + var title: String? = null + + @SerializedName("episodes") + var episodes: Int? = null + + @SerializedName("score") + var score: Float? = null + + @SerializedName("start_date") //we'll maybe remove this later + var start_date: String? = null + + @SerializedName("end_date") //we'll maybe remove this later + var end_date: String? = null + +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnimeResponse.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnimeResponse.kt new file mode 100644 index 0000000..1e20e2f --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchAnimeResponse.kt @@ -0,0 +1,13 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class SearchAnimeResponse { //only kept the infos I didn't have and that were interesting to me. + + @SerializedName("results") + private lateinit var results: List + + fun getResults(): List { + return results + } +} diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchManga.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchManga.kt new file mode 100644 index 0000000..c24d93c --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchManga.kt @@ -0,0 +1,34 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class SearchManga { + + @SerializedName("mal_id") + var mal_id: Int? = null + + @SerializedName("url") + var url: String? = null + + @SerializedName("image_url") + var image_url: String? = null + + @SerializedName("title") + var title: String? = null + + @SerializedName("chapters") + var chapters: Int? = null + + @SerializedName("volumes") + var volumes: Int? = null + + @SerializedName("score") + var score: Float? = null + + @SerializedName("start_date") //we'll maybe remove this later + var start_date: String? = null + + @SerializedName("end_date") //we'll maybe remove this later + var end_date: String? = null + +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchMangaResponse.kt new file mode 100644 index 0000000..4fe7047 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/SearchMangaResponse.kt @@ -0,0 +1,13 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class SearchMangaResponse { //only kept the infos I didn't have and that were interesting to me. + + @SerializedName("results") + private lateinit var results: List + + fun getResults(): List { + return results + } +} diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnime.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnime.kt new file mode 100644 index 0000000..3cf4390 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnime.kt @@ -0,0 +1,35 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +//Content of the top field from the api of top anime +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/presentation/model/TopAnimeResponse.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnimeResponse.kt new file mode 100644 index 0000000..95d39d6 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopAnimeResponse.kt @@ -0,0 +1,13 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class TopAnimeResponse { + + @SerializedName("top") + var top: List? = null + + fun getResults(): List { + return top!! + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopManga.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopManga.kt new file mode 100644 index 0000000..2e0886a --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopManga.kt @@ -0,0 +1,35 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +//Content of the top field from the api of top manga +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/presentation/model/TopMangaResponse.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopMangaResponse.kt new file mode 100644 index 0000000..1b0dd8f --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/model/TopMangaResponse.kt @@ -0,0 +1,13 @@ +package xyz.adjutor.aniki.presentation.model + +import com.google.gson.annotations.SerializedName + +class TopMangaResponse { + + @SerializedName("top") + private lateinit var top: List + + fun getResults(): List { + return top + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchAnimeActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchAnimeActivity.kt new file mode 100644 index 0000000..68a30de --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchAnimeActivity.kt @@ -0,0 +1,156 @@ +package xyz.adjutor.aniki.presentation.view + +import android.os.Bundle +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.bumptech.glide.Glide +import com.bumptech.glide.request.RequestOptions +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.data.AnimeApi +import xyz.adjutor.aniki.presentation.model.AnimeResponse + +class DetailSearchAnimeActivity : AppCompatActivity() { + + private var baseUrl = "https://api.jikan.moe/" + private val gson = GsonBuilder() + .setLenient() + .create() + + //used in the list + private val intentAnimeImageUrl = "theanimeimageurl" + private val intentAnimeTitle = "theanimetitle" + private val intentAnimeScore = "theanimescore" + + //only used for the detail + private val intentAnimeId = "theanimeid" + private val intentAnimeUrl = "theanimeurl" + private val intentAnimeEpisodes = "theanimeepisodes" + private val intentAnimeStartDate = "theanimestartdate" + private val intentAnimeEndDate = "theanimeenddate" + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_detail_search_anime) + + val animeImageUrl = intent.getStringExtra(intentAnimeImageUrl) + val animeTitle = intent.getStringExtra(intentAnimeTitle) + val animeScore = intent.getStringExtra(intentAnimeScore) + + val animeId = intent.getStringExtra(intentAnimeId) + val animeUrl = intent.getStringExtra(intentAnimeUrl) + val animeEpisodes = intent.getStringExtra(intentAnimeEpisodes) + val animeStartDate = intent.getStringExtra(intentAnimeStartDate) + val animeEndDate = intent.getStringExtra(intentAnimeEndDate) + + + val ivImage: ImageView = findViewById(R.id.iv_detail_image) + val tvTitle: TextView = findViewById(R.id.tv_detail_title) + val tvScore: TextView = findViewById(R.id.tv_detail_score) + + val tvId: TextView = findViewById(R.id.tv_detail_id) + val tvUrl: TextView = findViewById(R.id.tv_url) + val tvEpisodes: TextView = findViewById(R.id.tv_episodes) + val tvStartDate: TextView = findViewById(R.id.tv_start_date) + val tvEndDate: TextView = findViewById(R.id.tv_end_date) + + Glide + .with(this) + .load(animeImageUrl) + .apply(RequestOptions().override(400)) + .into(ivImage) + tvTitle.text = animeTitle + tvScore.text = animeScore + + + tvId.text = animeId + tvUrl.text = animeUrl + + //using null as a string because it has been converted to a string before + tvEpisodes.text = if (animeEpisodes != "null") { + animeEpisodes + } else { + fieldIsNull() + } + + tvStartDate.text = splitDate(animeStartDate!!) + + tvEndDate.text = if (animeEndDate != "null") { + splitDate(animeEndDate!!) + } else { + fieldIsNull() + } + + makeApiCall(baseUrl, animeId.toString()) + + } + + private fun splitDate(animeDate: String): CharSequence { + val delimiter = "T" + return animeDate + .split(delimiter) //split between the date and the time + .toTypedArray()[0] //convert it to an array and take the first string + + } + + private fun makeApiCall( + BASE_URL: String, + animeId: String + ) { //we take the rest of the data that we need from the internet + + val retrofit = Retrofit.Builder() + .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 { + 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 anime = response.body() //getting the AnimeResponse fields + showDetail(anime!!) + + } else { + showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call, t: Throwable) { + showError("API ERROR : onFailure") + } + + }) + } + + private fun showDetail(anime: AnimeResponse) { + //elements from AnimeResponse + val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) + val tvRank: TextView = findViewById(R.id.tv_detail_rank) + + tvSynopsis.text = anime.synopsis.toString() + + tvRank.text = anime.rank.toString() + + } + + fun showError(text: String) { + Toast.makeText(this, text, Toast.LENGTH_LONG).show() + } + + private fun fieldIsNull(): String { + return "Unknown" + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchMangaActivity.kt new file mode 100644 index 0000000..d96cc70 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailSearchMangaActivity.kt @@ -0,0 +1,169 @@ +package xyz.adjutor.aniki.presentation.view + +import android.os.Bundle +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.bumptech.glide.Glide +import com.bumptech.glide.request.RequestOptions +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.data.MangaApi +import xyz.adjutor.aniki.presentation.model.MangaResponse + +class DetailSearchMangaActivity : AppCompatActivity() { + + private var baseUrl = "https://api.jikan.moe/" + private val gson = GsonBuilder() + .setLenient() + .create() + + //used in the list + private val intentMangaImageUrl = "themangaimageurl" + private val intentMangaTitle = "themangatitle" + private val intentMangaScore = "themangascore" + + //only used for the detail + private val intentMangaId = "themangaid" + private val intentMangaUrl = "themangaurl" + private val intentMangaChapters = "themangachapters" + 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_search_manga) + + val mangaImageUrl = intent.getStringExtra(intentMangaImageUrl) + val mangaTitle = intent.getStringExtra(intentMangaTitle) + val mangaScore = intent.getStringExtra(intentMangaScore) + + val mangaId = intent.getStringExtra(intentMangaId) + val mangaUrl = intent.getStringExtra(intentMangaUrl) + val mangaChapters = intent.getStringExtra(intentMangaChapters) + val mangaVolumes = intent.getStringExtra(intentMangaVolumes) + val mangaStartDate = intent.getStringExtra(intentMangaStartDate) + val mangaEndDate = intent.getStringExtra(intentMangaEndDate) + + + val ivImage: ImageView = findViewById(R.id.iv_detail_image) + val tvTitle: TextView = findViewById(R.id.tv_detail_title) + val tvScore: TextView = findViewById(R.id.tv_detail_score) + + val tvId: TextView = findViewById(R.id.tv_detail_id) + val tvUrl: TextView = findViewById(R.id.tv_url) + val tvChapters: TextView = findViewById(R.id.tv_chapters) + val tvVolumes: TextView = findViewById(R.id.tv_volumes) + val tvStartDate: TextView = findViewById(R.id.tv_start_date) + val tvEndDate: TextView = findViewById(R.id.tv_end_date) + + Glide + .with(this) + .load(mangaImageUrl) + .apply(RequestOptions().override(400)) + .into(ivImage) + tvTitle.text = mangaTitle + tvScore.text = mangaScore + + + tvId.text = mangaId + tvUrl.text = mangaUrl + + //using null as a string because it has been converted to a string before + tvChapters.text = if (mangaChapters != "null") { + mangaChapters + } else { + fieldIsNull() + } + + tvVolumes.text = if (mangaVolumes != "null") { + mangaVolumes + } else { + fieldIsNull() + } + + tvStartDate.text = splitDate(mangaStartDate!!) + + tvEndDate.text = if (mangaEndDate != "null") { + splitDate(mangaEndDate!!) + } else { + fieldIsNull() + } + + makeApiCall(baseUrl, mangaId.toString()) + + } + + private fun splitDate(mangaDate: String): CharSequence { + val delimiter = "T" + return mangaDate + .split(delimiter) //split between the date and the time + .toTypedArray()[0] //convert it to an array and take the first string + + } + + private fun makeApiCall(BASE_URL: String, mangaId: String) { + + val retrofit = Retrofit.Builder() + .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 { + 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 MangaResponse 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: MangaResponse) { + //elements from MangaResponse + val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) + val tvRank: TextView = findViewById(R.id.tv_detail_rank) + val tvBackground: TextView = findViewById(R.id.tv_background) + + tvSynopsis.text = manga.synopsis.toString() + + tvRank.text = manga.rank.toString() + + tvBackground.text = if (manga.background != null) { + manga.background.toString() + } else { + fieldIsNull() + } + + } + + fun showError(text: String) { + Toast.makeText(this, text, Toast.LENGTH_LONG).show() + } + + private fun fieldIsNull(): String { + return "Unknown" + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopAnimeActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopAnimeActivity.kt new file mode 100644 index 0000000..3ec6ab6 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopAnimeActivity.kt @@ -0,0 +1,176 @@ +package xyz.adjutor.aniki.presentation.view + +import android.content.Context +import android.content.SharedPreferences +import android.os.Bundle +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +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 +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import xyz.adjutor.aniki.R +import xyz.adjutor.aniki.data.AnimeApi +import xyz.adjutor.aniki.presentation.model.AnimeResponse +import java.lang.reflect.Type + +class DetailTopAnimeActivity : AppCompatActivity() { + + private var baseUrl = "https://api.jikan.moe/" + private lateinit var sharedPreferences: SharedPreferences + private val gson = GsonBuilder() + .setLenient() + .create() + + private val intentAnimeId = "theanimeid" + private val intentAnimeTitle = "theanimetitle" + private val intentAnimeRank = "theanimerank" + private val intentAnimeScore = "theanimescore" + private val intentAnimeImageUrl = "theanimeimageurl" + + private val intentAnimeEpisodes = "theanimeepisodes" + private val intentAnimeStartDate = "theanimestartdate" + private val intentAnimeEndDate = "theanimeenddate" + private val intentAnimeUrl = "theanimeurl" + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_detail_top_anime) + + sharedPreferences = this.getSharedPreferences("sp_anime", Context.MODE_PRIVATE) + + val animeId = intent.getStringExtra(intentAnimeId) + val animeTitle = intent.getStringExtra(intentAnimeTitle) + val animeRank = intent.getStringExtra(intentAnimeRank) + val animeScore = intent.getStringExtra(intentAnimeScore) + val animeImageUrl = intent.getStringExtra(intentAnimeImageUrl) + + val animeEpisodes = intent.getStringExtra(intentAnimeEpisodes) + val animeStartDate = intent.getStringExtra(intentAnimeStartDate) + val animeEndDate = intent.getStringExtra(intentAnimeEndDate) + val animeUrl = intent.getStringExtra(intentAnimeUrl) + + val tvId: TextView = findViewById(R.id.tv_detail_id) + val tvTitle: TextView = findViewById(R.id.tv_detail_title) + val tvRank: TextView = findViewById(R.id.tv_detail_rank) + val tvScore: TextView = findViewById(R.id.tv_detail_score) + val ivImage: ImageView = findViewById(R.id.iv_detail_image) + + val tvEpisodes: TextView = findViewById(R.id.tv_episodes) + val tvStartDate: TextView = findViewById(R.id.tv_start_date) + val tvEndDate: TextView = findViewById(R.id.tv_end_date) + val tvUrl: TextView = findViewById(R.id.tv_url) + + tvId.text = animeId + tvTitle.text = animeTitle + tvRank.text = animeRank + tvScore.text = animeScore + Glide + .with(this) + .load(animeImageUrl) + .apply(RequestOptions().override(400)) + .into(ivImage) + + //using null as a string because it has been converted to a string before + tvEpisodes.text = if (animeEpisodes != "null") { + animeEpisodes + } else { + fieldIsNull() + } + + tvStartDate.text = animeStartDate + + tvEndDate.text = if (animeEndDate != "null") { + animeEndDate + } else { + fieldIsNull() + } + + tvUrl.text = animeUrl + + val anime: AnimeResponse? = getDataFromCache(animeId.toString()) + if (anime != null) { + showDetail(anime) + } else { + //taking the API's fields I want and displaying them + makeApiCall(baseUrl, animeId.toString()) + } + + } + + private fun getDataFromCache(animeId: String): AnimeResponse? { + val jsonAnime: String? = sharedPreferences.getString(animeId, null) + + return if (jsonAnime == null) { + null + } else { + val type: Type = object : TypeToken() {}.type + gson.fromJson(jsonAnime, type) + } + } + + private fun makeApiCall(BASE_URL: String, animeId: String) { + + val retrofit = Retrofit.Builder() + .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 { + 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 anime = response.body() //getting the AnimeResponse fields + saveList(anime) + showDetail(anime!!) + + } else { + showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call, t: Throwable) { + showError("API ERROR : onFailure") + } + + }) + } + + private fun showDetail(anime: AnimeResponse) { + //elements from AnimeResponse + val tvSynopsis: TextView = findViewById(R.id.tv_synopsis) + + tvSynopsis.text = anime.synopsis.toString() + + } + + fun showError(text: String) { + Toast.makeText(this, text, Toast.LENGTH_LONG).show() + } + + private fun fieldIsNull(): String { + return "Unknown" + } + + fun saveList(anime: AnimeResponse?) { + val jsonString: String = gson.toJson(anime) + + sharedPreferences + .edit() + .putString(anime?.mal_id.toString(), jsonString) + .apply() + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopMangaActivity.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopMangaActivity.kt new file mode 100644 index 0000000..0142018 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/DetailTopMangaActivity.kt @@ -0,0 +1,193 @@ +package xyz.adjutor.aniki.presentation.view + +import android.content.Context +import android.content.SharedPreferences +import android.os.Bundle +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +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 +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import xyz.adjutor.aniki.R +import xyz.adjutor.aniki.data.MangaApi +import xyz.adjutor.aniki.presentation.model.MangaResponse +import java.lang.reflect.Type + +class DetailTopMangaActivity : AppCompatActivity() { + + private var baseUrl = "https://api.jikan.moe/" + private lateinit var sharedPreferences: SharedPreferences + private val gson = GsonBuilder() + .setLenient() + .create() + + //used in the list + private val intentMangaTitle = "themangatitle" + private val intentMangaRank = "themangarank" + private val intentMangaScore = "themangascore" + private val intentMangaImageUrl = "themangaimageurl" + + //only used for the detail + private val intentMangaId = "themangaid" + private val intentMangaVolumes = "themangavolumes" + private val intentMangaStartDate = "themangastartdate" + private val intentMangaEndDate = "themangaenddate" + private val intentMangaUrl = "themangaurl" + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_detail_top_manga) + + sharedPreferences = this.getSharedPreferences("sp_manga", Context.MODE_PRIVATE) + + val mangaTitle = intent.getStringExtra(intentMangaTitle) + val mangaRank = intent.getStringExtra(intentMangaRank) + val mangaScore = intent.getStringExtra(intentMangaScore) + val mangaImageUrl = intent.getStringExtra(intentMangaImageUrl) + + val mangaId = intent.getStringExtra(intentMangaId) + val mangaVolumes = intent.getStringExtra(intentMangaVolumes) + val mangaStartDate = intent.getStringExtra(intentMangaStartDate) + val mangaEndDate = intent.getStringExtra(intentMangaEndDate) + val mangaUrl = intent.getStringExtra(intentMangaUrl) + + val tvTitle: TextView = findViewById(R.id.tv_detail_title) + val tvRank: TextView = findViewById(R.id.tv_detail_rank) + val tvScore: TextView = findViewById(R.id.tv_detail_score) + val ivImage: ImageView = findViewById(R.id.iv_detail_image) + + val tvId: TextView = findViewById(R.id.tv_detail_id) + val tvVolumes: TextView = findViewById(R.id.tv_volumes) + val tvStartDate: TextView = findViewById(R.id.tv_start_date) + val tvEndDate: TextView = findViewById(R.id.tv_end_date) + val tvUrl: TextView = findViewById(R.id.tv_url) + + tvTitle.text = mangaTitle + tvRank.text = mangaRank + tvScore.text = mangaScore + Glide + .with(this) + .load(mangaImageUrl) + .apply(RequestOptions().override(400)) + .into(ivImage) + + tvId.text = mangaId + + //using null as a string because it has been converted to a string before + tvVolumes.text = if (mangaVolumes != "null") { + mangaVolumes + } else { + fieldIsNull() + } + + tvStartDate.text = mangaStartDate + + tvEndDate.text = if (mangaEndDate != "null") { + mangaEndDate + } else { + fieldIsNull() + } + + tvUrl.text = mangaUrl + + val manga: MangaResponse? = 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): MangaResponse? { + val jsonManga: String? = sharedPreferences.getString(mangaId, null) + + return if (jsonManga == null) { + null + } else { + val type: Type = object : TypeToken() {}.type + gson.fromJson(jsonManga, type) + } + } + + private fun makeApiCall(BASE_URL: String, mangaId: String) { + + val retrofit = Retrofit.Builder() + .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 { + 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 MangaResponse fields + saveList(manga) + showDetail(manga!!) + + } else { + showError("API ERROR : is not successful") + } + } + + override fun onFailure(call: Call, t: Throwable) { + showError("API ERROR : onFailure") + } + + }) + } + + private fun showDetail(manga: MangaResponse) { + //elements from MangaResponse + 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 = if (manga.chapters != null) { + manga.chapters.toString() + } else { + fieldIsNull() + } + + tvSynopsis.text = manga.synopsis.toString() + + tvBackground.text = if (manga.background != null) { + manga.background.toString() + } else { + fieldIsNull() + } + + } + + fun showError(text: String) { + Toast.makeText(this, text, Toast.LENGTH_LONG).show() + } + + private fun fieldIsNull(): String { + return "Unknown" + } + + fun saveList(manga: MangaResponse?) { + val jsonString: String = gson.toJson(manga) + + sharedPreferences + .edit() + .putString(manga?.mal_id.toString(), jsonString) + .apply() + } +} \ No newline at end of file diff --git a/app/src/main/java/xyz/adjutor/aniki/presentation/view/HomePage.kt b/app/src/main/java/xyz/adjutor/aniki/presentation/view/HomePage.kt new file mode 100644 index 0000000..f2abca1 --- /dev/null +++ b/app/src/main/java/xyz/adjutor/aniki/presentation/view/HomePage.kt @@ -0,0 +1,38 @@ +package xyz.adjutor.aniki.presentation.view + +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 xyz.adjutor.aniki.R + +class HomePage : Fragment() { + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.home_page, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + view.findViewById