From 2fe3422895c19d93fdb515b72ae497f4263e77bc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 16 Sep 2017 20:53:49 -0400 Subject: Feature/contextual overlays (#295) * Update theme * Update theme * Compile compacts * Update changelog * Update theme * Add overlay context items and their bindings * Replace default with null and add changelog --- .../pitchedapps/frost/activities/MainActivity.kt | 1 + .../frost/activities/WebOverlayActivity.kt | 10 +++- .../com/pitchedapps/frost/enums/OverlayContext.kt | 65 ++++++++++++++++++++++ .../frost/services/FrostNotifications.kt | 9 +-- .../kotlin/com/pitchedapps/frost/utils/Utils.kt | 1 + app/src/main/res/menu/menu_main.xml | 2 +- app/src/main/res/menu/menu_settings.xml | 2 +- app/src/main/res/menu/menu_web.xml | 5 +- app/src/main/res/values/ids.xml | 5 ++ app/src/main/res/xml/frost_changelog.xml | 2 +- 10 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/enums/OverlayContext.kt (limited to 'app/src/main') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt index dd8ed558..32f7c793 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/MainActivity.kt @@ -48,6 +48,7 @@ import com.pitchedapps.frost.contracts.FileChooserContract import com.pitchedapps.frost.contracts.FileChooserDelegate import com.pitchedapps.frost.dbflow.loadFbCookie import com.pitchedapps.frost.dbflow.loadFbTabs +import com.pitchedapps.frost.enums.OverlayContext import com.pitchedapps.frost.enums.Theme import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.FbCookie.switchUser diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt index e5452655..de806779 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt @@ -20,6 +20,7 @@ import com.pitchedapps.frost.R import com.pitchedapps.frost.contracts.ActivityWebContract import com.pitchedapps.frost.contracts.FileChooserContract import com.pitchedapps.frost.contracts.FileChooserDelegate +import com.pitchedapps.frost.enums.OverlayContext import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.USER_AGENT_BASIC import com.pitchedapps.frost.facebook.formattedFbUrl @@ -53,7 +54,7 @@ class WebOverlayBasicActivity : WebOverlayActivityBase(true) */ class WebOverlayActivity : WebOverlayActivityBase(false) -open class WebOverlayActivityBase(val forceBasicAgent: Boolean) : KauBaseActivity(), +open class WebOverlayActivityBase(private val forceBasicAgent: Boolean) : KauBaseActivity(), ActivityWebContract, FileChooserContract by FileChooserDelegate() { val toolbar: Toolbar by bindView(R.id.overlay_toolbar) @@ -69,6 +70,9 @@ open class WebOverlayActivityBase(val forceBasicAgent: Boolean) : KauBaseActivit val userId: Long get() = intent.extras?.getLong(ARG_USER_ID, Prefs.userId) ?: Prefs.userId + val overlayContext: OverlayContext? + get() = intent.extras?.getSerializable(ARG_OVERLAY_CONTEXT) as OverlayContext? + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (urlTest == null) { @@ -157,6 +161,7 @@ open class WebOverlayActivityBase(val forceBasicAgent: Boolean) : KauBaseActivit override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_web, menu) + overlayContext?.onMenuCreate(this, menu) toolbar.tint(Prefs.iconColor) setMenuIcons(menu, Prefs.iconColor, R.id.action_share to CommunityMaterial.Icon.cmd_share, @@ -168,7 +173,8 @@ open class WebOverlayActivityBase(val forceBasicAgent: Boolean) : KauBaseActivit when (item.itemId) { R.id.action_copy_link -> copyToClipboard(frostWeb.web.url) R.id.action_share -> shareText(frostWeb.web.url) - else -> return super.onOptionsItemSelected(item) + else -> if (!OverlayContext.onOptionsItemSelected(frostWeb.web, item.itemId)) + return super.onOptionsItemSelected(item) } return true } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/enums/OverlayContext.kt b/app/src/main/kotlin/com/pitchedapps/frost/enums/OverlayContext.kt new file mode 100644 index 00000000..cc71b19e --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/enums/OverlayContext.kt @@ -0,0 +1,65 @@ +package com.pitchedapps.frost.enums + +import android.content.Context +import android.view.Menu +import android.view.MenuItem +import ca.allanwang.kau.utils.toDrawable +import com.mikepenz.iconics.typeface.IIcon +import com.pitchedapps.frost.R +import com.pitchedapps.frost.facebook.FbItem +import com.pitchedapps.frost.web.FrostWebViewCore + +/** + * Created by Allan Wang on 2017-09-16. + * + * Options for [WebOverlayActivityBase] to give more info as to what kind of + * overlay is present. + * + * For now, this is able to add new menu options upon first load + */ +enum class OverlayContext(private val menuItem: FrostMenuItem?) { + + NOTIFICATION(FrostMenuItem(R.id.action_notification, FbItem.NOTIFICATIONS.icon, R.string.notifications) { webview -> + webview.loadUrl(FbItem.NOTIFICATIONS.url, true) + }), + MESSAGE(FrostMenuItem(R.id.action_messages, FbItem.MESSAGES.icon, R.string.messages) { webview -> + webview.loadUrl(FbItem.MESSAGES.url, true) + }); + + /** + * Inject the [menuItem] in the order that they are given at the front of the menu + */ + fun onMenuCreate(context: Context, menu: Menu) { + menuItem?.addToMenu(context, menu, 0) + } + + companion object { + + val values = OverlayContext.values() //save one instance + /** + * Execute selection call for an item by id + * Returns [true] if selection was consumed, [false] otherwise + */ + fun onOptionsItemSelected(webview: FrostWebViewCore, id: Int): Boolean { + val consumer = values.firstOrNull { id == it.menuItem?.id } ?: return false + consumer.menuItem!!.onClick(webview) + return true + } + } +} + +/** + * Frame for an injectable menu item + */ +class FrostMenuItem( + val id: Int, + val iicon: IIcon, + val stringRes: Int, + val showAsAction: Int = MenuItem.SHOW_AS_ACTION_ALWAYS, + val onClick: (webview: FrostWebViewCore) -> Unit) { + fun addToMenu(context: Context, menu: Menu, index: Int) { + val item = menu.add(Menu.NONE, id, index, stringRes) + item.icon = iicon.toDrawable(context, 18) + item.setShowAsAction(showAsAction) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index 810e5f97..800fe492 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -21,11 +21,9 @@ import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.FrostWebActivity import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.enums.OverlayContext import com.pitchedapps.frost.facebook.formattedFbUrl -import com.pitchedapps.frost.utils.ARG_USER_ID -import com.pitchedapps.frost.utils.L -import com.pitchedapps.frost.utils.Prefs -import com.pitchedapps.frost.utils.withRoundIcon +import com.pitchedapps.frost.utils.* import org.jetbrains.anko.runOnUiThread /** @@ -96,6 +94,7 @@ data class NotificationContent(val data: CookieModel, val text: String, val timestamp: Long, val profileUrl: String) { + fun createNotification(context: Context) = createNotification(context, FROST_NOTIFICATION_GROUP) fun createMessageNotification(context: Context) = createNotification(context, FROST_MESSAGE_NOTIFICATION_GROUP) @@ -104,6 +103,8 @@ data class NotificationContent(val data: CookieModel, val intent = Intent(context, FrostWebActivity::class.java) intent.data = Uri.parse(href.formattedFbUrl) intent.putExtra(ARG_USER_ID, data.id) + val overlayContext = if (groupPrefix == FROST_MESSAGE_NOTIFICATION_GROUP) OverlayContext.MESSAGE else OverlayContext.NOTIFICATION + intent.putExtra(ARG_OVERLAY_CONTEXT, overlayContext) val group = "${groupPrefix}_${data.id}" val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) val ringtone = if (groupPrefix == FROST_MESSAGE_NOTIFICATION_GROUP) Prefs.messageRingtone else Prefs.notificationRingtone 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 bcda6a43..e5e1a706 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -41,6 +41,7 @@ const val ARG_URL = "arg_url" const val ARG_USER_ID = "arg_user_id" const val ARG_IMAGE_URL = "arg_image_url" const val ARG_TEXT = "arg_text" +const val ARG_OVERLAY_CONTEXT = "arg_overlay_context" fun Context.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf(), clearStack: Boolean = false) { startActivity(clazz, clearStack, intentBuilder = { diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index 3063ffec..8c6d3b34 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -1,7 +1,7 @@ + tools:context="com.pitchedapps.frost.activities.MainActivity"> + tools:context="com.pitchedapps.frost.activities.SettingsActivity"> - + tools:context="com.pitchedapps.frost.activities.WebOverlayActivity"> + app:showAsAction="ifRoom" /> diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index d32a3b71..0aaeeab8 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -9,4 +9,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/frost_changelog.xml b/app/src/main/res/xml/frost_changelog.xml index 99e60483..0e6bb252 100644 --- a/app/src/main/res/xml/frost_changelog.xml +++ b/app/src/main/res/xml/frost_changelog.xml @@ -15,7 +15,7 @@ - + -- cgit v1.2.3