package net.lacolaco.smileessence import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import androidx.annotation.StringRes import android.widget.Toast import com.squareup.leakcanary.LeakCanary class Application : android.app.Application() { private lateinit var dbOpenHelper: DbOpenHelper val db get() = dbOpenHelper.writableDatabase!! override fun onCreate() { super.onCreate() if (LeakCanary.isInAnalyzerProcess(this)) return LeakCanary.install(this) dbOpenHelper = DbOpenHelper(this) instance = this World.load() } companion object { lateinit var instance: Application private set fun toast(@StringRes id: Int) { Toast.makeText(instance, id, Toast.LENGTH_LONG).show() } fun toast(text: String) { Toast.makeText(instance, text, Toast.LENGTH_LONG).show() } var currentWorld: World? = null } private class DbOpenHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) { override fun onCreate(db: SQLiteDatabase) { // Schema v1 db.execSQL(""" CREATE TABLE profiles ( id INTEGER PRIMARY KEY, oauth_token TEXT NOT NULL, oauth_token_secret TEXT NOT NULL, screen_name TEXT NOT NULL, profile_image_url TEXT NOT NULL, theme INTEGER NOT NULL, theme_color INTEGER NOT NULL, page_infos TEXT NOT NULL ) """) onUpgrade(db, 1, DATABASE_VERSION) } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { if (oldVersion < 2) { db.execSQL(""" ALTER TABLE profiles ADD COLUMN twitter_event_stream_endpoint TEXT """) db.execSQL(""" UPDATE profiles SET twitter_event_stream_endpoint="https://userstream.herokuapp.com/stream" """) } } companion object { private const val DATABASE_NAME = "main.db" private const val DATABASE_VERSION = 2 } } }