Commit 2a5ec926 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Show a snackbar if a rule already exists

parent 16cf83d3
Pipeline #4876 passed with stage
in 9 minutes and 8 seconds
......@@ -27,6 +27,7 @@ import com.frostnerd.smokescreen.dialog.NewHostSourceDialog
import com.frostnerd.smokescreen.service.RuleExportService
import com.frostnerd.smokescreen.service.RuleImportService
import com.frostnerd.smokescreen.util.SpaceItemDecorator
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_dns_rules.*
import kotlinx.android.synthetic.main.activity_dns_rules.toolBar
import kotlinx.android.synthetic.main.item_datasource.view.*
......@@ -204,19 +205,23 @@ class DnsRuleActivity : BaseActivity() {
else -> it
}
}
getDatabase().dnsRuleRepository().insertAsync(newRule)
userDnsRules.add(insertPos, newRule)
val wereRulesShown = showUserRules
showUserRules = true
if(wereRulesShown) {
userRuleCount += 1
sourceAdapter.notifyItemInserted(sourceAdapterList.size + 1 + insertPos)
} else {
userRuleCount = userDnsRules.size
runOnUiThread {
sourceAdapter.notifyItemChanged(sourceAdapterList.size)
sourceAdapter.notifyItemRangeInserted(sourceAdapterList.size + 1, userRuleCount)
val id = getDatabase().dnsRuleDao().insertIgnore(newRule)
if(id != -1L) {
userDnsRules.add(insertPos, newRule)
val wereRulesShown = showUserRules
showUserRules = true
if(wereRulesShown) {
userRuleCount += 1
sourceAdapter.notifyItemInserted(sourceAdapterList.size + 1 + insertPos)
} else {
userRuleCount = userDnsRules.size
runOnUiThread {
sourceAdapter.notifyItemChanged(sourceAdapterList.size)
sourceAdapter.notifyItemRangeInserted(sourceAdapterList.size + 1, userRuleCount)
}
}
} else {
Snackbar.make(findViewById(android.R.id.content), R.string.window_dnsrules_hostalreadyexists, Snackbar.LENGTH_LONG).show()
}
}).show()
})
......
......@@ -33,6 +33,9 @@ interface DnsRuleDao {
@Insert
fun insert(dnsRule: DnsRule)
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertIgnore(dnsRule: DnsRule):Long
@Query("DELETE FROM DnsRule WHERE importedFrom IS NULL")
fun deleteAllUserRules()
......
......@@ -70,6 +70,7 @@
<string name="button_main_dnsrules">DNS rules</string>
<string name="window_dnsrules_customhosts">Custom hosts</string>
<string name="window_dnsrules_hostalreadyexists">There already is a rule for this host.</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>
......
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