Skip to content

Commit

Permalink
refactor: use native apis to build notification
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jun 14, 2024
1 parent e6204b8 commit 99dc458
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
62 changes: 40 additions & 22 deletions app/src/main/java/com/osfans/trime/ui/setup/SetupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

package com.osfans.trime.ui.setup

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
Expand All @@ -19,18 +22,19 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.NotificationUtils
import com.osfans.trime.R
import com.osfans.trime.databinding.ActivitySetupBinding
import com.osfans.trime.ui.setup.SetupPage.Companion.firstUndonePage
import com.osfans.trime.ui.setup.SetupPage.Companion.isLastPage
import splitties.systemservices.notificationManager

class SetupActivity : FragmentActivity() {
private lateinit var viewPager: ViewPager2
private val viewModel: SetupViewModel by viewModels()

companion object {
private var binaryCount = false
private const val CHANNEL_ID = "setup"
private const val NOTIFY_ID = 87463

fun shouldSetup() = !binaryCount && SetupPage.hasUndonePage()
Expand All @@ -54,7 +58,7 @@ class SetupActivity : FragmentActivity() {
val prevButton =
binding.prevButton.apply {
text = getString(R.string.setup__prev)
setOnClickListener { viewPager.currentItem = viewPager.currentItem - 1 }
setOnClickListener { viewPager.currentItem -= 1 }
}
binding.skipButton.apply {
text = getString(R.string.setup__skip)
Expand All @@ -71,8 +75,8 @@ class SetupActivity : FragmentActivity() {
val nextButton =
binding.nextButton.apply {
setOnClickListener {
if (viewPager.currentItem != SetupPage.values().size - 1) {
viewPager.currentItem = viewPager.currentItem + 1
if (viewPager.currentItem != SetupPage.entries.size - 1) {
viewPager.currentItem += 1
} else {
finish()
}
Expand Down Expand Up @@ -110,6 +114,7 @@ class SetupActivity : FragmentActivity() {
// Skip to undone page
firstUndonePage()?.let { viewPager.currentItem = it.ordinal }
binaryCount = true
createNotificationChannel()
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
Expand All @@ -118,38 +123,51 @@ class SetupActivity : FragmentActivity() {
(fragment as SetupFragment).sync()
}

private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel(
CHANNEL_ID,
getText(R.string.setup_channel),
NotificationManager.IMPORTANCE_HIGH,
).apply { description = CHANNEL_ID }
notificationManager.createNotificationChannel(channel)
}
}

override fun onPause() {
if (SetupPage.hasUndonePage()) {
NotificationUtils.notify(NOTIFY_ID) { param ->
param.setSmallIcon(R.drawable.ic_trime_status)
.setContentTitle(getText(R.string.trime_app_name))
.setContentText(getText(R.string.setup__notify_hint))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(
PendingIntent.getActivity(
this,
0,
Intent(this, javaClass),
PendingIntent.FLAG_IMMUTABLE,
),
)
.setAutoCancel(true)
}
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_trime_status)
.setContentTitle(getText(R.string.trime_app_name))
.setContentText(getText(R.string.setup__notify_hint))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(
PendingIntent.getActivity(
this,
0,
Intent(this, javaClass),
PendingIntent.FLAG_IMMUTABLE,
),
)
.setAutoCancel(true)
.build()
.let { notificationManager.notify(NOTIFY_ID, it) }
}
super.onPause()
}

override fun onResume() {
NotificationUtils.cancel(NOTIFY_ID)
notificationManager.cancel(NOTIFY_ID)
super.onResume()
}

private inner class Adapter : FragmentStateAdapter(this) {
override fun getItemCount(): Int = SetupPage.values().size
override fun getItemCount(): Int = SetupPage.entries.size

override fun createFragment(position: Int): Fragment =
SetupFragment().apply {
arguments = bundleOf("page" to SetupPage.values()[position])
arguments = bundleOf("page" to SetupPage.entries[position])
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
</string-array>
<string name="theme_trime">默认</string>
<string name="theme_tongwenfeng">同文风</string>
<string name="setup_channel">设置向导</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
</string-array>
<string name="theme_trime">預設</string>
<string name="theme_tongwenfeng">同文風</string>
<string name="setup_channel">設定精靈</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,5 @@ SPDX-License-Identifier: GPL-3.0-or-later
</string-array>
<string name="theme_trime">default</string>
<string name="theme_tongwenfeng">tongwenfeng</string>
<string name="setup_channel">Setup Wizard</string>
</resources>

0 comments on commit 99dc458

Please sign in to comment.