Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.GroupRepo
import io.nekohasekai.sagernet.database.ProfileManager
import io.nekohasekai.sagernet.database.ProxyEntity
import io.nekohasekai.sagernet.group.SubscriptionClient
import io.nekohasekai.sagernet.ktx.Logs
import io.nekohasekai.sagernet.ktx.confirmAction
import io.nekohasekai.sagernet.ktx.nameList
import io.nekohasekai.sagernet.ktx.showAllowingStateLoss
import io.nekohasekai.sagernet.ktx.snackbar
import io.nekohasekai.sagernet.ui.ConfigurationFragment
import io.nekohasekai.sagernet.ui.MainActivity
import io.nekohasekai.sagernet.widget.QRCodeDialog

/**
Expand Down Expand Up @@ -123,6 +126,16 @@ class SelectionMode(private val host: ConfigurationFragment) {
refreshPinTitle()
}

private fun invertSelection() {
val shown = host.listFor(groupId)?.adapter?.displayedIds ?: return
for (id in shown) {
if (!checked.remove(id)) checked.add(id)
}
if (checked.isEmpty()) return finish()
host.onSelectionChanged(null)
refreshPinTitle()
}

/** Every checked profile is pinned, so the action unpins (refresh_pin_action). */
private fun allPinned(): Boolean {
val adapter = host.listFor(groupId)?.adapter ?: return false
Expand Down Expand Up @@ -155,9 +168,12 @@ class SelectionMode(private val host: ConfigurationFragment) {
}

R.id.action_selection_all -> selectAll()
R.id.action_selection_invert -> invertSelection()
R.id.action_selection_delete -> delete(ids)
R.id.action_selection_pin -> pin(ids)
R.id.action_selection_clear_results -> clearResults(ids)
R.id.action_selection_resolve_domain -> resolve(ids)
R.id.action_selection_clear_traffic -> clearTraffic(ids)
R.id.action_selection_move -> moveToGroup(ids)
R.id.action_selection_copy_links -> copyLinks(ids, throne = false)
R.id.action_selection_copy_throne -> copyLinks(ids, throne = true)
Expand Down Expand Up @@ -199,6 +215,35 @@ class SelectionMode(private val host: ConfigurationFragment) {
}
}

private fun resolve(ids: List<Long>) {
if (ids.isEmpty()) return
val gid = groupId
finish()
host.launchIo {
val report = SubscriptionClient.resolveProfiles(gid, ids)
if (report.isNotBlank()) host.onUi { snackbar(report).show() }
}
}

private fun clearTraffic(ids: List<Long>) {
val gid = groupId
confirm(ids, host.getString(R.string.confirm_clear_traffic), R.string.confirm_clear) {
val service = (host.activity as? MainActivity)?.connection?.service
val adapter = host.listFor(gid)?.adapter
finish()
ids.forEach { adapter?.clearTraffic(it) }
host.launchIo {
val array = ids.toLongArray()
try {
service?.resetTraffic(array)
} catch (e: Exception) {
Logs.w(e)
}
ProfileManager.resetTraffic(array)
}
}
}

/** [title] above the names of [ids] in list order. */
private fun confirm(ids: List<Long>, title: String, @StringRes action: Int, run: () -> Unit) {
host.launchIo {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/menu/profile_selection_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
android:id="@+id/action_selection_all"
android:title="@string/profiles_select_all"
app:showAsAction="never" />
<item
android:id="@+id/action_selection_invert"
android:title="@string/profiles_invert_selection"
app:showAsAction="never" />
<item
android:id="@+id/action_selection_pin"
android:title="@string/profiles_pin"
Expand All @@ -41,6 +45,14 @@
android:id="@+id/action_selection_clear_results"
android:title="@string/connection_test_clear_results"
app:showAsAction="never" />
<item
android:id="@+id/action_selection_resolve_domain"
android:title="@string/profiles_resolve_domains"
app:showAsAction="never" />
<item
android:id="@+id/action_selection_clear_traffic"
android:title="@string/clear_traffic_statistics"
app:showAsAction="never" />
<item
android:id="@+id/action_selection_move"
android:title="@string/profiles_move_to_group"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@
<string name="profiles_resolve_domain">Разрешить домен</string>
<string name="profiles_running_kept">Работающий профиль сохранён.</string>
<string name="profiles_select_all">Выбрать все</string>
<string name="profiles_invert_selection">Инвертировать выбор</string>
<string name="profiles_show">Показать</string>
<string name="profiles_show_out_ip">Внешний IP</string>
<string name="profiles_show_speed">Скорость</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,7 @@
<item quantity="other">%d selected</item>
</plurals>
<string name="profiles_select_all">Select all</string>
<string name="profiles_invert_selection">Invert selection</string>
<string name="profiles_move_to_group">Move to group</string>
<string name="profiles_move_no_target">There is no other basic group to move to.</string>
<plurals name="profiles_moved">
Expand Down
Loading