Tuesday, July 23, 2019

android kotlin - Room TypeConverter date example

MainActivity.kt

package com.example.roomexamples

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread
import java.text.DateFormat
import java.util.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val room = RoomSingleton.getInstance(this)
        textView.movementMethod = ScrollingMovementMethod()


        // Insert some products in database
        btnInsert.setOnClickListener {
            val url = "https://tradeogre.com/api/v1/history/BTC-KRB"

            val request = JsonArrayRequest(
                Request.Method.GET,
                url,
                null,
                Response.Listener{
                    val list = arrayListOf<History>()

                    for (i in 0 until it.length()){
                        val obj =  it.getJSONObject(i)

                        val timeMillis = obj.getLong("date")
                        val type = obj.getString("type")
                        val price = obj.getString("price").toBigDecimal()
                        val quantity = obj.getString("quantity").toBigDecimal()

                        val history = History(null,Date(timeMillis),type,price,quantity)
                        list.add(history)
                    }

                    doAsync {
                        room.historyDao().insertAll(list)
                        uiThread {
                            toast("done")
                        }
                    }


                },Response.ErrorListener {
                    toast(it.toString())
                }
            )

            // Add the request to the RequestQueue.
            Volley.newRequestQueue(this).add(request)
        }

        btnSelect.setOnClickListener {
            doAsync {
                val list = room.historyDao().allHistory()

                uiThread {
                    textView.text = ""
                    list.forEach {
                        textView.append(it.date.medium)
                        textView.append("\nType: " + it.type)
                        textView.append("\nPrice: " + it.price)
                        textView.append("\nQuantity: " + it.quantity + "\n\n")
                    }
                }
            }
        }
    }
}


// Extension property to format date
val Date.medium:String
get() = DateFormat.getDateInstance(DateFormat.MEDIUM).format(this)
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        android:text=""
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnInsert" />
    <Button
        android:id="@+id/btnInsert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Insert"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <Button
        android:id="@+id/btnSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Select"
        app:layout_constraintStart_toEndOf="@+id/btnInsert"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
RoomSingleton.kt

package com.example.roomexamples

import android.content.Context
import androidx.room.*

@Database(entities = arrayOf(History::class), version = 1, exportSchema = false)
@TypeConverters(Converter::class)

abstract class RoomSingleton : RoomDatabase(){
    abstract fun historyDao(): HistoryDao
    companion object{
        private var INSTANCE: RoomSingleton? = null
        fun getInstance(context: Context): RoomSingleton{
            if (INSTANCE == null){
                INSTANCE = Room.databaseBuilder(
                    context,
                    RoomSingleton::class.java,
                    "roomdb")
                    .build()
            }
            return INSTANCE as RoomSingleton
        }
    }
}
RoomDao.kt

package com.example.roomexamples

import androidx.room.*
import java.math.BigDecimal
import java.util.*


@Entity(tableName = "historyTbl")
data class History(
    @PrimaryKey(autoGenerate = true)
    var id:Long?=null,
    var date:Date,
    var type:String,
    var price:BigDecimal,
    var quantity:BigDecimal
)


@Dao
interface HistoryDao {
    @Query("SELECT * FROM historyTbl")
    fun allHistory():List<History>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(history:History)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAll(histories:List<History>)
}
Converter.kt

package com.example.roomexamples

import androidx.room.TypeConverter
import java.math.BigDecimal
import java.util.*

class Converter{
    companion object{
        @TypeConverter
        @JvmStatic
        fun fromBigDecimal(value:BigDecimal):String{
            return value.toString()
        }

        @TypeConverter
        @JvmStatic
        fun toBigDecimal(value:String):BigDecimal{
            return value.toBigDecimal()
        }

        @TypeConverter
        @JvmStatic
        fun toDate(value:Long):Date{
            //If your result always returns 1970, then comment this line
            //val date =  Date(value)

            //If your result always returns 1970, then uncomment this line
            val date = Date(value*1000L)

            return date
        }

        @TypeConverter
        @JvmStatic
        fun fromDate(date:Date):Long{
            return date.time
        }
    }
}
app build.gradle [required]

apply plugin: 'kotlin-kapt'

dependencies {
    // Room
    def room_version = "2.2.0-alpha01"
    implementation "androidx.room:room-runtime:$room_version"
    kapt 'androidx.room:room-compiler:2.2.0-alpha01'

    // Anko Commons
    implementation "org.jetbrains.anko:anko-commons:0.10.8"

    // Volley network library
    implementation 'com.android.volley:volley:1.1.1'
}

More android examples