From 828a39331baced1e0394fa0048bdc5c195a73417 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 14 Aug 2024 17:55:41 +0800 Subject: [PATCH] Add a progress bar --- .../kotlin/com/v2ray/ang/ui/MainActivity.kt | 57 +++++++-- .../com/v2ray/ang/ui/SubEditActivity.kt | 19 ++- .../com/v2ray/ang/viewmodel/MainViewModel.kt | 121 +++++++----------- 3 files changed, 103 insertions(+), 94 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index d12c9cf1..65948271 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -128,7 +128,6 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList initGroupTab() setupViewModel() - mainViewModel.copyAssets(assets) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { RxPermissions(this) @@ -175,6 +174,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } } mainViewModel.startListenBroadcast() + mainViewModel.copyAssets(assets) } private fun initGroupTab() { @@ -310,7 +310,18 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } R.id.export_all -> { - mainViewModel.exportAllServer() + binding.pbWaiting.show() + lifecycleScope.launch(Dispatchers.IO) { + val ret = mainViewModel.exportAllServer() + launch(Dispatchers.Main) { + if (ret == 0) + toast(R.string.toast_success) + else + toast(R.string.toast_failure) + binding.pbWaiting.hide() + } + } + true } @@ -332,7 +343,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList R.id.del_all_config -> { AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) .setPositiveButton(android.R.string.ok) { _, _ -> - mainViewModel.removeAllServer() + binding.pbWaiting.show() + lifecycleScope.launch(Dispatchers.IO) { + mainViewModel.removeAllServer() + launch(Dispatchers.Main) { + mainViewModel.reloadServerList() + binding.pbWaiting.hide() + } + } } .setNegativeButton(android.R.string.no) {_, _ -> //do noting @@ -343,18 +361,34 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList R.id.del_duplicate_config-> { AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) .setPositiveButton(android.R.string.ok) { _, _ -> - mainViewModel.removeDuplicateServer() + binding.pbWaiting.show() + lifecycleScope.launch(Dispatchers.IO) { + val ret = mainViewModel.removeDuplicateServer() + launch(Dispatchers.Main) { + mainViewModel.reloadServerList() + toast(getString(R.string.title_del_duplicate_config_count, ret)) + binding.pbWaiting.hide() + } + } } - .setNegativeButton(android.R.string.no) {_, _ -> + .setNegativeButton(android.R.string.no) { _, _ -> //do noting } .show() true } + R.id.del_invalid_config -> { AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) .setPositiveButton(android.R.string.ok) { _, _ -> - mainViewModel.removeInvalidServer() + binding.pbWaiting.show() + lifecycleScope.launch(Dispatchers.IO) { + mainViewModel.removeInvalidServer() + launch(Dispatchers.Main) { + mainViewModel.reloadServerList() + binding.pbWaiting.hide() + } + } } .setNegativeButton(android.R.string.no) {_, _ -> //do noting @@ -363,7 +397,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList true } R.id.sort_by_test_results -> { - mainViewModel.sortByTestResults() + binding.pbWaiting.show() + lifecycleScope.launch(Dispatchers.IO) { + mainViewModel.sortByTestResults() + launch(Dispatchers.Main) { + mainViewModel.reloadServerList() + binding.pbWaiting.hide() + } + } true } else -> super.onOptionsItemSelected(item) @@ -451,7 +492,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList //dialog.dismiss() binding.pbWaiting.hide() } - } + } } private fun importConfigCustomClipboard() diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt index 8106aedb..1f52f6d2 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt @@ -5,22 +5,17 @@ import android.text.TextUtils import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AlertDialog -import androidx.work.Constraints -import androidx.work.ExistingPeriodicWorkPolicy -import androidx.work.NetworkType -import androidx.work.PeriodicWorkRequest -import androidx.work.multiprocess.RemoteWorkManager +import androidx.lifecycle.lifecycleScope import com.google.gson.Gson import com.tencent.mmkv.MMKV -import com.v2ray.ang.AngApplication import com.v2ray.ang.R import com.v2ray.ang.databinding.ActivitySubEditBinding import com.v2ray.ang.dto.SubscriptionItem import com.v2ray.ang.extension.toast -import com.v2ray.ang.service.SubscriptionUpdater import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.Utils -import java.util.concurrent.TimeUnit +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class SubEditActivity : BaseActivity() { private val binding by lazy {ActivitySubEditBinding.inflate(layoutInflater)} @@ -106,8 +101,12 @@ class SubEditActivity : BaseActivity() { if (editSubId.isNotEmpty()) { AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) .setPositiveButton(android.R.string.ok) { _, _ -> - MmkvManager.removeSubscription(editSubId) - finish() + lifecycleScope.launch(Dispatchers.IO) { + MmkvManager.removeSubscription(editSubId) + launch(Dispatchers.Main) { + finish() + } + } } .setNegativeButton(android.R.string.no) {_, _ -> // do nothing diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt index 284d3582..cb9a736b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt @@ -173,28 +173,22 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } - fun exportAllServer() { - viewModelScope.launch(Dispatchers.Default) { - val serverListCopy = - if (subscriptionId.isNullOrEmpty()) { - serverList - } else { - serversCache.map { it.guid }.toList() - } - - val ret = AngConfigManager.shareNonCustomConfigsToClipboard( - getApplication(), - serverListCopy - ) - launch(Dispatchers.Main) { - if (ret == 0) - getApplication().toast(R.string.toast_success) - else - getApplication().toast(R.string.toast_failure) + fun exportAllServer() : Int { + val serverListCopy = + if (subscriptionId.isNullOrEmpty()) { + serverList + } else { + serversCache.map { it.guid }.toList() } - } + + val ret = AngConfigManager.shareNonCustomConfigsToClipboard( + getApplication(), + serverListCopy + ) + return ret } + fun testAllTcping() { tcpingTestScope.coroutineContext[Job]?.cancelChildren() SpeedtestUtil.closeAllTcpSockets() @@ -279,81 +273,56 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { return -1 } - fun removeDuplicateServer() { - viewModelScope.launch(Dispatchers.Default) { - val serversCacheCopy = mutableListOf>() - for (it in serversCache) { - val config = MmkvManager.decodeServerConfig(it.guid) ?: continue - serversCacheCopy.add(Pair(it.guid, config)) - } + fun removeDuplicateServer() : Int { + val serversCacheCopy = mutableListOf>() + for (it in serversCache) { + val config = MmkvManager.decodeServerConfig(it.guid) ?: continue + serversCacheCopy.add(Pair(it.guid, config)) + } - val deleteServer = mutableListOf() - serversCacheCopy.forEachIndexed { index, it -> - val outbound = it.second.getProxyOutbound() - serversCacheCopy.forEachIndexed { index2, it2 -> - if (index2 > index) { - val outbound2 = it2.second.getProxyOutbound() - if (outbound == outbound2 && !deleteServer.contains(it2.first)) { - deleteServer.add(it2.first) - } + val deleteServer = mutableListOf() + serversCacheCopy.forEachIndexed { index, it -> + val outbound = it.second.getProxyOutbound() + serversCacheCopy.forEachIndexed { index2, it2 -> + if (index2 > index) { + val outbound2 = it2.second.getProxyOutbound() + if (outbound == outbound2 && !deleteServer.contains(it2.first)) { + deleteServer.add(it2.first) } } } - for (it in deleteServer) { - MmkvManager.removeServer(it) - } - launch(Dispatchers.Main) { - reloadServerList() - getApplication().toast( - getApplication().getString( - R.string.title_del_duplicate_config_count, - deleteServer.count() - ) - ) - } - } + for (it in deleteServer) { + MmkvManager.removeServer(it) + } + + return deleteServer.count() } fun removeAllServer() { - viewModelScope.launch(Dispatchers.Default) { - if (subscriptionId.isNullOrEmpty()) { - MmkvManager.removeAllServer() - } else { - val serversCopy = serversCache.toList() - for (item in serversCopy) { - MmkvManager.removeServer(item.guid) - } - } - launch(Dispatchers.Main) { - reloadServerList() + if (subscriptionId.isNullOrEmpty()) { + MmkvManager.removeAllServer() + } else { + val serversCopy = serversCache.toList() + for (item in serversCopy) { + MmkvManager.removeServer(item.guid) } } } fun removeInvalidServer() { - viewModelScope.launch(Dispatchers.Default) { - if (subscriptionId.isNullOrEmpty()) { - MmkvManager.removeInvalidServer("") - } else { - val serversCopy = serversCache.toList() - for (item in serversCopy) { - MmkvManager.removeInvalidServer(item.guid) - } - } - launch(Dispatchers.Main) { - reloadServerList() + if (subscriptionId.isNullOrEmpty()) { + MmkvManager.removeInvalidServer("") + } else { + val serversCopy = serversCache.toList() + for (item in serversCopy) { + MmkvManager.removeInvalidServer(item.guid) } } } fun sortByTestResults() { - viewModelScope.launch(Dispatchers.Default) { - MmkvManager.sortByTestResults() - launch(Dispatchers.Main) { - reloadServerList() - } - } + MmkvManager.sortByTestResults() }