Skip to content

Commit

Permalink
feat: Update subscription on menu
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Nov 4, 2023
1 parent a921b72 commit 451cdab
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingPeriodicWorkPolicy.UPDATE
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkerParameters
import androidx.work.multiprocess.RemoteWorkManager
Expand Down Expand Up @@ -39,7 +39,7 @@ object SubscriptionUpdater {
// main process
RemoteWorkManager.getInstance(app).enqueueUniquePeriodicWork(
WORK_NAME,
ExistingPeriodicWorkPolicy.REPLACE,
UPDATE,
PeriodicWorkRequest.Builder(UpdateTask::class.java, minDelay, TimeUnit.MINUTES)
.apply {
if (minInitDelay > 0) setInitialDelay(minInitDelay, TimeUnit.SECONDS)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/io/nekohasekai/sagernet/ktx/Asyncs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package io.nekohasekai.sagernet.ktx

import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.*

fun block(block: suspend CoroutineScope.() -> Unit): suspend CoroutineScope.() -> Unit {
Expand All @@ -11,6 +13,9 @@ fun block(block: suspend CoroutineScope.() -> Unit): suspend CoroutineScope.() -
fun runOnDefaultDispatcher(block: suspend CoroutineScope.() -> Unit) =
GlobalScope.launch(Dispatchers.Default, block = block)

fun Fragment.runOnLifecycleDispatcher(block: suspend CoroutineScope.() -> Unit) =
lifecycleScope.launch(Dispatchers.Default, block = block)

suspend fun <T> onDefaultDispatcher(block: suspend CoroutineScope.() -> T) =
withContext(Dispatchers.Default, block = block)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import io.nekohasekai.sagernet.databinding.LayoutProfileListBinding
import io.nekohasekai.sagernet.databinding.LayoutProgressListBinding
import io.nekohasekai.sagernet.fmt.AbstractBean
import io.nekohasekai.sagernet.fmt.toUniversalLink
import io.nekohasekai.sagernet.group.GroupUpdater
import io.nekohasekai.sagernet.group.RawUpdater
import io.nekohasekai.sagernet.ktx.*
import io.nekohasekai.sagernet.plugin.PluginManager
Expand Down Expand Up @@ -419,6 +420,18 @@ class ConfigurationFragment @JvmOverloads constructor(
.show()
}

R.id.action_update_subscription -> {
val group = DataStore.currentGroup()
if (group.type != GroupType.SUBSCRIPTION) {
snackbar(R.string.group_not_subscription).show()
Logs.e("onMenuItemClick: Group(${group.displayName()}) is not subscription")
} else {
runOnLifecycleDispatcher {
GroupUpdater.startUpdate(group, true)
}
}
}

R.id.action_clear_traffic_statistics -> {
runOnDefaultDispatcher {
val profiles = SagerDatabase.proxyDao.getByGroup(DataStore.currentGroupId())
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/menu/add_profile_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
android:title=""
app:showAsAction="always">
<menu>
<item
android:id="@+id/action_update_subscription"
android:title="@string/update_current_subscription" />
<item
android:id="@+id/action_clear_traffic_statistics"
android:title="@string/clear_traffic_statistics" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,6 @@
<string name="ech_config">ECH 配置</string>
<string name="http_upgrade_host">HTTPUpgrade 主机</string>
<string name="http_upgrade_path">HTTPUpgrade 路径</string>
<string name="update_current_subscription">更新当前组订阅</string>
<string name="group_not_subscription">组类型不是订阅</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -528,5 +528,7 @@ Anyone can write advanced plugins, which can control NekoBox. please download an
<string name="dynamic_record_sizing_sum">If enable, the largest possible TLS record size is always used. When disable, the size of TLS records may be adjusted in an attempt to improve latency.</string>
<string name="http_upgrade_host">HTTPUpgrade Host</string>
<string name="http_upgrade_path">HTTPUpgrade Path</string>
<string name="update_current_subscription">Update current Group\'s subscription</string>
<string name="group_not_subscription">Group type is not subscription</string>

</resources>

0 comments on commit 451cdab

Please sign in to comment.