Kotlin Blueprints

Book description

Get to know the building blocks of Kotlin and best practices when using quality world-class applications

About This Book

  • Learn to build exciting and scalable Android and web applications (both the server-side and client-side parts) with your Kotlin skills
  • Dive into the great ecosystem of Kotlin frameworks and libraries through projects that you'll build using this book
  • This project-based guide contains clear instructions to help you extend your applications across a wide domain

Who This Book Is For

This practical guide is for programmers who are already familiar with Kotlin. If you are familiar with Kotlin and want to put your knowledge to work, then this is the book for you. Kotlin programming knowledge is a must.

What You Will Learn

  • See how Kotlin's power and versatility make it a great choice to create applications across various platforms, and how it delivers business and technology benefits
  • Write a robust web applications using Kotlin with Spring Boot
  • Write Android applications with ease using Kotlin
  • Write rich desktop applications in Kotlin
  • Learn how Kotlin can generate Javascript and how this can be used on client side and server side development
  • Understand how native applications can be written with Kotlin/Native
  • Learn the practical aspects of programming in each of the applications

In Detail

Kotlin is a powerful language that has applications in a wide variety of fields. It is a concise, safe, interoperable, and tool-friendly language. The Android team has also announced first-class support for Kotlin, which is an added boost to the language. Kotlin's growth is fueled through carefully designed business and technology benefits.

The collection of projects demonstrates the versatility of the language and enables you to build standalone applications on your own. You'll build comprehensive applications using the various features of Kotlin. Scale, performance, and high availability lie at the heart of the projects, and the lessons learned throughout this book.

You'll learn how to build a social media aggregator app that will help you efficiently track various feeds, develop a geospatial webservice with Kotlin and Spring Boot, build responsive web applications with Kotlin, build a REST API for a news feed reader, and build a server-side chat application with Kotlin.

It also covers the various libraries and frameworks used in the projects. Through the course of building applications, you'll not only get to grips with the various features of Kotlin, but you'll also discover how to design and prototype professional-grade applications.

Style and approach

Each chapter is independent and focuses on a unique technology, where Kotlin is used to build an example application. Together the chapters cover a full spectrum.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. The Power of Kotlin
    1. Kotlin – a better Java
      1. Why not other languages?
      2. Why Kotlin?
        1. Concise yet expressive code
          1. Enhanced robustness
        2. Excellent IDE support from day one
          1. Beyond being a better Java
      3. Kotlin is in the winning camp
      4. Go native strategy
    2. Kotlin's winning strategy
    3. Summary
  3. Geospatial Messenger – Spring Boot
    1. Why Spring Boot?
      1. Leveraging Kotlin in Spring Boot applications
        1. Extension functions
        2. Constructor injection
        3. Leveraging Null safety
        4. Functional bean declaration DSL
    2. Let's build our geospatial messenger
      1. Preview
      2. Features
    3. Architecture
      1. Setting up the IDE
        1. Creating a project
        2. Project structure
    4. Setting up the build script
      1. Adding Kotlin dependency
      2. Adding Spring Boot dependencies
      3. Exposed library
        1. Gradle dependency
        2. Defining the table
        3. Connecting to the database
        4. CRUD operations
      4. Explaining PostGIS
        1. Installation
        2. Gradle dependency
      5. Object mapping
        1. Gradle dependency
      6. Completing the Gradle script
    5. Coding the application
      1. Frontend
        1. HTML
        2. CSS
        3. JavaScript
          1. Rendering the map
          2. Plotting messages on the map
          3. Listening to message saved events
          4. Complete JavaScript
        4. Application class
      2. Backend
        1. Application configuration
        2. Data classes
        3. Exposed integration
          1. Exposed objects
          2. Extras for geospatial support
        4. Service/Controller
        5. Repository
        6. CrudRepository
        7. Event broadcaster
        8. Extension functions
    6. Testing
      1. Gradle dependency
      2. Test cases
    7. Pro-tips
      1. All-open compiler plugin
      2. Spring starter
      3. Playing with the Java to Kotlin converter
      4. Migrating to Kotlin
        1. Should I rewrite the existing Java code in Kotlin?
        2. What if I want to rewrite Java to Kotlin?
    8. Summary
  4. Social Media Aggregator Android App
    1. Setting up Kotlin
      1. Setting up the Kotlin plugin for Android Studio
      2. Getting started
        1. Converting Java code to Kotlin code
    2. Creating a social media aggregator
      1. Using datatypes and conversion
      2. String interpolation
        1. String utility methods
      3. Classes
        1. Constructors
        2. Data classes
        3. Inheritance
        4. Singletons
      4. lateinit versus lazy initialization
        1. lateinit
        2. The lazy property
      5. Control flow
        1. The when() expression
      6. Getters and setters
        1. Declaring a property
      7. Interfaces
      8. Kotlin Android extension
        1. Ditching the findViewById() method
        2. View extensions
        3. Fragment LayoutInflater extension
        4. The Standard.kt function
          1. The with() function
          2. The apply() function
          3. The let() function
      9. Functions in Kotlin
        1. Single-expression function
        2. Inline function
        3. Default and named parameters
          1. Default parameter
          2. Named parameter
        4. Destructing declaration
      10. Android context
      11. Null-Safety
        1. Why Kotlin is called null-safe
        2. Safe call operator (?.)
        3. Elvis operator (?:)
        4. Force unwrap (!!)
      12. Smart casts
        1. The is and !is operators
        2. The as operator
      13. Companion object
        1. Fragment instantiation using companion objects
      14. Dealing with constants
      15. Object expressions and declarations
      16. Delegated properties
        1. Dealing with Shared Preferences
      17. Setting up an item click on RecyclerView
      18. Anko - Kotlin's buddy for Android
        1. Setting up Anko
        2. Displaying toast()
        3. Starting an activity made easy
        4. Anko layout
    3. Summary
  5. Weather App Using Kotlin for JavaScript
    1. Creating your first Kotlin and JavaScript project
      1. Choosing an IDE 
      2. Creating a project
      3. Creating an HTML page
      4. Creating a Main.kt file
      5. Running the project
    2. Developing a weather forecast web app
      1. Creating a UI with dummy data
        1. Simple HTML approach
      2. Creating UI using Kotlin
        1. What is DSL?
        2. Using Kotlinx.html
      3. Refactoring the HTML code using DSL
        1. Calling a weather API
          1. Reading data from input elements
          2. Data classes
        2. Showing data to the user
        3. Showing weather details
          1. Named parameters
          2. Extension functions
        4. Giving final touches
          1. Adding CSS
    3. Interoperability with JavaScript
    4. Summary
  6. Chat Application with Server-Side JavaScript Generation
    1. Creating our first Node.js app using Kotlin
      1. Choosing an IDE
      2. Installing Node.js
      3. Installing the Node.js plugin
      4. Creating a project
    2. Creating a chat application
      1. Setting up the Node.js server
      2. Specifying the output files
        1. Examining the compilation output
      3. Specifying the router
      4. Starting the node server
      5. Creating a login page
        1. Creating an index.ejs file
        2. Using DSL
        3. Using kotlinx.html
      6. Lambda functions
      7. Reading the nickname
      8. Passing nickname to the server
        1. Smart cast
        2. Registering a callback
      9. Establishing a socket connection
        1. Setting up Socket.IO
        2. Listening to events
        3. Emitting the event
        4. Incrementing and decrementing operator overloading
      10. Showing a list of online users
        1. Using the data class
        2. Using the Pair class
        3. Iterating list
      11. Sending and receiving a message
        1. Null safety
        2. Force unwraps
        3. Using the let function
        4. Named parameter
      12. Disconnecting a socket
      13. Styling the page using CSS
    3. Summary
  7. News Feed – REST API
    1. What is REST?
    2. What is Ktor?
      1. Why Ktor?
        1. Understanding unopinionated applications
        2. Asynchronous nature
        3. Highly testable
      2. Deploying the Ktor app
    3. Let's build our news application
      1. News provider
        1. Fetching news sources
        2. Fetching news articles
      2. Preview
      3. Features
      4. Architecture
      5. Setting up the IDE
        1. Prerequisites
        2. Creating the project
        3. Project structure
        4. Deploying
          1. Deploying in IntelliJ IDEA
    4. Setting up the build script
      1. Adding Kotlin dependency
      2. Adding Ktor dependencies
      3. Configuring logging
      4. Adding Fuel
        1. What is Fuel?
        2. Adding Gradle dependency
        3. Quick sample
          1. Asynchronous mode
          2. Blocking mode
      5. Completing Gradle script
    5. Let's code!
      1. Application configuration
        1. Deployment block
        2. Application block
      2. Application main
        1. Installing routing
          1. Simple routing
          2. Modular routing
          3. Understanding route paths
          4. Path parameters
    6. Testing
      1. Adding Gradle dependency
      2. Testing the application
        1. Testing the index URL
        2. Testing JSON using Postman
          1. Testing news sources using Postman
          2. Testing news sources using Postman
    7. Summary
  8. CSV Reader in Kotlin Native
    1. What is Kotlin Native?
      1. Target platforms
      2. Multiplatform Kotlin
    2. Installing Kotlin Native
      1. Installing from the source
      2. Installing from binaries
      3. Testing the installation
      4. Memory management
    3. Building our app
      1. Writing the main function
      2. Reading command-line arguments
      3. Opening the file
      4. Reading the file contents
      5. Counting unique entries
        1. Converting to Kotlin string
        2. Splitting strings
        3. Printing the result
      6. Complete code
      7. Running the program
        1. Understanding the dataset
        2. Compiling the program
        3. Executing the program
    4. Multiplatform Kotlin
      1. Project structure
        1. Common module
        2. Platform module
        3. Regular module
        4. Overview of the dependency structure
      2. Setting up a multiplatform project
        1. Creating a multiplatform project with an IDE
        2. Creating a multiplatform project without an IDE
    5. Summary
  9. Dictionary Desktop Application - TornadoFX
    1. Introducing TornadoFX
      1. Java-based desktop frameworks
      2. TornadoFX special additions
        1. Creating a type-safe UI versus FXML
        2. Type-safe styling
        3. Other improvements
    2. Let's build our dictionary application
      1. What we will build
      2. Words API
        1. Definition API
      3. Features of our application
      4. Setting up the IDE
        1. Prerequisites
        2. Installing the TornadoFX plugin
        3. Creating the project
        4. Project structure
    3. Let's code!
      1. Application class
      2. Type-safe CSS
      3. Dependency injection
      4. Property delegate
      5. Consuming the REST API
        1. Defining the Model
          1. Implementing the JSONModel interface
        2. REST client
          1. Configuring the client
        3. Controllers
      7. Layouts
        1. VBox layout
        2. HBox layout
        3. Other layouts
        4. Forms
      8. Background operations
      9. Printing the result
      10. Complete View
    4. Launching the application
    5. Summary

Product information

  • Title: Kotlin Blueprints
  • Author(s): Ashish Belagali, Hardik Trivedi, Akshay Chordiya
  • Release date: December 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788390804