mirror of
https://github.com/AllanWang/Frost-for-Facebook.git
synced 2024-11-08 20:12:39 +01:00
Update parsers and add widget items
This commit is contained in:
parent
2fc18d0803
commit
7f371a9532
@ -173,6 +173,20 @@
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!--Widgets-->
|
||||
<receiver android:name=".widgets.NotificationWidget">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/notification_widget_info" />
|
||||
</receiver>
|
||||
<service
|
||||
android:name=".widgets.NotificationWidgetService"
|
||||
android:enabled="true"
|
||||
android:permission="android.permission.BIND_REMOTEVIEWS" />
|
||||
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
android:authorities="${applicationId}.provider"
|
||||
|
@ -20,6 +20,7 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import com.pitchedapps.frost.BuildConfig
|
||||
import org.koin.dsl.module.module
|
||||
import org.koin.standalone.StandAloneContext
|
||||
|
||||
@ -69,15 +70,22 @@ class FrostDatabase(private val privateDb: FrostPrivateDatabase, private val pub
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private fun <T : RoomDatabase> RoomDatabase.Builder<T>.frostBuild() = if (BuildConfig.DEBUG) {
|
||||
fallbackToDestructiveMigration().build()
|
||||
} else {
|
||||
build()
|
||||
}
|
||||
|
||||
fun create(context: Context): FrostDatabase {
|
||||
val privateDb = Room.databaseBuilder(
|
||||
context, FrostPrivateDatabase::class.java,
|
||||
FrostPrivateDatabase.DATABASE_NAME
|
||||
).build()
|
||||
).frostBuild()
|
||||
val publicDb = Room.databaseBuilder(
|
||||
context, FrostPublicDatabase::class.java,
|
||||
FrostPublicDatabase.DATABASE_NAME
|
||||
).build()
|
||||
).frostBuild()
|
||||
return FrostDatabase(privateDb, publicDb)
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,8 @@ data class NotificationEntity(
|
||||
val timestamp: Long,
|
||||
val profileUrl: String?,
|
||||
// Type essentially refers to channel
|
||||
val type: String
|
||||
val type: String,
|
||||
val unread: Boolean
|
||||
) {
|
||||
constructor(
|
||||
type: String,
|
||||
@ -77,7 +78,8 @@ data class NotificationEntity(
|
||||
content.text,
|
||||
content.timestamp,
|
||||
content.profileUrl,
|
||||
type
|
||||
type,
|
||||
content.unread
|
||||
)
|
||||
}
|
||||
|
||||
@ -94,7 +96,8 @@ data class NotificationContentEntity(
|
||||
title = notif.title,
|
||||
text = notif.text,
|
||||
timestamp = notif.timestamp,
|
||||
profileUrl = notif.profileUrl
|
||||
profileUrl = notif.profileUrl,
|
||||
unread = notif.unread
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,8 @@ data class FrostMessages(
|
||||
title = title,
|
||||
text = content ?: "",
|
||||
timestamp = time,
|
||||
profileUrl = img
|
||||
profileUrl = img,
|
||||
unread = unread
|
||||
)
|
||||
}
|
||||
}.toList()
|
||||
|
@ -53,7 +53,8 @@ data class FrostNotifs(
|
||||
title = null,
|
||||
text = content,
|
||||
timestamp = time,
|
||||
profileUrl = img
|
||||
profileUrl = img,
|
||||
unread = unread
|
||||
)
|
||||
}
|
||||
}.toList()
|
||||
|
@ -181,7 +181,8 @@ enum class NotificationType(
|
||||
"Debug Notif",
|
||||
"Test 123",
|
||||
System.currentTimeMillis() / 1000,
|
||||
"https://www.iconexperience.com/_img/v_collection_png/256x256/shadow/dog.png"
|
||||
"https://www.iconexperience.com/_img/v_collection_png/256x256/shadow/dog.png",
|
||||
false
|
||||
)
|
||||
createNotification(context, content).notify(context)
|
||||
}
|
||||
@ -266,7 +267,8 @@ data class NotificationContent(
|
||||
val title: String? = null, // defaults to frost title
|
||||
val text: String,
|
||||
val timestamp: Long,
|
||||
val profileUrl: String?
|
||||
val profileUrl: String?,
|
||||
val unread: Boolean
|
||||
) {
|
||||
|
||||
val notifId = Math.abs(id.toInt())
|
||||
|
@ -1,7 +1,140 @@
|
||||
/*
|
||||
* Copyright 2019 Allan Wang
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.pitchedapps.frost.widgets
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.RemoteViewsService
|
||||
import androidx.annotation.ColorRes
|
||||
import ca.allanwang.kau.utils.ContextHelper
|
||||
import ca.allanwang.kau.utils.withAlpha
|
||||
import com.bumptech.glide.request.target.AppWidgetTarget
|
||||
import com.pitchedapps.frost.R
|
||||
import com.pitchedapps.frost.db.NotificationDao
|
||||
import com.pitchedapps.frost.db.selectNotifications
|
||||
import com.pitchedapps.frost.glide.FrostGlide
|
||||
import com.pitchedapps.frost.glide.GlideApp
|
||||
import com.pitchedapps.frost.services.NOTIF_CHANNEL_GENERAL
|
||||
import com.pitchedapps.frost.services.NotificationContent
|
||||
import com.pitchedapps.frost.utils.Prefs
|
||||
import com.pitchedapps.frost.widgets.NotificationWidget.Companion.NOTIF_WIDGET_IDS
|
||||
import com.pitchedapps.frost.widgets.NotificationWidget.Companion.NOTIF_WIDGET_TYPE
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.koin.standalone.KoinComponent
|
||||
import org.koin.standalone.inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
class NotificationWidget : AppWidgetProvider() {
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
val views = RemoteViews(context.packageName, com.pitchedapps.frost.R.layout.widget_notifications)
|
||||
val intent = NotificationWidgetService.createIntent(context, NOTIF_CHANNEL_GENERAL, appWidgetIds)
|
||||
for (id in appWidgetIds) {
|
||||
views.setRemoteAdapter(R.id.widget_notification_list, intent)
|
||||
appWidgetManager.updateAppWidget(id, views)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
companion object {
|
||||
const val NOTIF_WIDGET_TYPE = "notif_widget_type"
|
||||
const val NOTIF_WIDGET_IDS = "notif_widget_ids"
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationWidgetService : RemoteViewsService() {
|
||||
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory = NotificationWidgetDataProvider(this, intent)
|
||||
|
||||
companion object {
|
||||
fun createIntent(context: Context, type: String, appWidgetIds: IntArray): Intent =
|
||||
Intent(context, NotificationWidgetService::class.java)
|
||||
.putExtra(NOTIF_WIDGET_TYPE, type)
|
||||
.putExtra(NOTIF_WIDGET_IDS, appWidgetIds)
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationWidgetDataProvider(val context: Context, val intent: Intent) : RemoteViewsService.RemoteViewsFactory,
|
||||
CoroutineScope, KoinComponent {
|
||||
|
||||
private val notifDao: NotificationDao by inject()
|
||||
@Volatile
|
||||
private var content: List<NotificationContent> = emptyList()
|
||||
|
||||
private val type = intent.getStringExtra(NOTIF_WIDGET_TYPE)
|
||||
|
||||
private val widgetIds = intent.getIntArrayExtra(NOTIF_WIDGET_IDS)
|
||||
|
||||
private lateinit var job: Job
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = ContextHelper.dispatcher + job
|
||||
|
||||
private suspend fun loadNotifications() {
|
||||
content = notifDao.selectNotifications(Prefs.userId, type)
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
job = SupervisorJob()
|
||||
runBlocking {
|
||||
loadNotifications()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDataSetChanged() {
|
||||
runBlocking {
|
||||
loadNotifications()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getLoadingView(): RemoteViews? = null
|
||||
|
||||
override fun getItemId(position: Int): Long = content[position].id
|
||||
|
||||
override fun hasStableIds(): Boolean = true
|
||||
|
||||
override fun getViewAt(position: Int): RemoteViews {
|
||||
val views = RemoteViews(context.packageName, R.layout.widget_notification_item)
|
||||
val glide = GlideApp.with(context).asBitmap()
|
||||
val notif = content[position]
|
||||
views.setBackgroundColor(R.id.item_frame, Prefs.nativeBgColor(notif.unread))
|
||||
views.setTextColor(R.id.item_content, Prefs.textColor)
|
||||
views.setTextViewText(R.id.item_content, notif.text)
|
||||
views.setTextColor(R.id.item_date, Prefs.textColor.withAlpha(150))
|
||||
views.setTextViewText(R.id.item_date, notif.timestamp.toString()) // TODO
|
||||
glide.load(notif.profileUrl).transform(FrostGlide.circleCrop)
|
||||
.into(AppWidgetTarget(context, R.id.item_avatar, views))
|
||||
return views
|
||||
}
|
||||
|
||||
private fun RemoteViews.setBackgroundColor(viewId: Int, @ColorRes color: Int) {
|
||||
setInt(viewId, "setBackgroundColor", color)
|
||||
}
|
||||
|
||||
override fun getCount(): Int = content.size
|
||||
|
||||
override fun getViewTypeCount(): Int {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
job.cancel()
|
||||
}
|
||||
}
|
||||
|
53
app/src/main/res/layout/widget_notification_item.xml
Normal file
53
app/src/main/res/layout/widget_notification_item.xml
Normal file
@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/item_frame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:orientation="horizontal"
|
||||
android:paddingStart="@dimen/kau_activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/kau_activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/kau_activity_vertical_margin">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/item_avatar"
|
||||
android:layout_width="@dimen/avatar_image_size"
|
||||
android:layout_height="@dimen/avatar_image_size"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<!--
|
||||
Unlike the actual notification panel,
|
||||
we do not show thumbnails, and we limit the title length
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/kau_padding_normal"
|
||||
android:layout_marginEnd="@dimen/kau_padding_normal"
|
||||
android:ellipsize="end"
|
||||
android:lines="2" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/item_date"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/kau_padding_normal"
|
||||
android:layout_marginEnd="@dimen/kau_padding_normal"
|
||||
android:ellipsize="end"
|
||||
android:lines="1"
|
||||
android:textSize="12sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
22
app/src/main/res/layout/widget_notifications.xml
Normal file
22
app/src/main/res/layout/widget_notifications.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
<LinearLayout
|
||||
android:id="@+id/widget_layout_main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:paddingStart="@dimen/kau_padding_small"
|
||||
android:paddingEnd="@dimen/kau_padding_small">
|
||||
<ImageView
|
||||
android:id="@+id/img_refresh"
|
||||
android:layout_width="@dimen/toolbar_icon_size"
|
||||
android:layout_height="@dimen/toolbar_icon_size"
|
||||
android:layout_gravity="center_vertical"/>
|
||||
</LinearLayout>
|
||||
<ListView
|
||||
android:id="@+id/widget_notification_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
</ListView>
|
||||
</LinearLayout>
|
@ -9,4 +9,6 @@
|
||||
<dimen name="tab_bar_height">50dp</dimen>
|
||||
<dimen name="intro_bar_height">64dp</dimen>
|
||||
<dimen name="badge_icon_size">20dp</dimen>
|
||||
|
||||
<dimen name="toolbar_icon_size">18dp</dimen>
|
||||
</resources>
|
||||
|
4
app/src/main/res/xml/notification_widget_info.xml
Normal file
4
app/src/main/res/xml/notification_widget_info.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
</appwidget-provider>
|
@ -2,7 +2,7 @@
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 1,
|
||||
"identityHash": "0a9d994786b7e07fea95c11d9210ce0e",
|
||||
"identityHash": "fe8f5b6c27f48d7e0733ee6819f06f40",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "cookies",
|
||||
@ -38,7 +38,7 @@
|
||||
},
|
||||
{
|
||||
"tableName": "notifications",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`notif_id` INTEGER NOT NULL, `userId` INTEGER NOT NULL, `href` TEXT NOT NULL, `title` TEXT, `text` TEXT NOT NULL, `timestamp` INTEGER NOT NULL, `profileUrl` TEXT, `type` TEXT NOT NULL, PRIMARY KEY(`notif_id`, `userId`), FOREIGN KEY(`userId`) REFERENCES `cookies`(`cookie_id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`notif_id` INTEGER NOT NULL, `userId` INTEGER NOT NULL, `href` TEXT NOT NULL, `title` TEXT, `text` TEXT NOT NULL, `timestamp` INTEGER NOT NULL, `profileUrl` TEXT, `type` TEXT NOT NULL, `unread` INTEGER NOT NULL, PRIMARY KEY(`notif_id`, `userId`), FOREIGN KEY(`userId`) REFERENCES `cookies`(`cookie_id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
@ -87,6 +87,12 @@
|
||||
"columnName": "type",
|
||||
"affinity": "TEXT",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "unread",
|
||||
"columnName": "unread",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
@ -183,7 +189,7 @@
|
||||
"views": [],
|
||||
"setupQueries": [
|
||||
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"0a9d994786b7e07fea95c11d9210ce0e\")"
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"fe8f5b6c27f48d7e0733ee6819f06f40\")"
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user