feat: show toast after profile updated

This commit is contained in:
Steve Johnson 2023-10-31 22:14:47 +08:00
parent bd82ba7773
commit b6f4f7ac62
13 changed files with 107 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import com.github.kr328.clash.util.ActivityResultLifecycle
import com.github.kr328.clash.util.ApplicationObserver
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@ -39,7 +40,9 @@ abstract class BaseActivity<D : Design<*>> :
ClashStop,
ClashStart,
ProfileLoaded,
ProfileChanged
ProfileChanged,
ProfileUpdateCompleted,
ProfileUpdateFailed
}
@ -177,6 +180,14 @@ abstract class BaseActivity<D : Design<*>> :
events.trySend(Event.ProfileChanged)
}
override fun onProfileUpdateCompleted(uuid: UUID?) {
events.trySend(Event.ProfileUpdateCompleted)
}
override fun onProfileUpdateFailed(uuid: UUID?, reason: String?) {
events.trySend(Event.ProfileUpdateFailed)
}
override fun onProfileLoaded() {
events.trySend(Event.ProfileLoaded)
}

View File

@ -1,15 +1,23 @@
package com.github.kr328.clash
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.github.kr328.clash.common.util.intent
import com.github.kr328.clash.common.util.setUUID
import com.github.kr328.clash.common.util.ticker
import com.github.kr328.clash.design.ProfilesDesign
import com.github.kr328.clash.design.ui.ToastDuration
import com.github.kr328.clash.R
import com.github.kr328.clash.service.model.Profile
import com.github.kr328.clash.util.withProfile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.selects.select
import kotlinx.coroutines.withContext
import java.util.*
import java.util.concurrent.TimeUnit
class ProfilesActivity : BaseActivity<ProfilesDesign>() {
@ -83,4 +91,37 @@ class ProfilesActivity : BaseActivity<ProfilesDesign>() {
patchProfiles(queryAll())
}
}
override fun onProfileUpdateCompleted(uuid: UUID?) {
if(uuid == null)
return;
launch {
var name: String? = null;
withProfile {
name = queryByUUID(uuid)?.name
}
design?.showToast(
getString(R.string.toast_profile_updated_complete, name),
ToastDuration.Long
)
}
}
override fun onProfileUpdateFailed(uuid: UUID?, reason: String?) {
if(uuid == null)
return;
launch {
var name: String? = null;
withProfile {
name = queryByUUID(uuid)?.name
}
design?.showToast(
getString(R.string.toast_profile_updated_failed, name, reason),
ToastDuration.Long
){
setAction(R.string.edit) {
startActivity(PropertiesActivity::class.intent.setUUID(uuid))
}
}
}
}
}

View File

@ -7,6 +7,7 @@ import android.content.Intent
import android.content.IntentFilter
import com.github.kr328.clash.common.constants.Intents
import com.github.kr328.clash.common.log.Log
import java.util.*
class Broadcasts(private val context: Application) {
interface Observer {
@ -14,6 +15,8 @@ class Broadcasts(private val context: Application) {
fun onStarted()
fun onStopped(cause: String?)
fun onProfileChanged()
fun onProfileUpdateCompleted(uuid: UUID?)
fun onProfileUpdateFailed(uuid: UUID?, reason: String?)
fun onProfileLoaded()
}
@ -52,6 +55,17 @@ class Broadcasts(private val context: Application) {
receivers.forEach {
it.onProfileChanged()
}
Intents.ACTION_PROFILE_UPDATE_COMPLETED ->
receivers.forEach {
it.onProfileUpdateCompleted(
UUID.fromString(intent.getStringExtra(Intents.EXTRA_UUID)))
}
Intents.ACTION_PROFILE_UPDATE_FAILED ->
receivers.forEach {
it.onProfileUpdateFailed(
UUID.fromString(intent.getStringExtra(Intents.EXTRA_UUID)),
intent.getStringExtra(Intents.EXTRA_FAIL_REASON))
}
Intents.ACTION_PROFILE_LOADED -> {
receivers.forEach {
it.onProfileLoaded()
@ -79,6 +93,8 @@ class Broadcasts(private val context: Application) {
addAction(Intents.ACTION_CLASH_STARTED)
addAction(Intents.ACTION_CLASH_STOPPED)
addAction(Intents.ACTION_PROFILE_CHANGED)
addAction(Intents.ACTION_PROFILE_UPDATE_COMPLETED)
addAction(Intents.ACTION_PROFILE_UPDATE_FAILED)
addAction(Intents.ACTION_PROFILE_LOADED)
})

View File

@ -14,6 +14,8 @@ object Intents {
val ACTION_CLASH_STOPPED = "$packageName.intent.action.CLASH_STOPPED"
val ACTION_CLASH_REQUEST_STOP = "$packageName.intent.action.CLASH_REQUEST_STOP"
val ACTION_PROFILE_CHANGED = "$packageName.intent.action.PROFILE_CHANGED"
val ACTION_PROFILE_UPDATE_COMPLETED = "$packageName.intent.action.PROFILE_UPDATE_COMPLETED"
val ACTION_PROFILE_UPDATE_FAILED = "$packageName.intent.action.PROFILE_UPDATE_FAILED"
val ACTION_PROFILE_REQUEST_UPDATE = "$packageName.intent.action.REQUEST_UPDATE"
val ACTION_PROFILE_SCHEDULE_UPDATES = "$packageName.intent.action.SCHEDULE_UPDATES"
val ACTION_PROFILE_LOADED = "$packageName.intent.action.PROFILE_LOADED"
@ -21,4 +23,5 @@ object Intents {
const val EXTRA_STOP_REASON = "stop_reason"
const val EXTRA_UUID = "uuid"
const val EXTRA_FAIL_REASON = "fail_reason"
}

View File

@ -243,4 +243,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -243,4 +243,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -308,4 +308,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -240,4 +240,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -240,4 +240,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -243,4 +243,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -308,4 +308,6 @@
<string name="geofile_unknown_db_format">Unknown Database format</string>
<string name="geofile_unknown_db_format_message">Only %1$s are supported</string>
<string name="geofile_imported">%1$s imported</string>
<string name="toast_profile_updated_complete">Update profile %s completed</string>
<string name="toast_profile_updated_failed">Update profile %1$s failed: %2$s</string>
</resources>

View File

@ -15,6 +15,8 @@ import com.github.kr328.clash.common.id.UndefinedIds
import com.github.kr328.clash.common.util.setUUID
import com.github.kr328.clash.common.util.uuid
import com.github.kr328.clash.service.data.ImportedDao
import com.github.kr328.clash.service.util.sendProfileUpdateCompleted
import com.github.kr328.clash.service.util.sendProfileUpdateFailed
import kotlinx.coroutines.*
import java.util.*
import java.util.concurrent.TimeUnit
@ -176,6 +178,8 @@ class ProfileWorker : BaseService() {
NotificationManagerCompat.from(this)
.notify(id, notification)
sendProfileUpdateCompleted(uuid)
}
private fun failed(uuid: UUID, name: String, reason: String) {
@ -191,6 +195,8 @@ class ProfileWorker : BaseService() {
NotificationManagerCompat.from(this)
.notify(id, notification)
sendProfileUpdateFailed(uuid, reason)
}
companion object {

View File

@ -27,6 +27,21 @@ fun Context.sendProfileLoaded(uuid: UUID) {
sendBroadcastSelf(intent)
}
fun Context.sendProfileUpdateCompleted(uuid: UUID) {
val intent = Intent(Intents.ACTION_PROFILE_UPDATE_COMPLETED)
.putExtra(Intents.EXTRA_UUID, uuid.toString())
sendBroadcastSelf(intent)
}
fun Context.sendProfileUpdateFailed(uuid: UUID, reason: String) {
val intent = Intent(Intents.ACTION_PROFILE_UPDATE_FAILED)
.putExtra(Intents.EXTRA_UUID, uuid.toString())
.putExtra(Intents.EXTRA_FAIL_REASON, reason)
sendBroadcastSelf(intent)
}
fun Context.sendOverrideChanged() {
val intent = Intent(Intents.ACTION_OVERRIDE_CHANGED)