aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Changelog.kt98
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt26
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/FragmentUtils.kt24
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/IIconUtils.kt19
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Kotterknife.kt137
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt9
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt103
7 files changed, 17 insertions, 399 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Changelog.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Changelog.kt
deleted file mode 100644
index 14a095a1..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Changelog.kt
+++ /dev/null
@@ -1,98 +0,0 @@
-package com.pitchedapps.frost.utils
-
-import android.content.Context
-import android.content.res.XmlResourceParser
-import android.os.Handler
-import android.support.annotation.LayoutRes
-import android.support.annotation.NonNull
-import android.support.annotation.XmlRes
-import android.support.v4.app.FragmentActivity
-import android.support.v7.widget.RecyclerView
-import android.view.LayoutInflater
-import android.view.View
-import android.view.ViewGroup
-import android.widget.TextView
-import com.afollestad.materialdialogs.MaterialDialog
-import com.pitchedapps.frost.R
-import org.xmlpull.v1.XmlPullParser
-import java.util.*
-
-
-/**
- * Created by Allan Wang on 2017-05-28.
- */
-class Changelog {
- companion object {
- fun show(@NonNull activity: FragmentActivity, @XmlRes xmlRes: Int = R.xml.changelog) {
- val mHandler = Handler()
- Thread(Runnable {
- val items = parse(activity, xmlRes)
- mHandler.post(object : TimerTask() {
- override fun run() {
- MaterialDialog.Builder(activity)
- .title(R.string.changelog)
- .positiveText(R.string.great)
- .adapter(ChangelogAdapter(items), null)
- .show()
- }
- })
- }).start()
- }
- }
-}
-
-private class ChangelogAdapter(val items: List<Pair<String, ChangelogType>>) : RecyclerView.Adapter<ChangelogAdapter.ChangelogVH>() {
-
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ChangelogVH(LayoutInflater.from(parent.context)
- .inflate(getLayout(viewType), parent, false))
-
- private fun getLayout(position: Int) = items[position].second.layout
-
- override fun onBindViewHolder(holder: ChangelogVH, position: Int) {
- holder.text.text = items[position].first
- }
-
- override fun getItemId(position: Int) = position.toLong()
-
- override fun getItemViewType(position: Int) = position
-
- override fun getItemCount() = items.size
-
- internal class ChangelogVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
- val text: TextView = itemView.findViewById(R.id.changelog_text) as TextView
- }
-}
-
-private fun parse(context: Context, @XmlRes xmlRes: Int): List<Pair<String, ChangelogType>> {
- val items = mutableListOf<Pair<String, ChangelogType>>()
- context.resources.getXml(xmlRes).use {
- parser ->
- var eventType = parser.eventType
- while (eventType != XmlPullParser.END_DOCUMENT) {
- if (eventType == XmlPullParser.START_TAG)
- ChangelogType.values.any { it.add(parser, items) }
- eventType = parser.next()
- }
- }
- return items
-}
-
-private enum class ChangelogType(val tag: String, val attr: String, @LayoutRes val layout: Int) {
- TITLE("title", "version", R.layout.changelog_title),
- ITEM("item", "text", R.layout.changelog_content);
-
- companion object {
- val values = values()
- }
-
- /**
- * Returns true if tag matches; false otherwise
- */
- fun add(parser: XmlResourceParser, list: MutableList<Pair<String, ChangelogType>>): Boolean {
- if (parser.name != tag) return false
- if (parser.getAttributeValue(null, attr).isNotBlank())
- list.add(Pair(parser.getAttributeValue(null, attr), this))
- return true
- }
-}
-
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt
index f13ec20d..e601283c 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt
@@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.support.v4.app.ActivityOptionsCompat
import android.support.v4.content.ContextCompat
+import ca.allanwang.kau.utils.startActivity
import com.pitchedapps.frost.LoginActivity
import com.pitchedapps.frost.R
import com.pitchedapps.frost.WebOverlayActivity
@@ -17,12 +18,10 @@ import com.pitchedapps.frost.facebook.FbTab
private const val EXTRA_COOKIES = "extra_cookies"
private const val ARG_URL = "arg_url"
-fun Context.launchNewTask(clazz: Class<out Activity>, cookieList: ArrayList<CookieModel> = arrayListOf()) {
- val intent = (Intent(this, clazz))
- if (clazz != LoginActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
- intent.putParcelableArrayListExtra(EXTRA_COOKIES, cookieList)
- startActivity(intent)
- if (this is Activity) finish()
+fun Context.launchNewTask(clazz: Class<out Activity>, cookieList: ArrayList<CookieModel> = arrayListOf(), clearStack: Boolean = clazz != LoginActivity::class.java) {
+ startActivity(clazz, clearStack, {
+ putParcelableArrayListExtra(EXTRA_COOKIES, cookieList)
+ })
}
fun Activity.cookies(): ArrayList<CookieModel> {
@@ -39,17 +38,4 @@ fun Context.launchWebOverlay(url: String) {
fun WebOverlayActivity.url(): String {
return intent.extras?.getString(ARG_URL) ?: FbTab.FEED.url
-}
-
-fun Activity.restart(extras: ((Intent) -> Unit)? = null) {
- val i = Intent(this, this::class.java)
- i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
- extras?.invoke(i)
- startActivity(i)
- overridePendingTransition(0, 0) //No transitions
- finish()
- overridePendingTransition(0, 0)
-}
-
-fun Int.toString(c: Context) = c.getString(this)
-fun Int.toColor(c: Context) = ContextCompat.getColor(c, this) \ No newline at end of file
+} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/FragmentUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/FragmentUtils.kt
deleted file mode 100644
index f945c90a..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/FragmentUtils.kt
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.pitchedapps.frost.utils
-
-import android.os.Bundle
-import android.support.v4.app.Fragment
-
-/**
- * Created by Allan Wang on 2017-05-29.
- */
-
-private fun Fragment.bundle(): Bundle {
- if (this.arguments == null)
- this.arguments = Bundle()
- return this.arguments
-}
-
-fun <T : Fragment> T.putString(key: String, value: String): T {
- this.bundle().putString(key, value)
- return this
-}
-
-fun <T : Fragment> T.putInt(key: String, value: Int): T {
- this.bundle().putInt(key, value)
- return this
-} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/IIconUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/IIconUtils.kt
deleted file mode 100644
index 2e0a44c1..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/IIconUtils.kt
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.pitchedapps.frost.utils
-
-import android.content.Context
-import android.content.res.ColorStateList
-import android.graphics.Color
-import android.graphics.drawable.Drawable
-import android.support.annotation.ColorInt
-import com.mikepenz.iconics.IconicsDrawable
-import com.mikepenz.iconics.typeface.IIcon
-
-/**
- * Created by Allan Wang on 2017-05-29.
- */
-fun IIcon.toDrawable(c: Context, sizeDp: Int = 24, @ColorInt color: Int = Color.WHITE): Drawable {
- val state = ColorStateList.valueOf(color)
- val icon = IconicsDrawable(c).icon(this).sizeDp(sizeDp)
- icon.setTintList(state)
- return icon
-} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Kotterknife.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Kotterknife.kt
deleted file mode 100644
index 6e3b5c24..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Kotterknife.kt
+++ /dev/null
@@ -1,137 +0,0 @@
-package com.pitchedapps.frost.utils
-
-/**
- * Created by Allan Wang on 2017-05-29.
- *
- * Courtesy of Jake Wharton
- *
- * https://github.com/JakeWharton/kotterknife/blob/master/src/main/kotlin/kotterknife/ButterKnife.kt
- */
-import android.app.Activity
-import android.app.Dialog
-import android.app.DialogFragment
-import android.app.Fragment
-import android.support.v7.widget.RecyclerView.ViewHolder
-import android.view.View
-import kotlin.properties.ReadOnlyProperty
-import kotlin.reflect.KProperty
-import android.support.v4.app.DialogFragment as SupportDialogFragment
-import android.support.v4.app.Fragment as SupportFragment
-
-public fun <V : View> View.bindView(id: Int)
- : ReadOnlyProperty<View, V> = required(id, viewFinder)
-public fun <V : View> Activity.bindView(id: Int)
- : ReadOnlyProperty<Activity, V> = required(id, viewFinder)
-public fun <V : View> Dialog.bindView(id: Int)
- : ReadOnlyProperty<Dialog, V> = required(id, viewFinder)
-public fun <V : View> DialogFragment.bindView(id: Int)
- : ReadOnlyProperty<DialogFragment, V> = required(id, viewFinder)
-public fun <V : View> SupportDialogFragment.bindView(id: Int)
- : ReadOnlyProperty<SupportDialogFragment, V> = required(id, viewFinder)
-public fun <V : View> Fragment.bindView(id: Int)
- : ReadOnlyProperty<Fragment, V> = required(id, viewFinder)
-public fun <V : View> SupportFragment.bindView(id: Int)
- : ReadOnlyProperty<SupportFragment, V> = required(id, viewFinder)
-public fun <V : View> ViewHolder.bindView(id: Int)
- : ReadOnlyProperty<ViewHolder, V> = required(id, viewFinder)
-
-public fun <V : View> View.bindOptionalView(id: Int)
- : ReadOnlyProperty<View, V?> = optional(id, viewFinder)
-public fun <V : View> Activity.bindOptionalView(id: Int)
- : ReadOnlyProperty<Activity, V?> = optional(id, viewFinder)
-public fun <V : View> Dialog.bindOptionalView(id: Int)
- : ReadOnlyProperty<Dialog, V?> = optional(id, viewFinder)
-public fun <V : View> DialogFragment.bindOptionalView(id: Int)
- : ReadOnlyProperty<DialogFragment, V?> = optional(id, viewFinder)
-public fun <V : View> SupportDialogFragment.bindOptionalView(id: Int)
- : ReadOnlyProperty<SupportDialogFragment, V?> = optional(id, viewFinder)
-public fun <V : View> Fragment.bindOptionalView(id: Int)
- : ReadOnlyProperty<Fragment, V?> = optional(id, viewFinder)
-public fun <V : View> SupportFragment.bindOptionalView(id: Int)
- : ReadOnlyProperty<SupportFragment, V?> = optional(id, viewFinder)
-public fun <V : View> ViewHolder.bindOptionalView(id: Int)
- : ReadOnlyProperty<ViewHolder, V?> = optional(id, viewFinder)
-
-public fun <V : View> View.bindViews(vararg ids: Int)
- : ReadOnlyProperty<View, List<V>> = required(ids, viewFinder)
-public fun <V : View> Activity.bindViews(vararg ids: Int)
- : ReadOnlyProperty<Activity, List<V>> = required(ids, viewFinder)
-public fun <V : View> Dialog.bindViews(vararg ids: Int)
- : ReadOnlyProperty<Dialog, List<V>> = required(ids, viewFinder)
-public fun <V : View> DialogFragment.bindViews(vararg ids: Int)
- : ReadOnlyProperty<DialogFragment, List<V>> = required(ids, viewFinder)
-public fun <V : View> SupportDialogFragment.bindViews(vararg ids: Int)
- : ReadOnlyProperty<SupportDialogFragment, List<V>> = required(ids, viewFinder)
-public fun <V : View> Fragment.bindViews(vararg ids: Int)
- : ReadOnlyProperty<Fragment, List<V>> = required(ids, viewFinder)
-public fun <V : View> SupportFragment.bindViews(vararg ids: Int)
- : ReadOnlyProperty<SupportFragment, List<V>> = required(ids, viewFinder)
-public fun <V : View> ViewHolder.bindViews(vararg ids: Int)
- : ReadOnlyProperty<ViewHolder, List<V>> = required(ids, viewFinder)
-
-public fun <V : View> View.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<View, List<V>> = optional(ids, viewFinder)
-public fun <V : View> Activity.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<Activity, List<V>> = optional(ids, viewFinder)
-public fun <V : View> Dialog.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<Dialog, List<V>> = optional(ids, viewFinder)
-public fun <V : View> DialogFragment.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<DialogFragment, List<V>> = optional(ids, viewFinder)
-public fun <V : View> SupportDialogFragment.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<SupportDialogFragment, List<V>> = optional(ids, viewFinder)
-public fun <V : View> Fragment.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<Fragment, List<V>> = optional(ids, viewFinder)
-public fun <V : View> SupportFragment.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<SupportFragment, List<V>> = optional(ids, viewFinder)
-public fun <V : View> ViewHolder.bindOptionalViews(vararg ids: Int)
- : ReadOnlyProperty<ViewHolder, List<V>> = optional(ids, viewFinder)
-
-private val View.viewFinder: View.(Int) -> View?
- get() = { findViewById(it) }
-private val Activity.viewFinder: Activity.(Int) -> View?
- get() = { findViewById(it) }
-private val Dialog.viewFinder: Dialog.(Int) -> View?
- get() = { findViewById(it) }
-private val DialogFragment.viewFinder: DialogFragment.(Int) -> View?
- get() = { dialog.findViewById(it) }
-private val SupportDialogFragment.viewFinder: SupportDialogFragment.(Int) -> View?
- get() = { dialog.findViewById(it) }
-private val Fragment.viewFinder: Fragment.(Int) -> View?
- get() = { view.findViewById(it) }
-private val SupportFragment.viewFinder: SupportFragment.(Int) -> View?
- get() = { view!!.findViewById(it) }
-private val ViewHolder.viewFinder: ViewHolder.(Int) -> View?
- get() = { itemView.findViewById(it) }
-
-private fun viewNotFound(id:Int, desc: KProperty<*>): Nothing =
- throw IllegalStateException("View ID $id for '${desc.name}' not found.")
-
-@Suppress("UNCHECKED_CAST")
-private fun <T, V : View> required(id: Int, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> t.finder(id) as V? ?: viewNotFound(id, desc) }
-
-@Suppress("UNCHECKED_CAST")
-private fun <T, V : View> optional(id: Int, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> t.finder(id) as V? }
-
-@Suppress("UNCHECKED_CAST")
-private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> ids.map { t.finder(it) as V? ?: viewNotFound(it, desc) } }
-
-@Suppress("UNCHECKED_CAST")
-private fun <T, V : View> optional(ids: IntArray, finder: T.(Int) -> View?)
- = Lazy { t: T, desc -> ids.map { t.finder(it) as V? }.filterNotNull() }
-
-// Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it
-private class Lazy<T, V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> {
- private object EMPTY
- private var value: Any? = EMPTY
-
- override fun getValue(thisRef: T, property: KProperty<*>): V {
- if (value == EMPTY) {
- value = initializer(thisRef, property)
- }
- @Suppress("UNCHECKED_CAST")
- return value as V
- }
-} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
index 3d5cf1cb..d9ce828e 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt
@@ -1,6 +1,7 @@
package com.pitchedapps.frost.utils
import android.util.Log
+import ca.allanwang.kau.logging.TimberLogger
import com.crashlytics.android.Crashlytics
import timber.log.Timber
@@ -8,13 +9,7 @@ import timber.log.Timber
/**
* Created by Allan Wang on 2017-05-28.
*/
-object L {
- const val TAG = "Frost: %s"
- fun e(s: String) = Timber.e(TAG, s)
- fun d(s: String) = Timber.d(TAG, s)
- fun i(s: String) = Timber.i(TAG, s)
- fun v(s: String) = Timber.v(TAG, s)
-}
+object L : TimberLogger("Frost")
internal class CrashReportingTree : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String?, t: Throwable?) {
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
index ec296309..89cccd40 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
@@ -1,111 +1,26 @@
package com.pitchedapps.frost.utils
-import android.content.Context
-import android.content.SharedPreferences
import android.graphics.Color
+import ca.allanwang.kau.kpref.KPref
+import ca.allanwang.kau.kpref.kpref
/**
* Created by Allan Wang on 2017-05-28.
*
* Shared Preference object with lazy cached retrievals
*/
+object Prefs : KPref() {
-private val PREFERENCE_NAME = "${com.pitchedapps.frost.BuildConfig.APPLICATION_ID}.prefs"
-private val LAST_ACTIVE = "last_active"
-private val USER_ID = "user_id"
-private val COLOR_TEXT = "color_text"
-private val COLOR_BG = "color_bg"
-private val COLOR_HEADER = "color_header"
-private val COLOR_ICONS = "color_icons"
-private val THEME_TYPE = "theme_type"
+ var lastActive: Long by kpref("last_active", -1L)
-object Prefs {
+ var userId: Long by kpref("user_id", -1L)
- private const val prefDefaultLong = -2L
- private const val prefDefaultInt = -2
+ var textColor: Int by kpref("color_text", Color.BLACK)
- lateinit private var c: Context
- operator fun invoke(c: Context) {
- this.c = c
- lastActive = 0
- }
+ var bgColor: Int by kpref("color_bg", Color.WHITE)
- private val sp: SharedPreferences by lazy { c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE) }
+ var headerColor: Int by kpref("color_header", 0xff3b5998.toInt())
- var lastActive: Long = prefDefaultLong
- get() {
- if (field == prefDefaultLong) field = sp.getLong(LAST_ACTIVE, -1)
- return field
- }
- set(value) {
- field = value
- if (value != prefDefaultLong) set(LAST_ACTIVE, System.currentTimeMillis())
- }
+ var iconColor: Int by kpref("color_icons", Color.WHITE)
- const val userIdDefault = -1L
- var userId: Long = prefDefaultLong
- get() {
- if (field == prefDefaultLong) field = sp.getLong(USER_ID, userIdDefault)
- return field
- }
- set(value) {
- field = value
- if (value != prefDefaultLong) set(USER_ID, value)
- }
-
- var textColor: Int = prefDefaultInt
- get() {
- if (field == prefDefaultInt) field = sp.getInt(COLOR_TEXT, Color.BLACK)
- return field
- }
- set(value) {
- field = value
- if (value != prefDefaultInt) set(COLOR_TEXT, value)
- }
-
- var bgColor: Int = prefDefaultInt
- get() {
- if (field == prefDefaultInt) field = sp.getInt(COLOR_BG, Color.WHITE)
- return field
- }
- set(value) {
- field = value
- if (value != prefDefaultInt) set(COLOR_BG, value)
- }
-
- var headerColor: Int = prefDefaultInt
- get() {
- if (field == prefDefaultInt) field = sp.getInt(COLOR_HEADER, 0xff3b5998.toInt())
- return field
- }
- set(value) {
- field = value
- if (value != prefDefaultInt) set(COLOR_HEADER, value)
- }
-
- var iconColor: Int = prefDefaultInt
- get() {
- if (field == prefDefaultInt) field = sp.getInt(COLOR_ICONS, Color.WHITE)
- return field
- }
- set(value) {
- field = value
- if (value != prefDefaultInt) set(COLOR_ICONS, value)
- }
-
- private fun set(key: String, value: Boolean) = sp.edit().putBoolean(key, value).apply()
- private fun set(key: String, value: Int) = sp.edit().putInt(key, value).apply()
- private fun set(key: String, value: Long) = sp.edit().putLong(key, value).apply()
- private fun set(key: String, value: String) = sp.edit().putString(key, value).apply()
-
- fun clear() {
- L.d("Clearing Prefs")
- sp.edit().clear().apply()
- lastActive = prefDefaultLong
- userId = prefDefaultLong
- textColor = prefDefaultInt
- bgColor = prefDefaultInt
- headerColor = prefDefaultInt
- iconColor = prefDefaultInt
- }
}