Commit b8d18901 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

The rules list now shows how many rules were imported from a given source

Implements #91
parent 513d019f
......@@ -15,6 +15,7 @@ import com.frostnerd.general.service.isServiceRunning
import com.frostnerd.lifecyclemanagement.BaseActivity
import com.frostnerd.lifecyclemanagement.BaseViewHolder
import com.frostnerd.lifecyclemanagement.LifecycleCoroutineScope
import com.frostnerd.lifecyclemanagement.launchWithLifecylce
import com.frostnerd.smokescreen.*
import com.frostnerd.smokescreen.database.entities.DnsRule
import com.frostnerd.smokescreen.database.entities.HostSource
......@@ -32,6 +33,7 @@ import kotlinx.android.synthetic.main.item_datasource.view.enable
import kotlinx.android.synthetic.main.item_datasource.view.text
import kotlinx.android.synthetic.main.item_datasource_rules.view.*
import kotlinx.android.synthetic.main.item_dnsrule_host.view.*
import kotlin.system.measureTimeMillis
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -55,8 +57,9 @@ class DnsRuleActivity : BaseActivity() {
private lateinit var sourceAdapter: RecyclerView.Adapter<*>
private lateinit var sourceAdapterList: MutableList<HostSource>
private lateinit var adapterDataSource: ListDataSource<HostSource>
private var importDoneReceiver:BroadcastReceiver? = null
private lateinit var userDnsRules:MutableList<DnsRule>
private lateinit var sourceRuleCount:MutableMap<HostSource, Int?>
private var importDoneReceiver:BroadcastReceiver? = null
private var refreshProgressShown = false
override fun onCreate(savedInstanceState: Bundle?) {
......@@ -95,6 +98,9 @@ class DnsRuleActivity : BaseActivity() {
}
}
sourceAdapterList = getDatabase().hostSourceDao().getAll().toMutableList()
sourceRuleCount = sourceAdapterList.map {
it to (null as Int?)
}.toMap().toMutableMap()
adapterDataSource = ListDataSource(sourceAdapterList)
var showUserRules = false
var userRuleCount = 0
......@@ -203,8 +209,18 @@ class DnsRuleActivity : BaseActivity() {
getItemCount = {
sourceAdapterList.size + 1 + userRuleCount
}
bindModelView = { viewHolder, _, data ->
bindModelView = { viewHolder, position, data ->
(viewHolder as SourceViewHolder).display(data)
when {
sourceRuleCount[data] != null -> viewHolder.ruleCount.text = getString(R.string.window_dnsrules_customhosts_hostsource_rulecount, sourceRuleCount[data])
data.enabled -> launchWithLifecylce(false) {
sourceRuleCount[data] = getDatabase().dnsRuleDao().getCountForHostSource(data.id)
runOnUiThread {
sourceAdapter.notifyItemChanged(position)
}
}
else -> viewHolder.ruleCount.text = getString(R.string.window_dnsrules_customhosts_hostsource_rulecount_pending)
}
}
bindNonModelView = { viewHolder, position ->
if(viewHolder is CustomRulesViewHolder) {
......@@ -245,6 +261,12 @@ class DnsRuleActivity : BaseActivity() {
super.onResume()
importDoneReceiver = registerLocalReceiver(IntentFilter(RuleImportService.BROADCAST_IMPORT_DONE)) {
refreshProgress.hide()
sourceRuleCount.keys.forEach {
sourceRuleCount[it] = null
}
sourceAdapterList.forEachIndexed { index, hostSource ->
if(hostSource.enabled) sourceAdapter.notifyItemChanged(index)
}
refreshProgressShown = false
}
if(!isServiceRunning(RuleImportService::class.java) && refreshProgressShown) {
......@@ -283,6 +305,7 @@ class DnsRuleActivity : BaseActivity() {
val subText = view.subText
val enabled = view.enable
val delete = view.delete
val ruleCount = view.ruleCount
lateinit var source: HostSource
init {
......
......@@ -71,4 +71,7 @@ interface DnsRuleDao {
@Delete
fun remove(rule:DnsRule)
@Query("SELECT COUNT(*) FROM DnsRule WHERE importedFrom=:hostSourceId")
fun getCountForHostSource(hostSourceId:Long):Int
}
\ No newline at end of file
......@@ -12,51 +12,68 @@
android:layout_width="match_parent"
android:background="?selectableItemBackground"
android:id="@+id/cardContent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:paddingStart="12dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:orientation="vertical"
android:layout_toStartOf="@id/buttonWrap"
android:layout_alignParentStart="true"
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/contentWrap"
android:layout_height="wrap_content">
<TextView
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/text"
android:textStyle="bold"
android:textSize="18sp"
android:layout_height="wrap_content"/>
android:paddingStart="12dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:orientation="vertical"
android:layout_toStartOf="@id/buttonWrap"
android:layout_alignParentStart="true"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:id="@+id/text"
android:textStyle="bold"
android:textSize="18sp"
android:layout_height="wrap_content"/>
<TextView
<TextView
android:layout_width="wrap_content"
android:id="@+id/subText"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/subText"
android:layout_height="wrap_content"/>
</LinearLayout>
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:id="@+id/buttonWrap"
android:layout_height="wrap_content">
<Switch
android:layout_width="wrap_content"
android:id="@+id/enable"
android:layout_height="wrap_content"/>
<LinearLayout
<ImageButton
android:layout_width="48dp"
style="@style/Widget.AppCompat.Button.Borderless"
android:src="@drawable/ic_delete"
android:id="@+id/delete"
android:tint="?android:attr/textColor"
android:layout_height="48dp"/>
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:id="@+id/buttonWrap"
android:layout_height="wrap_content">
android:layout_below="@id/contentWrap"
android:layout_centerHorizontal="true"
android:id="@+id/ruleCount"
android:paddingBottom="12dp"
android:text="@string/window_dnsrules_customhosts_hostsource_rulecount_pending"
android:layout_height="wrap_content"/>
<Switch
android:layout_width="wrap_content"
android:id="@+id/enable"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_width="48dp"
style="@style/Widget.AppCompat.Button.Borderless"
android:src="@drawable/ic_delete"
android:id="@+id/delete"
android:tint="?android:attr/textColor"
android:layout_height="48dp"/>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
\ No newline at end of file
......@@ -68,4 +68,6 @@
<string name="window_dnsrules_customhosts">Custom hosts</string>
<string name="window_dnsrules_customhosts_message">Define all your custom hosts here.</string>
<string name="window_dnsrules_customhosts_hostsource_rulecount_pending">Rules from this source: -</string>
<string name="window_dnsrules_customhosts_hostsource_rulecount">Rules from this source: %1d</string>
</resources>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment