aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/xyz/adjutor/aniki/anime
diff options
context:
space:
mode:
authorClyhtsuriva <aimeric@adjutor.xyz>2021-03-02 09:13:54 +0100
committerClyhtsuriva <aimeric@adjutor.xyz>2021-03-02 09:13:54 +0100
commitf9f079eea806454d49d6088089511d72e1c28f86 (patch)
treeb0eaf1019c094b9857c94a2e245682ebc976f0b0 /app/src/main/java/xyz/adjutor/aniki/anime
parent9e1f3503c1a80d7e108f60f9065ae08abb7c2b3f (diff)
Cleaning up the code.
Diffstat (limited to 'app/src/main/java/xyz/adjutor/aniki/anime')
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/anime/RestAnimeResponse.kt3
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/anime/topanime/DetailTopAnimeActivity.kt36
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnime.kt10
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimeAdapter.kt35
-rw-r--r--app/src/main/java/xyz/adjutor/aniki/anime/topanime/TopAnimePage.kt50
5 files changed, 78 insertions, 56 deletions
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