diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils')
4 files changed, 64 insertions, 131 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt deleted file mode 100644 index c2bcc2ab..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt +++ /dev/null @@ -1,69 +0,0 @@ -package com.pitchedapps.frost.utils - -import android.app.Activity -import android.content.Context -import android.content.Intent -import android.graphics.Color -import android.support.v4.app.ActivityOptionsCompat -import android.support.v4.content.ContextCompat -import ca.allanwang.kau.utils.adjustAlpha -import ca.allanwang.kau.utils.isColorDark -import ca.allanwang.kau.utils.lighten -import ca.allanwang.kau.utils.startActivity -import com.afollestad.materialdialogs.MaterialDialog -import com.pitchedapps.frost.LoginActivity -import com.pitchedapps.frost.R -import com.pitchedapps.frost.WebOverlayActivity -import com.pitchedapps.frost.dbflow.CookieModel -import com.pitchedapps.frost.facebook.FbTab - -/** - * Created by Allan Wang on 2017-06-03. - */ -internal const val EXTRA_COOKIES = "extra_cookies" -internal const val ARG_URL = "arg_url" - -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> { - return intent?.extras?.getParcelableArrayList<CookieModel>(EXTRA_COOKIES) ?: arrayListOf() -} - -fun Context.launchWebOverlay(url: String) { - val intent = Intent(this, WebOverlayActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) - intent.putExtra(ARG_URL, url) - val bundle = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.slide_in_right, R.anim.slide_out_right).toBundle() - ContextCompat.startActivity(this, intent, bundle) -} - -fun WebOverlayActivity.url(): String { - return intent.extras?.getString(ARG_URL) ?: FbTab.FEED.url -} - - -fun Activity.materialDialogThemed(action: MaterialDialog.Builder.() -> Unit): MaterialDialog { - val builder = MaterialDialog.Builder(this) - val dimmerTextColor = Prefs.textColor.adjustAlpha(0.8f) - builder.titleColor(Prefs.textColor) - .contentColor(dimmerTextColor) - .widgetColor(dimmerTextColor) - .backgroundColor(Prefs.bgColor.lighten(0.1f)) - .positiveColor(Prefs.textColor) - .negativeColor(Prefs.textColor) - .neutralColor(Prefs.textColor) - builder.action() - return builder.show() -} - -fun Activity.setFrostTheme() { - val isTransparent = Color.alpha(Prefs.bgColor) != 255 - if (Prefs.bgColor.isColorDark()) - setTheme(if (isTransparent) R.style.FrostTheme_Transparent else R.style.FrostTheme) - else - setTheme(if (isTransparent) R.style.FrostTheme_Light_Transparent else R.style.FrostTheme_Light) -}
\ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt deleted file mode 100644 index c8365f5c..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/LazyResettable.kt +++ /dev/null @@ -1,51 +0,0 @@ -package com.pitchedapps.frost.utils - -import java.io.Serializable -import kotlin.reflect.KProperty - -/** - * Created by Allan Wang on 2017-05-30. - * - * Lazy delegate that can be invalidated if needed - * https://stackoverflow.com/a/37294840/4407321 - */ -private object UNINITIALIZED - -fun <T : Any> lazyResettable(initializer: () -> T): LazyResettable<T> = LazyResettable<T>(initializer) - -class LazyResettable<T : Any>(private val initializer: () -> T, lock: Any? = null) : Lazy<T>, Serializable { - @Volatile private var _value: Any = UNINITIALIZED - private val lock = lock ?: this - - fun invalidate() { - _value = UNINITIALIZED - } - - override val value: T - get() { - val _v1 = _value - if (_v1 !== UNINITIALIZED) - @Suppress("UNCHECKED_CAST") - return _v1 as T - - return synchronized(lock) { - val _v2 = _value - if (_v2 !== UNINITIALIZED) { - @Suppress("UNCHECKED_CAST") - _v2 as T - } else { - val typedValue = initializer() - _value = typedValue - typedValue - } - } - } - - override fun isInitialized(): Boolean = _value !== UNINITIALIZED - - override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet." - - operator fun setValue(any: Any, property: KProperty<*>, t: T) { - _value = t - } -}
\ No newline at end of file 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 396b4471..e217da46 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -3,6 +3,7 @@ package com.pitchedapps.frost.utils import android.graphics.Color import ca.allanwang.kau.kpref.KPref import ca.allanwang.kau.kpref.kpref +import ca.allanwang.kau.utils.lazyResettable import com.pitchedapps.frost.injectors.InjectorContract /** diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index c754f6ff..8dfceaad 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -1,18 +1,70 @@ package com.pitchedapps.frost.utils +import android.app.Activity import android.content.Context -import android.net.ConnectivityManager +import android.content.Intent +import android.graphics.Color +import android.support.v4.app.ActivityOptionsCompat +import android.support.v4.content.ContextCompat +import ca.allanwang.kau.utils.* +import com.afollestad.materialdialogs.MaterialDialog +import com.pitchedapps.frost.LoginActivity +import com.pitchedapps.frost.R +import com.pitchedapps.frost.WebOverlayActivity +import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.facebook.FbTab /** - * Created by Allan Wang on 2017-05-28. + * Created by Allan Wang on 2017-06-03. */ -object Utils { - fun dpToPx(dp: Int) = (dp * android.content.res.Resources.getSystem().displayMetrics.density).toInt() - fun pxToDp(px:Int) = (px / android.content.res.Resources.getSystem().displayMetrics.density).toInt() - - fun isNetworkAvailable(context: Context): Boolean { - val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - val activeNetworkInfo = connectivityManager.activeNetworkInfo - return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting - } +internal const val EXTRA_COOKIES = "extra_cookies" +internal const val ARG_URL = "arg_url" + +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> { + return intent?.extras?.getParcelableArrayList<CookieModel>(EXTRA_COOKIES) ?: arrayListOf() +} + +fun Context.launchWebOverlay(url: String) { + val intent = Intent(this, WebOverlayActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.putExtra(ARG_URL, url) + val bundle = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.slide_in_right, R.anim.slide_out_right).toBundle() + ContextCompat.startActivity(this, intent, bundle) +} + +fun WebOverlayActivity.url(): String { + return intent.extras?.getString(ARG_URL) ?: FbTab.FEED.url +} + + +fun Activity.materialDialogThemed(action: MaterialDialog.Builder.() -> Unit): MaterialDialog { + val builder = MaterialDialog.Builder(this).theme() + builder.action() + return builder.show() +} + +fun MaterialDialog.Builder.theme(): MaterialDialog.Builder { + val dimmerTextColor = Prefs.textColor.adjustAlpha(0.8f) + titleColor(Prefs.textColor) + contentColor(dimmerTextColor) + widgetColor(dimmerTextColor) + backgroundColor(Prefs.bgColor.lighten(0.1f).withMinAlpha(200)) + positiveColor(Prefs.textColor) + negativeColor(Prefs.textColor) + neutralColor(Prefs.textColor) + return this +} + +fun Activity.setFrostTheme() { + val isTransparent = Color.alpha(Prefs.bgColor) != 255 + if (Prefs.bgColor.isColorDark()) + setTheme(if (isTransparent) R.style.FrostTheme_Transparent else R.style.FrostTheme) + else + setTheme(if (isTransparent) R.style.FrostTheme_Light_Transparent else R.style.FrostTheme_Light) }
\ No newline at end of file |