Skip to content

Commit

Permalink
Easily allow ignoring users from the context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Feb 22, 2019
1 parent 27cf008 commit 1c6ec32
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ class IgnoreItemActivity : ServiceBoundSettingsActivity(IgnoreItemFragment()) {
companion object {
fun launch(
context: Context,
item: IgnoreListManager.IgnoreListItem? = null
item: IgnoreListManager.IgnoreListItem? = null,
addRule: String? = null
) = context.startActivity(intent(context, item))

fun intent(
context: Context,
item: IgnoreListManager.IgnoreListItem? = null
item: IgnoreListManager.IgnoreListItem? = null,
addRule: String? = null
) = Intent(context, IgnoreItemActivity::class.java).apply {
if (item != null) {
putExtra("item", item)
}
if (addRule != null) {
putExtra("add_rule", addRule)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ class IgnoreItemFragment : ServiceBoundSettingsFragment(), Savable,
))
scope.adapter = scopeAdapter

item?.let { data ->
val addRule = arguments?.getString("add_rule")
val data = item
if (data != null) {
enabled.isChecked = data.isActive
ignoreRule.setText(data.ignoreRule)
isRegEx.isChecked = data.isRegEx
type.setSelection(typeAdapter.indexOf(data.type) ?: 0)
strictness.setSelection(strictnessAdapter.indexOf(data.strictness) ?: 0)
scope.setSelection(scopeAdapter.indexOf(data.scope) ?: 0)
scopeRule.setText(data.scopeRule)
} else if (addRule != null) {
ignoreRule.setText(addRule)
}

scope.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ import de.kuschku.quasseldroid.util.ui.settings.ServiceBoundSettingsActivity

class IgnoreListActivity : ServiceBoundSettingsActivity(IgnoreListFragment()) {
companion object {
fun launch(context: Context) = context.startActivity(intent(context))
fun intent(context: Context) = Intent(context, IgnoreListActivity::class.java)
fun launch(
context: Context,
addRule: String? = null
) = context.startActivity(intent(context, addRule))

fun intent(
context: Context,
addRule: String? = null
) = Intent(context, IgnoreListActivity::class.java).apply {
if (addRule != null) {
putExtra("add_rule", addRule)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ class IgnoreListFragment : ServiceBoundSettingsFragment(), Savable,
helper = ItemTouchHelper(callback)
helper.attachToRecyclerView(list)

val addRule = arguments?.getString("add_rule")

add.setOnClickListener {
startActivityForResult(IgnoreItemActivity.intent(requireContext()), REQUEST_CREATE_RULE)
}

if (addRule != null) {
startActivityForResult(IgnoreItemActivity.intent(requireContext(), addRule = addRule),
REQUEST_CREATE_RULE)
}

viewModel.ignoreListManager
.filter(Optional<IgnoreListManager>::isPresent)
.map(Optional<IgnoreListManager>::get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import butterknife.BindView
import butterknife.ButterKnife
Expand All @@ -42,9 +41,11 @@ import de.kuschku.libquassel.quassel.syncables.IrcUser
import de.kuschku.libquassel.util.Optional
import de.kuschku.libquassel.util.helpers.nullIf
import de.kuschku.libquassel.util.helpers.value
import de.kuschku.libquassel.util.irc.HostmaskHelper
import de.kuschku.quasseldroid.R
import de.kuschku.quasseldroid.settings.MessageSettings
import de.kuschku.quasseldroid.ui.chat.ChatActivity
import de.kuschku.quasseldroid.ui.coresettings.ignorelist.IgnoreListActivity
import de.kuschku.quasseldroid.util.ShortcutCreationHelper
import de.kuschku.quasseldroid.util.avatars.AvatarHelper
import de.kuschku.quasseldroid.util.avatars.MatrixApi
Expand Down Expand Up @@ -225,7 +226,7 @@ class UserInfoFragment : ServiceBoundFragment() {
}

nick.text = user.nick
val (content, hasSpoilers) = contentFormatter.formatContent(
val (content, _) = contentFormatter.formatContent(
user.realName ?: "",
networkId = user.networkId
)
Expand Down Expand Up @@ -253,7 +254,7 @@ class UserInfoFragment : ServiceBoundFragment() {

actionWhois.visibleIf(user.knownToCore)

actionQuery.setOnClickListener {
actionQuery.setOnClickListener { view ->
viewModel.session.value?.orNull()?.let { session ->
val info = session.bufferSyncer.find(
bufferName = user.nick,
Expand All @@ -262,8 +263,7 @@ class UserInfoFragment : ServiceBoundFragment() {
)

if (info != null) {
ChatActivity.launch(requireContext(),
bufferId = info.bufferId)
ChatActivity.launch(view.context, bufferId = info.bufferId)
} else {
viewModel.allBuffers.map {
listOfNotNull(it.find {
Expand All @@ -273,31 +273,30 @@ class UserInfoFragment : ServiceBoundFragment() {
it.isNotEmpty()
}.firstElement().toLiveData().observe(this, Observer {
it?.firstOrNull()?.let { info ->
ChatActivity.launch(requireContext(),
bufferId = info.bufferId)
ChatActivity.launch(view.context, bufferId = info.bufferId)
}
})

session.bufferSyncer.find(
networkId = user.networkId,
type = Buffer_Type.of(Buffer_Type.StatusBuffer)
)?.let { statusInfo ->
session.rpcHandler.sendInput(statusInfo,
"/query ${user.nick}")
session.rpcHandler.sendInput(statusInfo, "/query ${user.nick}")
}
}
}
}

actionIgnore.setOnClickListener {
Toast.makeText(requireContext(), "Not Implemented", Toast.LENGTH_SHORT).show()
actionIgnore.setOnClickListener { view ->
IgnoreListActivity.launch(view.context,
addRule = HostmaskHelper.build(user.nick, user.user, user.host))
}

actionMention.setOnClickListener {
ChatActivity.launch(requireContext(), sharedText = "${user.nick}: ")
actionMention.setOnClickListener { view ->
ChatActivity.launch(view.context, sharedText = "${user.nick}: ")
}

actionWhois.setOnClickListener {
actionWhois.setOnClickListener { view ->
viewModel.session {
it.orNull()?.let { session ->
session.bufferSyncer.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object AvatarHelper {
if (size != null) {
return listOf(
Avatar.IRCCloudAvatar(
"https://static.irccloud-cdn.com/avatar-redirect/s${truncateSize(size)}/$userId"
"https://static.irccloud-cdn.com/avatar-redirect/s$size/$userId"
)
)
}
Expand All @@ -99,20 +99,18 @@ object AvatarHelper {
if (size == null) {
"https://www.gravatar.com/avatar/$hash?d=404"
} else {
"https://www.gravatar.com/avatar/$hash?d=404&s=${truncateSize(size)}"
"https://www.gravatar.com/avatar/$hash?d=404&s=${size}"
}
}.map { Avatar.GravatarAvatar(it) }.toList()
}

private fun matrixFallback(realname: String, size: Int?): List<Avatar> {
return if (Patterns.MATRIX_REALNAME.matches(realname)) {
listOf(
Avatar.MatrixAvatar(realname, size?.let(this::truncateSize))
Avatar.MatrixAvatar(realname, size)
)
} else {
emptyList()
}
}

private fun truncateSize(originalSize: Int) = originalSize //if (originalSize > 72) 512 else 72
}
4 changes: 1 addition & 3 deletions app/src/main/res/layout/info_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@
style="@style/Widget.Info.ActionButton"
android:contentDescription="@string/label_ignore_long"
android:text="@string/label_ignore"
android:visibility="gone"
tools:drawableTint="?colorTextSecondary"
tools:drawableTop="@drawable/ic_eye_off"
tools:visibility="visible" />
tools:drawableTop="@drawable/ic_eye_off" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/action_whois"
Expand Down
10 changes: 10 additions & 0 deletions lib/src/main/java/de/kuschku/libquassel/util/irc/HostmaskHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ object HostmaskHelper {

return Triple(nick, user, host)
}

fun build(nick: String, user: String?, host: String?) = buildString {
append(nick)
if (!user.isNullOrEmpty()) {
append("!$user")
}
if (!host.isNullOrEmpty()) {
append("@$host")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class HostmaskHelperTest {
HostmaskHelper.host("[email protected]"))
assertEquals("lithium.kuschku.de",
HostmaskHelper.split("[email protected]").third)

assertEquals("[email protected]", HostmaskHelper.build(
"justJanne", "kuschku", "lithium.kuschku.de"
))
}

@Test
Expand All @@ -57,6 +61,10 @@ class HostmaskHelperTest {
HostmaskHelper.host("[email protected]"))
assertEquals("lithium.kuschku.de",
HostmaskHelper.split("[email protected]").third)

assertEquals("[email protected]", HostmaskHelper.build(
"justJanne", "~kuschku", "lithium.kuschku.de"
))
}

@Test
Expand All @@ -75,6 +83,10 @@ class HostmaskHelperTest {
HostmaskHelper.host("bärlauch!maße@flüge.de"))
assertEquals("flüge.de",
HostmaskHelper.split("bärlauch!maße@flüge.de").third)

assertEquals("bärlauch!maße@flüge.de", HostmaskHelper.build(
"bärlauch", "maße", "flüge.de"
))
}

@Test
Expand All @@ -93,6 +105,10 @@ class HostmaskHelperTest {
HostmaskHelper.host("irc.freenode.org"))
assertEquals("",
HostmaskHelper.split("irc.freenode.org").third)

assertEquals("irc.freenode.org", HostmaskHelper.build(
"irc.freenode.org", "", ""
))
}

@Test
Expand All @@ -111,6 +127,10 @@ class HostmaskHelperTest {
HostmaskHelper.host("@[email protected]"))
assertEquals("example.org",
HostmaskHelper.split("@[email protected]").third)

assertEquals("@[email protected]", HostmaskHelper.build(
"@nick", "~ident", "example.org"
))
}

@Test
Expand All @@ -129,6 +149,10 @@ class HostmaskHelperTest {
HostmaskHelper.host("a@a!"))
assertEquals("a!",
HostmaskHelper.split("a@a!").third)

assertEquals("a@a!", HostmaskHelper.build(
"a", "", "a!"
))
}

@Test
Expand All @@ -147,5 +171,9 @@ class HostmaskHelperTest {
HostmaskHelper.host("Gin_!Gin_!♡♅ƸӜƷ♅♡!@discord"))
assertEquals("discord",
HostmaskHelper.split("Gin_!Gin_!♡♅ƸӜƷ♅♡!@discord").third)

assertEquals("Gin_!Gin_!♡♅ƸӜƷ♅♡!@discord", HostmaskHelper.build(
"Gin_", "Gin_!♡♅ƸӜƷ♅♡!", "discord"
))
}
}

0 comments on commit 1c6ec32

Please sign in to comment.