Skip to content

Commit

Permalink
json to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlind committed Aug 18, 2022
1 parent ff4f379 commit 41d964e
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ android {
}

dependencies {
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.squareup.picasso:picasso:2.8'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/com/mojo/myapplication/Breed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mojo.myapplication

import com.google.gson.annotations.SerializedName


data class Breed(

@SerializedName("weight") var weight: Weight? = Weight(),
@SerializedName("id") var id: String? = null,
@SerializedName("name") var name: String? = null,
@SerializedName("cfa_url") var cfaUrl: String? = null,
@SerializedName("vetstreet_url") var vetstreetUrl: String? = null,
@SerializedName("vcahospitals_url") var vcahospitalsUrl: String? = null,
@SerializedName("temperament") var temperament: String? = null,
@SerializedName("origin") var origin: String? = null,
@SerializedName("country_codes") var countryCodes: String? = null,
@SerializedName("country_code") var countryCode: String? = null,
@SerializedName("description") var description: String? = null,
@SerializedName("life_span") var lifeSpan: String? = null,
@SerializedName("indoor") var indoor: Int? = null,
@SerializedName("lap") var lap: Int? = null,
@SerializedName("alt_names") var altNames: String? = null,
@SerializedName("adaptability") var adaptability: Int? = null,
@SerializedName("affection_level") var affectionLevel: Int? = null,
@SerializedName("child_friendly") var childFriendly: Int? = null,
@SerializedName("dog_friendly") var dogFriendly: Int? = null,
@SerializedName("energy_level") var energyLevel: Int? = null,
@SerializedName("grooming") var grooming: Int? = null,
@SerializedName("health_issues") var healthIssues: Int? = null,
@SerializedName("intelligence") var intelligence: Int? = null,
@SerializedName("shedding_level") var sheddingLevel: Int? = null,
@SerializedName("social_needs") var socialNeeds: Int? = null,
@SerializedName("stranger_friendly") var strangerFriendly: Int? = null,
@SerializedName("vocalisation") var vocalisation: Int? = null,
@SerializedName("experimental") var experimental: Int? = null,
@SerializedName("hairless") var hairless: Int? = null,
@SerializedName("natural") var natural: Int? = null,
@SerializedName("rare") var rare: Int? = null,
@SerializedName("rex") var rex: Int? = null,
@SerializedName("suppressed_tail") var suppressedTail: Int? = null,
@SerializedName("short_legs") var shortLegs: Int? = null,
@SerializedName("wikipedia_url") var wikipediaUrl: String? = null,
@SerializedName("hypoallergenic") var hypoallergenic: Int? = null,
@SerializedName("reference_image_id") var referenceImageId: String? = null

)
11 changes: 11 additions & 0 deletions app/src/main/java/com/mojo/myapplication/Cat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mojo.myapplication

import com.google.gson.annotations.SerializedName

data class Cat(
@SerializedName("breeds") var breeds: ArrayList<Breed> = arrayListOf(),
@SerializedName("id") var id: String,
@SerializedName("url") var url: String,
@SerializedName("width") var width: Int,
@SerializedName("height") var height: Int
)
119 changes: 119 additions & 0 deletions app/src/main/java/com/mojo/myapplication/CatJson.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.mojo.myapplication

val catJson = """
[
{
"breeds": [],
"id": "76j",
"url": "https://cdn2.thecatapi.com/images/76j.jpg",
"width": 500,
"height": 500
},
{
"breeds": [],
"id": "ap2",
"url": "https://cdn2.thecatapi.com/images/ap2.jpg",
"width": 640,
"height": 415
},
{
"breeds": [],
"id": "c84",
"url": "https://cdn2.thecatapi.com/images/c84.jpg",
"width": 900,
"height": 608
},
{
"breeds": [],
"id": "cek",
"url": "https://cdn2.thecatapi.com/images/cek.png",
"width": 765,
"height": 1024
},
{
"breeds": [],
"id": "MTU3NzIzNw",
"url": "https://cdn2.thecatapi.com/images/MTU3NzIzNw.jpg",
"width": 1280,
"height": 960
},
{
"breeds": [
{
"weight": {
"imperial": "6 - 15",
"metric": "3 - 7"
},
"id": "birm",
"name": "Birman",
"cfa_url": "http://cfa.org/Breeds/BreedsAB/Birman.aspx",
"vetstreet_url": "http://www.vetstreet.com/cats/birman",
"vcahospitals_url": "https://vcahospitals.com/know-your-pet/cat-breeds/birman",
"temperament": "Affectionate, Active, Gentle, Social",
"origin": "France",
"country_codes": "FR",
"country_code": "FR",
"description": "The Birman is a docile, quiet cat who loves people and will follow them from room to room. Expect the Birman to want to be involved in what you’re doing. He communicates in a soft voice, mainly to remind you that perhaps it’s time for dinner or maybe for a nice cuddle on the sofa. He enjoys being held and will relax in your arms like a furry baby.",
"life_span": "14 - 15",
"indoor": 0,
"lap": 1,
"alt_names": "Sacred Birman, Sacred Cat Of Burma",
"adaptability": 5,
"affection_level": 5,
"child_friendly": 4,
"dog_friendly": 5,
"energy_level": 3,
"grooming": 2,
"health_issues": 1,
"intelligence": 3,
"shedding_level": 3,
"social_needs": 4,
"stranger_friendly": 3,
"vocalisation": 1,
"experimental": 0,
"hairless": 0,
"natural": 0,
"rare": 0,
"rex": 0,
"suppressed_tail": 0,
"short_legs": 0,
"wikipedia_url": "https://en.wikipedia.org/wiki/Birman",
"hypoallergenic": 0,
"reference_image_id": "HOrX5gwLS"
}
],
"id": "HOrX5gwLS",
"url": "https://cdn2.thecatapi.com/images/HOrX5gwLS.jpg",
"width": 1376,
"height": 814
},
{
"breeds": [],
"id": "BjXI8y9gY",
"url": "https://cdn2.thecatapi.com/images/BjXI8y9gY.jpg",
"width": 2048,
"height": 1536
},
{
"breeds": [],
"id": "BBWwHY9Qg",
"url": "https://cdn2.thecatapi.com/images/BBWwHY9Qg.jpg",
"width": 225,
"height": 225
},
{
"breeds": [],
"id": "ng-IfW1Lq",
"url": "https://cdn2.thecatapi.com/images/ng-IfW1Lq.jpg",
"width": 600,
"height": 388
},
{
"breeds": [],
"id": "mDK29Po8k",
"url": "https://cdn2.thecatapi.com/images/mDK29Po8k.jpg",
"width": null,
"height": null
}
]
""".trimIndent()
82 changes: 23 additions & 59 deletions app/src/main/java/com/mojo/myapplication/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.widget.*

import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.squareup.picasso.Picasso

class MainActivity : AppCompatActivity() {
Expand All @@ -18,84 +20,46 @@ class MainActivity : AppCompatActivity() {

content = findViewById(R.id.content)

val dog1 = "https://www.freepnglogos.com/uploads/dog-png/bow-wow-gourmet-dog-treats-are-healthy-natural-low-4.png"
val dog2 = "http://pngimg.com/uploads/dog/dog_PNG50375.png"
val dog3 = "https://ichef.bbci.co.uk/news/976/cpsprodpb/17638/production/_124800859_gettyimages-817514614.jpg"
val cat1 = "https://w7.pngwing.com/pngs/174/600/png-transparent-cat-animal-lovely-cat.png"
val cats = Gson().fromJson<List<Cat>>(catJson)

val animals = listOf(
Dog("Bubi", dog1, "Mosha: 6 muaj", "Bubi eshte nje Laborador Retriever, shume i embel."),
Dog("Reksi",dog2 , "Mosha: 1 vjet", "Reksi eshte nje Kerry Beagle dhe i ka shume qef lojrat."),
Dog("Tokyo", dog3 , "Mosha: 5 vjet", "Tokyo eshte nje Golden Retriever dhe i do te gjithe."),
Cat("Lara",cat1 , "Mosha: 3 muaj", "Lara eshte nje Munchkin. Eshte lozonjare dhe e dashur.", true)
)


for (animal in animals) {
for (cat in cats) {
val animalItem = layoutInflater.inflate(R.layout.animal_item, null)


val animalNameText = animalItem.findViewById<TextView>(R.id.animalNameText)
val animalImage = animalItem.findViewById<ImageView>(R.id.animalImage)
val descriptionText = animalItem.findViewById<TextView>(R.id.descriptionText)
val ageText = animalItem.findViewById<TextView>(R.id.ageText)
val cuteText = animalItem.findViewById<TextView>(R.id.isCute)
val likeButoni= animalItem.findViewById<ImageView>(R.id.likeButton)
val likeButoni = animalItem.findViewById<ImageView>(R.id.likeButton)
val tekstiTeButoni = animalItem.findViewById<TextView>(R.id.tekstiTeButoni)

Picasso.get().load(cat.url).into(animalImage)

Picasso.get().load(animal.image).into(animalImage)

likeButoni.setOnClickListener {
if (likeButoni.isClickable){
tekstiTeButoni.visibility = View.VISIBLE
}
likeButoni.setOnClickListener {
if (likeButoni.isClickable) {
tekstiTeButoni.visibility = View.VISIBLE
}
}

animalImage.setOnClickListener {
if (animalImage.isClickable){
ageText.visibility =View.VISIBLE
descriptionText.visibility =View.VISIBLE
animalNameText.visibility =View.VISIBLE
if (animal is Cat && animal.isCute) {
cuteText.visibility = View.VISIBLE
}
if (animalImage.isClickable) {
ageText.visibility = View.VISIBLE
descriptionText.visibility = View.VISIBLE
animalNameText.visibility = View.VISIBLE
// if (cat is Cat && cat.isCute) { BREED
// cuteText.visibility = View.VISIBLE
// }
}
}



animalNameText.text = animal.name
descriptionText.text = animal.description
ageText.text = animal.age
animalNameText.text = cat.id
//descriptionText.text = cat.description BREED
//ageText.text = cat.age BREED

content.addView(animalItem)
}
}
}




}

open class Animal(
val name: String,
var image: String,
val age: String,
val description: String,
)

class Dog(
name: String,
image: String,
age: String,
description: String
) : Animal(name, image, age, description)

class Cat(
name: String,
image: String,
age: String,
description: String,
val isCute: Boolean
) : Animal(name, image, age, description)
internal inline fun <reified T> Gson.fromJson(json: String) =
fromJson<T>(json, object : TypeToken<T>() {}.type)
11 changes: 11 additions & 0 deletions app/src/main/java/com/mojo/myapplication/Weight.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mojo.myapplication

import com.google.gson.annotations.SerializedName


data class Weight(

@SerializedName("imperial") var imperial: String? = null,
@SerializedName("metric") var metric: String? = null

)

0 comments on commit 41d964e

Please sign in to comment.