aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-20 15:50:08 -0700
committerAllan Wang <me@allanwang.ca>2017-06-20 15:50:08 -0700
commite916afddb89550d7f99f17e1477ad17f3bd770f9 (patch)
treeb9a846da082599295d2671d29eaee74d9310f4c6 /app/src/main/kotlin
parent2d7fa0f6d0c152bb9f3fd30370ef510a0d452d00 (diff)
downloadfrost-e916afddb89550d7f99f17e1477ad17f3bd770f9.tar.gz
frost-e916afddb89550d7f99f17e1477ad17f3bd770f9.tar.bz2
frost-e916afddb89550d7f99f17e1477ad17f3bd770f9.zip
Add user switching for notifications
Diffstat (limited to 'app/src/main/kotlin')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt18
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt10
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt1
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt21
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt29
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt14
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt13
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt2
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt2
10 files changed, 69 insertions, 43 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt
index 31ebfda2..c853e8f0 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/FrostWebActivity.kt
@@ -1,17 +1,8 @@
package com.pitchedapps.frost
import android.os.Bundle
-import android.support.design.widget.CoordinatorLayout
-import android.support.v7.app.AppCompatActivity
-import android.support.v7.widget.Toolbar
-import ca.allanwang.kau.utils.*
-import com.jude.swipbackhelper.SwipeBackHelper
-import com.pitchedapps.frost.facebook.FbTab
-import com.pitchedapps.frost.utils.ARG_URL
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.formattedFbUrl
-import com.pitchedapps.frost.utils.setFrostColors
-import com.pitchedapps.frost.web.FrostWebView
/**
@@ -20,8 +11,15 @@ import com.pitchedapps.frost.web.FrostWebView
* Replica of [WebOverlayActivity] with a different base url
* Didn't use activity-alias because it causes issues when only one activity has the singleInstance mode
*/
-class FrostWebActivity:WebOverlayActivity() {
+class FrostWebActivity : WebOverlayActivity() {
+
override val url: String
get() = intent.dataString!!.formattedFbUrl
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ Prefs.prevId = Prefs.userId
+ super.onCreate(savedInstanceState)
+ }
+
} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt
index ca50d4e2..fcffffb2 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt
@@ -28,6 +28,7 @@ import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.materialdrawer.AccountHeader
import com.mikepenz.materialdrawer.Drawer
import com.pitchedapps.frost.dbflow.loadFbTabs
+import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbCookie.switchUser
import com.pitchedapps.frost.facebook.FbTab
import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL
@@ -83,8 +84,8 @@ class MainActivity : BaseActivity() {
super.onPageScrolled(position, positionOffset, positionOffsetPixels)
val delta: Float by lazy { positionOffset * (255 - 128).toFloat() }
tabsForEachView {
- position, view ->
- view.setAllAlpha(when (position) {
+ tabPosition, view ->
+ view.setAllAlpha(when (tabPosition) {
position -> 255.0f - delta
position + 1 -> 128.0f + delta
else -> 128f
@@ -243,6 +244,11 @@ class MainActivity : BaseActivity() {
return true
}
+ override fun onResume() {
+ super.onResume()
+ FbCookie.switchBackUser { }
+ }
+
override fun onBackPressed() {
if (currentFragment.onBackPressed()) return
super.onBackPressed()
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt
index b55b2a10..19fa6362 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/SettingsActivity.kt
@@ -87,7 +87,6 @@ class SettingsActivity : KPrefActivity() {
_, _, which, text ->
item.pref = options[which]
this@SettingsActivity.scheduleNotifications(item.pref)
- this@SettingsActivity.toast(text)
true
})
}
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
index bdac366e..0b2be3ee 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt
@@ -1,9 +1,10 @@
package com.pitchedapps.frost
import android.os.Bundle
+import android.os.Handler
import android.support.v7.app.AppCompatActivity
import com.pitchedapps.frost.dbflow.loadFbCookiesAsync
-import com.pitchedapps.frost.services.requestNotifications
+import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.launchNewTask
@@ -16,15 +17,15 @@ class StartActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
L.d("Load cookies ${System.currentTimeMillis()}")
- loadFbCookiesAsync {
- cookies ->
- cookies.forEach { requestNotifications(it.id) }
-
- L.d("Cookies loaded ${System.currentTimeMillis()} $cookies")
- if (cookies.isNotEmpty())
- launchNewTask(if (Prefs.userId != -1L) MainActivity::class.java else SelectorActivity::class.java, ArrayList(cookies))
- else
- launchNewTask(LoginActivity::class.java)
+ FbCookie.switchBackUser {
+ loadFbCookiesAsync {
+ cookies ->
+ L.d("Cookies loaded ${System.currentTimeMillis()} $cookies")
+ if (cookies.isNotEmpty())
+ launchNewTask(if (Prefs.userId != -1L) MainActivity::class.java else SelectorActivity::class.java, ArrayList(cookies))
+ else
+ launchNewTask(LoginActivity::class.java)
+ }
}
}
} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt
index 320e4028..1660d8db 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt
@@ -6,6 +6,8 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import ca.allanwang.kau.utils.*
import com.jude.swipbackhelper.SwipeBackHelper
+import com.mikepenz.google_material_typeface_library.GoogleMaterial
+import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.utils.ARG_URL
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.formattedFbUrl
@@ -22,27 +24,38 @@ open class WebOverlayActivity : AppCompatActivity() {
val frostWeb: FrostWebView by bindView(R.id.overlay_frost_webview)
val coordinator: CoordinatorLayout by bindView(R.id.overlay_main_content)
+ companion object {
+ const val ARG_USER_ID = "arg_user_id"
+ }
+
open val url: String
get() = intent.extras!!.getString(ARG_URL).formattedFbUrl
+ val userId: Long
+ get() = intent.extras?.getLong(ARG_USER_ID) ?: Prefs.userId
+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_overlay)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
- toolbar.setNavigationOnClickListener { onBackPressed() }
- frostWeb.web.setupWebview(url)
- frostWeb.web.loadBaseUrl()
+ toolbar.navigationIcon = GoogleMaterial.Icon.gmd_close.toDrawable(this, 16, Prefs.iconColor)
+ toolbar.setNavigationOnClickListener { slideOut() }
SwipeBackHelper.onCreate(this)
SwipeBackHelper.getCurrentPage(this)
.setSwipeBackEnable(true)
.setSwipeSensitivity(0.5f)
.setSwipeRelateEnable(true)
.setSwipeRelateOffset(300)
- frostWeb.web.addTitleListener({ toolbar.title = it })
setFrostColors(toolbar, themeWindow = false)
coordinator.setBackgroundColor(Prefs.bgColor.withAlpha(255))
+
+ frostWeb.web.setupWebview(url)
+ frostWeb.web.addTitleListener({ toolbar.title = it })
+
+ if (userId != Prefs.userId) FbCookie.switchUser(userId) { frostWeb.web.loadBaseUrl() }
+ else frostWeb.web.loadBaseUrl()
}
/**
@@ -70,8 +83,12 @@ open class WebOverlayActivity : AppCompatActivity() {
override fun onBackPressed() {
if (!frostWeb.onBackPressed()) {
- finish()
- overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_slide_out_right_top)
+ slideOut()
}
}
+
+ fun slideOut() {
+ finish()
+ overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_slide_out_right_top)
+ }
} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
index 42232fc6..ac58fb2f 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/FbCookie.kt
@@ -84,4 +84,18 @@ object FbCookie {
removeCookie(id)
reset(callback)
}
+
+ /**
+ * Notifications may come from different accounts, and we need to switch the cookies to load them
+ * When coming back to the main app, switch back to our original account before continuing
+ */
+ fun switchBackUser(callback: () -> Unit) {
+ if (Prefs.prevId != -1L && Prefs.prevId != Prefs.userId) {
+ switchUser(Prefs.prevId) {
+ L.d("Switched from ${Prefs.userId} to ${Prefs.prevId}")
+ Prefs.prevId = -1L
+ callback()
+ }
+ } else callback()
+ }
} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt
deleted file mode 100644
index ac94b527..00000000
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationExtensions.kt
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.pitchedapps.frost.services
-
-import android.content.Context
-import android.content.Intent
-
-/**
- * Created by Allan Wang on 2017-06-14.
- */
-fun Context.requestNotifications(id: Long) {
- val intent = Intent(this, NotificationService::class.java)
- intent.putExtra(NotificationService.ARG_ID, id)
- startService(intent)
-} \ No newline at end of file
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
index 813bdce2..9c8bf2e6 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt
@@ -12,6 +12,7 @@ import ca.allanwang.kau.utils.string
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.FrostWebActivity
import com.pitchedapps.frost.R
+import com.pitchedapps.frost.WebOverlayActivity
import com.pitchedapps.frost.dbflow.*
import com.pitchedapps.frost.facebook.FACEBOOK_COM
import com.pitchedapps.frost.facebook.FB_URL_BASE
@@ -109,6 +110,7 @@ class NotificationService : JobService() {
} else {
val intent = Intent(context, FrostWebActivity::class.java)
intent.data = Uri.parse("$FB_URL_BASE$href")
+ intent.putExtra(WebOverlayActivity.ARG_USER_ID, data.id)
val group = "frost_${data.id}"
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
val notifBuilder = context.frostNotification
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 62ed3cdd..40735544 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt
@@ -17,6 +17,8 @@ object Prefs : KPref() {
var userId: Long by kpref("user_id", -1L)
+ var prevId: Long by kpref("prev_id", -1L)
+
var theme: Int by kpref("theme", 0, postSetter = { value: Int ->
loader.invalidate()
})
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 fe56d6d6..d4942e9e 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt
@@ -30,7 +30,7 @@ import com.pitchedapps.frost.services.NotificationService
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) {
+fun Context.launchNewTask(clazz: Class<out Activity>, cookieList: ArrayList<CookieModel> = arrayListOf(), clearStack: Boolean = false) {
startActivity(clazz, clearStack, {
putParcelableArrayListExtra(EXTRA_COOKIES, cookieList)
})