Add a progress bar

This commit is contained in:
2dust committed 2024-08-14 17:55:41 +08:00
1 parent a0223a3eee
commit 828a39331b
3 files changed
+103 -94

No files matched your search

@@ -128,7 +128,6 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
initGroupTab() initGroupTab()
setupViewModel() setupViewModel()
mainViewModel.copyAssets(assets)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
RxPermissions(this) RxPermissions(this)
@@ -175,6 +174,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
} }
} }
mainViewModel.startListenBroadcast() mainViewModel.startListenBroadcast()
mainViewModel.copyAssets(assets)
} }
private fun initGroupTab() { private fun initGroupTab() {
@@ -310,7 +310,18 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
} }
R.id.export_all -> { 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 true
} }
@@ -332,7 +343,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
R.id.del_all_config -> { R.id.del_all_config -> {
AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm)
.setPositiveButton(android.R.string.ok) { _, _ -> .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) {_, _ -> .setNegativeButton(android.R.string.no) {_, _ ->
//do noting //do noting
@@ -343,18 +361,34 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
R.id.del_duplicate_config-> { R.id.del_duplicate_config-> {
AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm)
.setPositiveButton(android.R.string.ok) { _, _ -> .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 //do noting
} }
.show() .show()
true true
} }
R.id.del_invalid_config -> { R.id.del_invalid_config -> {
AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm)
.setPositiveButton(android.R.string.ok) { _, _ -> .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) {_, _ -> .setNegativeButton(android.R.string.no) {_, _ ->
//do noting //do noting
@@ -363,7 +397,14 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
true true
} }
R.id.sort_by_test_results -> { 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 true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
@@ -451,7 +492,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
//dialog.dismiss() //dialog.dismiss()
binding.pbWaiting.hide() binding.pbWaiting.hide()
} }
} }
} }
private fun importConfigCustomClipboard() private fun importConfigCustomClipboard()
@@ -5,22 +5,17 @@ import android.text.TextUtils
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.work.Constraints import androidx.lifecycle.lifecycleScope
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.multiprocess.RemoteWorkManager
import com.google.gson.Gson import com.google.gson.Gson
import com.tencent.mmkv.MMKV import com.tencent.mmkv.MMKV
import com.v2ray.ang.AngApplication
import com.v2ray.ang.R import com.v2ray.ang.R
import com.v2ray.ang.databinding.ActivitySubEditBinding import com.v2ray.ang.databinding.ActivitySubEditBinding
import com.v2ray.ang.dto.SubscriptionItem import com.v2ray.ang.dto.SubscriptionItem
import com.v2ray.ang.extension.toast import com.v2ray.ang.extension.toast
import com.v2ray.ang.service.SubscriptionUpdater
import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.MmkvManager
import com.v2ray.ang.util.Utils import com.v2ray.ang.util.Utils
import java.util.concurrent.TimeUnit import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class SubEditActivity : BaseActivity() { class SubEditActivity : BaseActivity() {
private val binding by lazy {ActivitySubEditBinding.inflate(layoutInflater)} private val binding by lazy {ActivitySubEditBinding.inflate(layoutInflater)}
@@ -106,8 +101,12 @@ class SubEditActivity : BaseActivity() {
if (editSubId.isNotEmpty()) { if (editSubId.isNotEmpty()) {
AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm)
.setPositiveButton(android.R.string.ok) { _, _ -> .setPositiveButton(android.R.string.ok) { _, _ ->
MmkvManager.removeSubscription(editSubId) lifecycleScope.launch(Dispatchers.IO) {
finish() MmkvManager.removeSubscription(editSubId)
launch(Dispatchers.Main) {
finish()
}
}
} }
.setNegativeButton(android.R.string.no) {_, _ -> .setNegativeButton(android.R.string.no) {_, _ ->
// do nothing // do nothing
@@ -173,28 +173,22 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
} }
fun exportAllServer() { fun exportAllServer() : Int {
viewModelScope.launch(Dispatchers.Default) { val serverListCopy =
val serverListCopy = if (subscriptionId.isNullOrEmpty()) {
if (subscriptionId.isNullOrEmpty()) { serverList
serverList } else {
} else { serversCache.map { it.guid }.toList()
serversCache.map { it.guid }.toList()
}
val ret = AngConfigManager.shareNonCustomConfigsToClipboard(
getApplication<AngApplication>(),
serverListCopy
)
launch(Dispatchers.Main) {
if (ret == 0)
getApplication<AngApplication>().toast(R.string.toast_success)
else
getApplication<AngApplication>().toast(R.string.toast_failure)
} }
}
val ret = AngConfigManager.shareNonCustomConfigsToClipboard(
getApplication<AngApplication>(),
serverListCopy
)
return ret
} }
fun testAllTcping() { fun testAllTcping() {
tcpingTestScope.coroutineContext[Job]?.cancelChildren() tcpingTestScope.coroutineContext[Job]?.cancelChildren()
SpeedtestUtil.closeAllTcpSockets() SpeedtestUtil.closeAllTcpSockets()
@@ -279,81 +273,56 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return -1 return -1
} }
fun removeDuplicateServer() { fun removeDuplicateServer() : Int {
viewModelScope.launch(Dispatchers.Default) { val serversCacheCopy = mutableListOf<Pair<String, ServerConfig>>()
val serversCacheCopy = mutableListOf<Pair<String, ServerConfig>>() for (it in serversCache) {
for (it in serversCache) { val config = MmkvManager.decodeServerConfig(it.guid) ?: continue
val config = MmkvManager.decodeServerConfig(it.guid) ?: continue serversCacheCopy.add(Pair(it.guid, config))
serversCacheCopy.add(Pair(it.guid, config)) }
}
val deleteServer = mutableListOf<String>() val deleteServer = mutableListOf<String>()
serversCacheCopy.forEachIndexed { index, it -> serversCacheCopy.forEachIndexed { index, it ->
val outbound = it.second.getProxyOutbound() val outbound = it.second.getProxyOutbound()
serversCacheCopy.forEachIndexed { index2, it2 -> serversCacheCopy.forEachIndexed { index2, it2 ->
if (index2 > index) { if (index2 > index) {
val outbound2 = it2.second.getProxyOutbound() val outbound2 = it2.second.getProxyOutbound()
if (outbound == outbound2 && !deleteServer.contains(it2.first)) { if (outbound == outbound2 && !deleteServer.contains(it2.first)) {
deleteServer.add(it2.first) deleteServer.add(it2.first)
}
} }
} }
} }
for (it in deleteServer) {
MmkvManager.removeServer(it)
}
launch(Dispatchers.Main) {
reloadServerList()
getApplication<AngApplication>().toast(
getApplication<AngApplication>().getString(
R.string.title_del_duplicate_config_count,
deleteServer.count()
)
)
}
} }
for (it in deleteServer) {
MmkvManager.removeServer(it)
}
return deleteServer.count()
} }
fun removeAllServer() { fun removeAllServer() {
viewModelScope.launch(Dispatchers.Default) { if (subscriptionId.isNullOrEmpty()) {
if (subscriptionId.isNullOrEmpty()) { MmkvManager.removeAllServer()
MmkvManager.removeAllServer() } else {
} else { val serversCopy = serversCache.toList()
val serversCopy = serversCache.toList() for (item in serversCopy) {
for (item in serversCopy) { MmkvManager.removeServer(item.guid)
MmkvManager.removeServer(item.guid)
}
}
launch(Dispatchers.Main) {
reloadServerList()
} }
} }
} }
fun removeInvalidServer() { fun removeInvalidServer() {
viewModelScope.launch(Dispatchers.Default) { if (subscriptionId.isNullOrEmpty()) {
if (subscriptionId.isNullOrEmpty()) { MmkvManager.removeInvalidServer("")
MmkvManager.removeInvalidServer("") } else {
} else { val serversCopy = serversCache.toList()
val serversCopy = serversCache.toList() for (item in serversCopy) {
for (item in serversCopy) { MmkvManager.removeInvalidServer(item.guid)
MmkvManager.removeInvalidServer(item.guid)
}
}
launch(Dispatchers.Main) {
reloadServerList()
} }
} }
} }
fun sortByTestResults() { fun sortByTestResults() {
viewModelScope.launch(Dispatchers.Default) { MmkvManager.sortByTestResults()
MmkvManager.sortByTestResults()
launch(Dispatchers.Main) {
reloadServerList()
}
}
} }