Skip to content

Commit

Permalink
Add a progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 14, 2024
1 parent a0223a3 commit 828a393
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 94 deletions.
57 changes: 49 additions & 8 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -175,6 +174,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
}
}
mainViewModel.startListenBroadcast()
mainViewModel.copyAssets(assets)
}

private fun initGroupTab() {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -451,7 +492,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
//dialog.dismiss()
binding.pbWaiting.hide()
}
}
}
}

private fun importConfigCustomClipboard()
Expand Down
19 changes: 9 additions & 10 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -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
Expand Down
121 changes: 45 additions & 76 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<AngApplication>(),
serverListCopy
)
launch(Dispatchers.Main) {
if (ret == 0)
getApplication<AngApplication>().toast(R.string.toast_success)
else
getApplication<AngApplication>().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<AngApplication>(),
serverListCopy
)
return ret
}


fun testAllTcping() {
tcpingTestScope.coroutineContext[Job]?.cancelChildren()
SpeedtestUtil.closeAllTcpSockets()
Expand Down Expand Up @@ -279,81 +273,56 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
return -1
}

fun removeDuplicateServer() {
viewModelScope.launch(Dispatchers.Default) {
val serversCacheCopy = mutableListOf<Pair<String, ServerConfig>>()
for (it in serversCache) {
val config = MmkvManager.decodeServerConfig(it.guid) ?: continue
serversCacheCopy.add(Pair(it.guid, config))
}
fun removeDuplicateServer() : Int {
val serversCacheCopy = mutableListOf<Pair<String, ServerConfig>>()
for (it in serversCache) {
val config = MmkvManager.decodeServerConfig(it.guid) ?: continue
serversCacheCopy.add(Pair(it.guid, config))
}

val deleteServer = mutableListOf<String>()
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<String>()
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<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() {
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()
}


Expand Down

0 comments on commit 828a393

Please sign in to comment.