Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JetChat] Add glance widget for JetChat App #1424 #1425

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added Glance Widget for JetChat
  • Loading branch information
shangeethsivan committed Jun 28, 2024
commit e5d2904a314c8211784bbdcc3582ca7b1933026e
2 changes: 2 additions & 0 deletions Jetchat/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ dependencies {
implementation(composeBom)
androidTestImplementation(composeBom)

implementation(libs.androidx.glance.appwidget)
implementation(libs.androidx.glance.material3)
implementation(libs.kotlin.stdlib)
implementation(libs.kotlinx.coroutines.android)

Expand Down
10 changes: 9 additions & 1 deletion Jetchat/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".widget.WidgetReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_app_widget_info" />
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package com.example.compose.jetchat.components

import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.os.Build
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
Expand Down Expand Up @@ -45,13 +50,15 @@ import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.compose.jetchat.R
import com.example.compose.jetchat.data.colleagueProfile
import com.example.compose.jetchat.data.meProfile
import com.example.compose.jetchat.theme.JetchatTheme
import com.example.compose.jetchat.widget.WidgetReceiver

@Composable
fun JetchatDrawerContent(
Expand All @@ -61,7 +68,11 @@ fun JetchatDrawerContent(
) {
// Use windowInsetsTopHeight() to add a spacer which pushes the drawer content
// below the status bar (y-axis)
Column(modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
) {
Spacer(Modifier.windowInsetsTopHeight(WindowInsets.statusBars))
DrawerHeader()
DividerItem()
Expand All @@ -74,9 +85,14 @@ fun JetchatDrawerContent(
ProfileItem("Taylor Brooks", colleagueProfile.photo) {
onProfileClicked(colleagueProfile.userId)
}
val context = LocalContext.current
ChatItem("Add Widget to Home Page", false) {
addWidgetToHomeScreen(context)
}
}
}


@Composable
private fun DrawerHeader() {
Row(modifier = Modifier.padding(16.dp), verticalAlignment = CenterVertically) {
Expand All @@ -91,6 +107,7 @@ private fun DrawerHeader() {
)
}
}

@Composable
private fun DrawerItemHeader(text: String) {
Box(
Expand Down Expand Up @@ -200,6 +217,7 @@ fun DrawerPreview() {
}
}
}

@Composable
@Preview
fun DrawerPreviewDark() {
Expand All @@ -211,3 +229,26 @@ fun DrawerPreviewDark() {
}
}
}

private fun addWidgetToHomeScreen(context: Context) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val myProvider = ComponentName(context, WidgetReceiver::class.java)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (appWidgetManager.isRequestPinAppWidgetSupported) {
appWidgetManager.requestPinAppWidget(myProvider, null, null)
} else {
Toast.makeText(
context,
"Unable to add widget, please add from home screen",
Toast.LENGTH_LONG
).show()
}
} else {
Toast.makeText(
context,
"Unable to add widget, please add from home screen",
Toast.LENGTH_LONG
).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ data class Message(
val content: String,
val timestamp: String,
val image: Int? = null,
val authorImage: Int = if (author == "me") R.drawable.ali else R.drawable.someone_else
val authorImage: Int = if (author == "me") R.drawable.ali else R.drawable.someone_else,
)
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,58 @@ private val initialMessages = listOf(
"8:03 PM"
)
)
val unreadMessages = listOf(
Message(
"Taylor Brooks",
"You can use all the same stuff",
"8:05 PM"
),
Message(
"Taylor Brooks",
"@aliconors Take a look at the `Flow.collectAsStateWithLifecycle()` APIs",
"8:05 PM"
),
Message(
"Taylor Brooks",
"You can use all the same stuff",
"8:05 PM"
),
Message(
"Taylor Brooks",
"@aliconors Take a look at the `Flow.collectAsStateWithLifecycle()` APIs",
"8:05 PM"
),
Message(
"Taylor Brooks",
"You can use all the same stuff",
"8:05 PM"
),
Message(
"Taylor Brooks",
"@aliconors Take a look at the `Flow.collectAsStateWithLifecycle()` APIs",
"8:05 PM"
),
Message(
"Taylor Brooks",
"You can use all the same stuff",
"8:05 PM"
),
Message(
"Taylor Brooks",
"@aliconors Take a look at the `Flow.collectAsStateWithLifecycle()` APIs",
"8:05 PM"
),
Message(
"Taylor Brooks",
"You can use all the same stuff",
"8:05 PM"
),
Message(
"Taylor Brooks",
"@aliconors Take a look at the `Flow.collectAsStateWithLifecycle()` APIs",
"8:05 PM"
),
)

val exampleUiState = ConversationUiState(
initialMessages = initialMessages,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.compose.jetchat.widget

import android.content.Context
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceId
import androidx.glance.GlanceTheme
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.provideContent
import com.example.compose.jetchat.widget.composables.MessagesWidget

class MyAppWidget : GlanceAppWidget() {

override suspend fun provideGlance(context: Context, id: GlanceId) {
// Load data needed to render the AppWidget.
provideContent {
// Your Compose Code here
GlanceTheme {
MessagesWidget()
}
}
}

companion object {
internal val ICON_SQUARE = DpSize(50.dp, 50.dp)
internal val SMALL_SQUARE = DpSize(100.dp, 100.dp)
internal val MEDIUM_SQUARE = DpSize(150.dp, 150.dp)
}

override val sizeMode = SizeMode.Responsive(
setOf(
ICON_SQUARE,
SMALL_SQUARE,
MEDIUM_SQUARE,
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.compose.jetchat.widget

import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver

class WidgetReceiver : GlanceAppWidgetReceiver() {

override val glanceAppWidget: GlanceAppWidget
get() = MyAppWidget()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.example.compose.jetchat.widget.composables

import android.graphics.drawable.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceModifier
import androidx.glance.IconImageProvider
import androidx.glance.ImageProvider
import androidx.glance.LocalContext
import androidx.glance.LocalSize
import androidx.glance.action.Action
import androidx.glance.action.actionStartActivity
import androidx.glance.action.clickable
import androidx.glance.appwidget.components.Scaffold
import androidx.glance.appwidget.components.TitleBar
import androidx.glance.appwidget.lazy.LazyColumn
import androidx.glance.layout.Column
import androidx.glance.layout.Spacer
import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.height
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
import com.example.compose.jetchat.NavActivity
import com.example.compose.jetchat.R
import com.example.compose.jetchat.conversation.Message
import com.example.compose.jetchat.data.unreadMessages
import com.example.compose.jetchat.widget.MyAppWidget.Companion.ICON_SQUARE

@Composable
fun MessagesWidget() {
val size = LocalSize.current
if (size.width <= ICON_SQUARE.width) {
SmallIcon()
} else {
LargeMessages()
}
}

@Composable
fun SmallIcon() {
// IconImageProvider(Icon(R.drawable.ic_jetchat))
}

@Composable
fun LargeMessages() {
Scaffold(titleBar = {
TitleBar(
startIcon = ImageProvider(R.drawable.ic_jetchat),
iconColor = null,
title = LocalContext.current.getString(R.string.app_name_unreads)
)
}) {
val unreadMessage = unreadMessages
LazyColumn(modifier = GlanceModifier.fillMaxWidth()) {
unreadMessage.forEach {
item {
Column(modifier = GlanceModifier.fillMaxWidth()) {
MessageItem(it)
Spacer(modifier = GlanceModifier.height(10.dp))
}
}
}
}
}
}

@Composable
fun MessageItem(message: Message) {
Column(modifier = GlanceModifier.clickable(actionStartActivity<NavActivity>()).fillMaxWidth()) {
Text(
text = message.author,
style = TextStyle(fontWeight = FontWeight.Bold, color = ColorProvider(Color.White))
)
Text(
text = message.content,
style = TextStyle(fontWeight = FontWeight.Normal, color = ColorProvider(Color.White))
)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions Jetchat/app/src/main/res/layout/widget_error_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black30"
android:text="@string/widget_loading_error" />
</RelativeLayout>
11 changes: 11 additions & 0 deletions Jetchat/app/src/main/res/layout/widget_loading_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
2 changes: 2 additions & 0 deletions Jetchat/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@
<string name="more_options">More options</string>
<string name="touch_and_hold_to_record">Touch and hold to record</string>
<string name="record_message">Record voice message</string>
<string name="widget_loading_error">Cannot load Widget</string>
<string name="app_name_unreads">JetChat (Un-Reads)</string>

</resources>
9 changes: 9 additions & 0 deletions Jetchat/app/src/main/res/xml/my_app_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:resizeMode="none"
android:minHeight="128dp"
android:minWidth="256dp"
android:targetCellHeight="2"
android:targetCellWidth="4"
android:previewImage="@drawable/widget_icon"
android:initialLayout="@layout/widget_loading_layout" />