Skip to content

Commit

Permalink
feat(widget): new preview layout
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Info committed Oct 26, 2023
1 parent 628a6ba commit fa99940
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 47 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
targetSdk target_sdk

versionCode 1
versionName '0.1.0'
versionName '0.1.0-alpha.01'

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
8 changes: 7 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile

# Keep default constuctors of wiget classes
-keepnames class com.zjutjh.ijh.widget.* extends androidx.glance.appwidget.GlanceAppWidget
-keepclassmembers class com.zjutjh.ijh.widget.* extends androidx.glance.appwidget.GlanceAppWidget {
public <init>();
}
6 changes: 6 additions & 0 deletions app/src/main/kotlin/com/zjutjh/ijh/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.work.WorkManager
import com.zjutjh.ijh.ui.IJhApp
import com.zjutjh.ijh.ui.theme.IJhTheme
import com.zjutjh.ijh.util.ViewModelStoreMappingOwner
import com.zjutjh.ijh.widget.ScheduleWidget
import com.zjutjh.ijh.work.enqueueWidgetRefresh
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down Expand Up @@ -60,6 +63,9 @@ class MainActivity : ComponentActivity() {
// Make system bars inside the application's layout scope
WindowCompat.setDecorFitsSystemWindows(window, false)

// Update widget
WorkManager.getInstance(this).enqueueWidgetRefresh<ScheduleWidget>()

setContent {
IJhTheme {
IJhApp(
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ class ScheduleWidget : GlanceAppWidget() {
}
}
}
} else {
GText(context.getString(R.string.loading))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.work.WorkManager
import com.zjutjh.ijh.data.repository.CourseRepository
import com.zjutjh.ijh.data.repository.WeJhInfoRepository
import com.zjutjh.ijh.work.ScheduleWidgetUpdater
import com.zjutjh.ijh.work.enqueueWidgetRefresh
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
Expand All @@ -31,7 +32,6 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() {

override fun onEnabled(context: Context) {
super.onEnabled(context)

// Enter relevant functionality for when the first widget is created
val manager = WorkManager.getInstance(context)
val request = PeriodicWorkRequestBuilder<ScheduleWidgetUpdater>(
Expand All @@ -46,6 +46,8 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() {
request
)

manager.enqueueWidgetRefresh<ScheduleWidget>()

Log.i("ScheduleWidget", "Updater enqueued.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import com.zjutjh.ijh.widget.ScheduleWidget
import com.zjutjh.ijh.widget.ScheduleWidgetReceiver
import dagger.hilt.android.EntryPointAccessors

/**
* Worker that syncs data and updates widgets.
*
* **Note** On some devices, periodic update will work only when auto launch is enabled.
*/
class ScheduleWidgetUpdater(
private val context: Context,
workerParameters: WorkerParameters
Expand Down
68 changes: 41 additions & 27 deletions app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import androidx.glance.appwidget.updateAll
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import java.time.Duration
import java.util.concurrent.TimeUnit

/**
* This worker is used to refresh the widget periodically.
Expand All @@ -27,33 +30,6 @@ class WidgetRefreshWorker(
inline fun <reified T : GlanceAppWidget> getUniqueWorkName() =
WidgetRefreshWorker::class.java.simpleName + "_" + T::class.java.simpleName

/**
* Enqueue a [WidgetRefreshWorker] for the given [GlanceAppWidget].
* Do nothing if the worker is already enqueued.
* Default refresh interval is 15 minutes.
*/
inline fun <reified T : GlanceAppWidget> enqueue(context: Context) {
val manager = WorkManager.getInstance(context)
val request = PeriodicWorkRequestBuilder<WidgetRefreshWorker>(
Duration.ofMinutes(15), Duration.ofMinutes(5)
).setConstraints(
Constraints(requiresBatteryNotLow = true)
).setInputData(
workDataOf(INPUT_CLASS_NAME to T::class.java.name)
)
.build()

manager.enqueueUniquePeriodicWork(
getUniqueWorkName<T>(),
ExistingPeriodicWorkPolicy.KEEP,
request
)
}

inline fun <reified T : GlanceAppWidget> cancel(context: Context) {
val manager = WorkManager.getInstance(context)
manager.cancelUniqueWork(getUniqueWorkName<T>())
}
}

/**
Expand All @@ -78,5 +54,43 @@ class WidgetRefreshWorker(
Result.failure()
}
}
}

inline fun <reified T : GlanceAppWidget> WorkManager.enqueueWidgetRefresh(force: Boolean = false) {
enqueueUniqueWork(
WidgetRefreshWorker.getUniqueWorkName<T>(),
if (force) ExistingWorkPolicy.REPLACE else ExistingWorkPolicy.KEEP,
OneTimeWorkRequestBuilder<WidgetRefreshWorker>()
.setInitialDelay(1, TimeUnit.SECONDS)
.setInputData(
workDataOf(WidgetRefreshWorker.INPUT_CLASS_NAME to T::class.java.name)
)
.build()
)
}

/**
* Enqueue a [WidgetRefreshWorker] for the given [GlanceAppWidget].
* Do nothing if the worker is already enqueued.
* Default refresh interval is 15 minutes.
*/
inline fun <reified T : GlanceAppWidget> WorkManager.enqueuePeriodicWidgetRefresh() {
val request = PeriodicWorkRequestBuilder<WidgetRefreshWorker>(
Duration.ofMinutes(15), Duration.ofMinutes(5)
).setConstraints(
Constraints(requiresBatteryNotLow = true)
).setInputData(
workDataOf(WidgetRefreshWorker.INPUT_CLASS_NAME to T::class.java.name)
)
.build()

enqueueUniquePeriodicWork(
WidgetRefreshWorker.getUniqueWorkName<T>(),
ExistingPeriodicWorkPolicy.KEEP,
request
)
}

inline fun <reified T : GlanceAppWidget> WorkManager.cancelPeriodicWidgetRefresh() {
cancelUniqueWork(WidgetRefreshWorker.getUniqueWorkName<T>())
}
1 change: 0 additions & 1 deletion app/src/main/res/drawable/baseline_autorenew_24.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/res/layout/widget_loading.xml

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/res/layout/widget_schedule_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorSurface"
android:orientation="vertical"
android:theme="@style/Theme.IJh.AppWidgetContainer">

<LinearLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:text="@string/schedule"
android:textSize="20sp"
android:textStyle="bold" />

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="16dp"
android:foregroundTint="?attr/colorOnBackground"
android:src="@drawable/baseline_autorenew_24"
app:tint="?attr/colorOnBackground" />
</LinearLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="5dp"
android:background="?attr/colorOutline" />


<!-- <FrameLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="50dp"-->
<!-- android:layout_margin="10dp"-->
<!-- android:background="?attr/colorPrimaryContainer" />-->

<!-- <FrameLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="50dp"-->
<!-- android:layout_margin="10dp"-->
<!-- android:background="?attr/colorSecondaryContainer" />-->

<!-- <FrameLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="50dp"-->
<!-- android:layout_margin="10dp"-->
<!-- android:background="?attr/colorSecondaryContainer" />-->

</LinearLayout>
6 changes: 3 additions & 3 deletions app/src/main/res/xml/app_widget_schedule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:description="@string/app_widget_schedule_description"
android:initialKeyguardLayout="@layout/widget_loading"
android:initialLayout="@layout/widget_loading"
android:initialKeyguardLayout="@layout/widget_schedule_layout"
android:initialLayout="@layout/widget_schedule_layout"
android:minWidth="200dp"
android:minHeight="160dp"
android:minResizeWidth="200dp"
android:minResizeHeight="160dp"
android:previewLayout="@layout/widget_loading"
android:previewLayout="@layout/widget_schedule_layout"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="3"
android:targetCellHeight="3"
Expand Down

0 comments on commit fa99940

Please sign in to comment.