wykorzystanie języka kotlin do aplikacji na platformie android
TRANSCRIPT
1. DLACZEGO WYBRAŁEM KOTLINA?
CIEKAWOŚĆ
CODE REVIEW Z iOS
JETBRAINS - ZNANA MARKA
ROZMIAR APK I SZYBKA KOMPILACJA
class User(val name: String, age: Int?)
val name: String = “Damian” var isSuperUser: Boolean = true var pet: String? = null
public fun sayHi(name: String, times: Int = 3) { for (i in 1..times) { println(“Hi $name”) } } private fun getSome() : String { return “some” }
Kotlin
InputStream stream = ... if (stream != null) { stream.close(); }
stream.close()
stream?.close()
Java
Kotlin
public class Item { private String type;
public Item(String type) { this.type = type; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false;
Item item2 = (Item) o;
if (type != null ? !type.equals(item2.type) : item2.type != null) return false;
return true; }
@Override public int hashCode() { return type != null ? type.hashCode() : 0; }
@Override public String toString() { return "Item2{" + "type='" + type + '\'' + '}'; } } public class Article extends Item {
private Boolean isDeleted; private String id; private String category; private String published; private String publishedFormatted; private String firstPublished; private String updated; private String created; private List<Topic> topics; private String title; private String newsLifetime; private Integer newsValue; private List<Author> authors; private List<Resource> resources private Version version; private String vignette; private String displaySize; private Boolean live; private Boolean breaking; private Story story; private Boolean sponsored = false;
public Article(String type) { super(type); }
public Article(String type, Boolean isDeleted, String id, String category, String published, String publishedFormatted, String firstPublished, String updated, String created, List<Topic> topics, String title, String newsLifetime, Integer newsValue, List<Author> authors, List<Resource> resources, Version version, String vignette, String displaySize, Boolean live, Boolean breaking, Story story, Boolean sponsored) { super(type); this.isDeleted = isDeleted; this.id = id; this.category = category; this.published = published; this.publishedFormatted = publishedFormatted; this.firstPublished = firstPublished; this.updated = updated; this.created = created; this.topics = topics; this.title = title; this.newsLifetime = newsLifetime; this.newsValue = newsValue; this.authors = authors; this.resources = resources; this.version = version; this.vignette = vignette; this.displaySize = displaySize; this.live = live; this.breaking = breaking; this.story = story; this.sponsored = sponsored; }
public Boolean getIsDeleted() { return isDeleted; }
public void setIsDeleted(Boolean isDeleted) { this.isDeleted = isDeleted; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public String getPublished() { return published; }
public void setPublished(String published) { this.published = published; }
public String getPublishedFormatted() { return publishedFormatted; }
public void setPublishedFormatted(String publishedFormatted) { this.publishedFormatted = publishedFormatted; }
public String getFirstPublished() { return firstPublished; }
public void setFirstPublished(String firstPublished) { this.firstPublished = firstPublished; }
public String getUpdated() { return updated; }
public void setUpdated(String updated) { this.updated = updated; }
public String getCreated() { return created; }
public void setCreated(String created) { this.created = created; }
public List<Topic> getTopics() { return topics; }
public void setTopics(List<Topic> topics) { this.topics = topics; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getNewsLifetime() { return newsLifetime; }
public void setNewsLifetime(String newsLifetime) { this.newsLifetime = newsLifetime; }
public Integer getNewsValue() { return newsValue; }
public void setNewsValue(Integer newsValue) { this.newsValue = newsValue; }
public List<Author> getAuthors() { return authors; }
public void setAuthors(List<Author> authors) { this.authors = authors; }
public List<Resource> getResources() { return resources; }
public void setResources(List<Resource> resources) { this.resources = resources; }
public Version getVersion() { return version; }
public void setVersion(Version version) { this.version = version; }
public String getVignette() { return vignette; }
public void setVignette(String vignette) { this.vignette = vignette; }
public String getDisplaySize() { return displaySize; }
public void setDisplaySize(String displaySize) { this.displaySize = displaySize; }
public Boolean getLive() { return live; }
public void setLive(Boolean live) { this.live = live; }
public Boolean getBreaking() { return breaking; }
public void setBreaking(Boolean breaking) { this.breaking = breaking; }
public Story getStory() { return story; }
public void setStory(Story story) { this.story = story; }
public Boolean getSponsored() { return sponsored; }
public void setSponsored(Boolean sponsored) { this.sponsored = sponsored; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false;
Article article2 = (Article) o;
if (authors != null ? !authors.equals(article2.authors) : article2.authors != null) return false; if (breaking != null ? !breaking.equals(article2.breaking) : article2.breaking != null) return false; if (category != null ? !category.equals(article2.category) : article2.category != null) return false; if (created != null ? !created.equals(article2.created) : article2.created != null) return false; if (displaySize != null ? !displaySize.equals(article2.displaySize) : article2.displaySize != null) return false; if (firstPublished != null ? !firstPublished.equals(article2.firstPublished) : article2.firstPublished != null) return false; if (id != null ? !id.equals(article2.id) : article2.id != null) return false; if (isDeleted != null ? !isDeleted.equals(article2.isDeleted) : article2.isDeleted != null) return false; if (live != null ? !live.equals(article2.live) : article2.live != null) return false; if (newsLifetime != null ? !newsLifetime.equals(article2.newsLifetime) : article2.newsLifetime != null) return false; if (newsValue != null ? !newsValue.equals(article2.newsValue) : article2.newsValue != null) return false; if (published != null ? !published.equals(article2.published) : article2.published != null) return false; if (publishedFormatted != null ? !publishedFormatted.equals(article2.publishedFormatted) : article2.publishedFormatted != null) return false; if (resources != null ? !resources.equals(article2.resources) : article2.resources != null) return false; if (sponsored != null ? !sponsored.equals(article2.sponsored) : article2.sponsored != null) return false; if (story != null ? !story.equals(article2.story) : article2.story != null) return false; if (title != null ? !title.equals(article2.title) : article2.title != null) return false; if (topics != null ? !topics.equals(article2.topics) : article2.topics != null) return false; if (updated != null ? !updated.equals(article2.updated) : article2.updated != null) return false; if (version != null ? !version.equals(article2.version) : article2.version != null) return false; if (vignette != null ? !vignette.equals(article2.vignette) : article2.vignette != null) return false;
return true; }
@Override public int hashCode() { int result = super.hashCode(); result = 31 * result + (isDeleted != null ? isDeleted.hashCode() : 0); result = 31 * result + (id != null ? id.hashCode() : 0); result = 31 * result + (category != null ? category.hashCode() : 0); result = 31 * result + (published != null ? published.hashCode() : 0); result = 31 * result + (publishedFormatted != null ? publishedFormatted.hashCode() : 0); result = 31 * result + (firstPublished != null ? firstPublished.hashCode() : 0); result = 31 * result + (updated != null ? updated.hashCode() : 0); result = 31 * result + (created != null ? created.hashCode() : 0); result = 31 * result + (topics != null ? topics.hashCode() : 0); result = 31 * result + (title != null ? title.hashCode() : 0); result = 31 * result + (newsLifetime != null ? newsLifetime.hashCode() : 0); result = 31 * result + (newsValue != null ? newsValue.hashCode() : 0); result = 31 * result + (authors != null ? authors.hashCode() : 0); result = 31 * result + (resources != null ? resources.hashCode() : 0); result = 31 * result + (version != null ? version.hashCode() : 0); result = 31 * result + (vignette != null ? vignette.hashCode() : 0); result = 31 * result + (displaySize != null ? displaySize.hashCode() : 0); result = 31 * result + (live != null ? live.hashCode() : 0); result = 31 * result + (breaking != null ? breaking.hashCode() : 0); result = 31 * result + (story != null ? story.hashCode() : 0); result = 31 * result + (sponsored != null ? sponsored.hashCode() : 0); return result; }
@Override public String toString() { return "Article{" +
open class Item(val type: ItemTypeEnum): Serializable
data class Article(type: ItemTypeEnum, val isDeleted: Boolean?, val id: String, val category: String, val published: String, var publishedFormatted: String?, val firstPublished: String?, val updated: String, val created: String, val topics: Array<Topic>, val title: String, val newsLifetime: String, val newsValue: Int, val authors: Array<Author>, val resources: Array<Resource>, val version: Version?, val vignette: String?, val displaySize: String, val live: Boolean?, val breaking: Boolean?, val story: Story?, val sponsored: Boolean = false ) : Item(type)
Java - 354 Kotlin - 25
data class User(val name: String, val surname: String, val age: Int)
val user = User(“Damian”, “Petla”, 33) val user2 = user.copy(age = 30, name = “Adam”) println(user) println(user2)
User(name=Damian, surname=Petla, age=33) User(name=Adam, surname=Petla, age=30)
Kotlin
Log
3. CO NAJBARDZIEJ MI SIĘ PODOBA W KOTLINIE?
BEZPIECZNY KOD
PROSTE DEFINICJE KLAS
LAMBDA STRING TEMPLATES
Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println(“Hello! " + v.isOpaque()); } });
Java
Kotlin
val button = findViewById(R.id.button) as Button button.setOnClickListener { println("Hello! ${it.isOpaque()}") }
3. CO NAJBARDZIEJ MI SIĘ PODOBA W KOTLINIE?
BEZPIECZNY KOD
PROSTE DEFINICJE KLAS
LAMBDA STRING TEMPLATES
SMART CAST
int l = 0; if (obj instanceof String) { l = ((String)obj).length; }
Java
Kotlin
var l = 0 if (obj is String) { l = obj.length }
val l : Int = (obj as? String)?.length ?: 0
3. CO NAJBARDZIEJ MI SIĘ PODOBA W KOTLINIE?
BEZPIECZNY KOD
PROSTE DEFINICJE KLAS
LAMBDA STRING TEMPLATES
SMART CAST
FUNCTION EXTENSIONS
String utcDate = ... String formatted = Utilites.toPrettyDate(utcDate); textView.setText(formatted);
Java
Kotlin
textView.setText(utcDate.toPrettyDate())
fun String.toPrettyDate(): String { return “$this is formatted now” }
4. CZY SĄ JAKIEŚ PROBLEMY Z KOTLINEM?
SZYBKA ODPOWIEDŹ NA ZGŁOSZONE BŁEDY
JETBRAINS AKTYWNY NA STACK OVERFLOW
220 BUGÓW NAPRAWIONYCH W WERSJI M11(0.11.91)
HTTP://KOTLINLANG.ORG/DOCS/TUTORIALS/KOTLIN-ANDROID.HTML
HTTPS://DOCS.GOOGLE.COM/DOCUMENT/D/1RES3EP-HJXWA8KZI0YQDBEHCQTT29HG8P44AA9W0DM8
7. CZY MAM JAKIEŚ PORADY NA POCZĄTEK?
!! ZAMIAST ?
CZĘSTO WRACAJ DO DOKUMENTACJI
UDZIELAJ SIĘ W COMMUNITY