From f9f079eea806454d49d6088089511d72e1c28f86 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Tue, 2 Mar 2021 09:13:54 +0100 Subject: Cleaning up the code. --- .../aniki/manga/topmanga/DetailTopMangaActivity.kt | 40 ++++++++++++---------- 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'app/src/main/java/xyz/adjutor/aniki/manga/topmanga/DetailTopMangaActivity.kt') 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() {}.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 { - 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 + 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) @@ -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 -- cgit v1.2.3