Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Filter emojis in RAT profile fields (EXPOSUREAPP-8108) (#3597)
Browse files Browse the repository at this point in the history
* Create a custom view

* Use extension function instead

* Some Tests

Co-authored-by: harambasicluka <64483219+harambasicluka@users.noreply.github.com>
Co-authored-by: Juraj Kusnier <jurajkusnier@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 2, 2021
1 parent 31ed88f commit 61b8e18
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.rki.coronawarnapp.ui.view

import android.text.SpannableString
import io.kotest.matchers.shouldBe
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class EmojiFilterTest {

private val testData = mapOf<CharSequence, CharSequence?>(
"Pneumonoultramicroscopicsilicovolcanoconiosis" to null, // English
"Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft" to null, // German
"Muvaffakiyetsizleştiricileştiriveremeyebileceklerimizdenmişsinizcesine" to null, // Turkish
"Рентгеноэлектрокардиографический" to null, // Russian
"speciallægepraksisplanlægningsstabiliseringsperiode" to null, // Danish
"Непротивоконституционствувателствувайте" to null, // Bulgarian
"Dziewięćsetdziewięćdziesięciodziewięcionarodowościowego" to null, // Polish
"Pneumoultramicroscopicossilicovulcanoconiótico" to null, // Portuguese
"paraskevidékatriaphobie" to null, // French
"peruspalveluliikelaitoskuntayhtymä" to null, // Finnish
"Arquitectónicamente" to null, // Spanish
"znajneprekryštalizovávateľnejšievajúcimi" to null, // Slovak
// Swedish
"Spårvagnsaktiebolagsskensmutsskjutarefackföreningspersonalbeklädnadsmagasinsförråd-sförvaltarens" to null,
// Hungarian
"legösszetettebbszóhosszúságvilágrekorddöntéskényszerneurózistünetegyüttesmegnyilvá-nulásfejleszthetőségvizsgálataitokként" to null,
"nebeprisikiškiakopūstlapiaujančiuosiuose" to null, // Lithuanian
"וכשבהשתעשעויותיהם" to null, // Hebrew
"ηλεκτροεγκεφαλογραφήματος" to null, // Greek
"prijestolonasljednikovičičinima" to null, // Croatian
"Sünnipäevanädalalõpupeopärastlõunaväsimatus" to null, // Estonian
"أفاستسقيناكموها" to null, // Arabic,
"✌️✌️✌️✌️✌️✌️✌️✌️✌️️" to "", // Emoji,
"☕☕☕☕☕☕☕☕☕" to "", // Emoji,
)

@Test
fun filter() {
testData.forEach { (input, output) ->
EmojiFilter().filter(
input,
0,
input.length,
SpannableString(input),
0,
input.length
) shouldBe output
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.google.android.material.datepicker.MaterialDatePicker
import de.rki.coronawarnapp.R
import de.rki.coronawarnapp.contactdiary.util.hideKeyboard
import de.rki.coronawarnapp.databinding.RatProfileCreateFragmentBinding
import de.rki.coronawarnapp.ui.view.addEmojiFilter
import de.rki.coronawarnapp.util.TimeAndDateExtensions.toDayFormat
import de.rki.coronawarnapp.util.di.AutoInject
import de.rki.coronawarnapp.util.ui.doNavigate
Expand Down Expand Up @@ -36,8 +37,8 @@ class RATProfileCreateFragment : Fragment(R.layout.rat_profile_create_fragment),
}

// Full name
firstNameInputEdit.doAfterTextChanged { viewModel.firstNameChanged(it.toString()) }
lastNameInputEdit.doAfterTextChanged { viewModel.lastNameChanged(it.toString()) }
firstNameInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.firstNameChanged(it.toString()) }
lastNameInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.lastNameChanged(it.toString()) }

// Birth date
birthDateInputEdit.setOnClickListener { openDatePicker() }
Expand All @@ -48,15 +49,15 @@ class RATProfileCreateFragment : Fragment(R.layout.rat_profile_create_fragment),
}

// Address
streetInputEdit.doAfterTextChanged { viewModel.streetChanged(it.toString()) }
streetInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.streetChanged(it.toString()) }
cityInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.cityChanged(it.toString()) }
zipCodeInputEdit.doAfterTextChanged { viewModel.zipCodeChanged(it.toString()) }
cityInputEdit.doAfterTextChanged { viewModel.cityChanged(it.toString()) }

// Phone
phoneInputEdit.doAfterTextChanged { viewModel.phoneChanged(it.toString()) }

// E-mail
emailInputEdit.doAfterTextChanged { viewModel.emailChanged(it.toString()) }
emailInputEdit.addEmojiFilter().doAfterTextChanged { viewModel.emailChanged(it.toString()) }

viewModel.profile.observe(viewLifecycleOwner) {
profileSaveButton.isEnabled = it?.isValid == true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.rki.coronawarnapp.ui.view

import android.text.InputFilter
import android.text.Spanned
import com.google.android.material.textfield.TextInputEditText
import java.lang.Character.OTHER_SYMBOL
import java.lang.Character.SURROGATE

/**
* Adds [InputFilter] of emojis ,returns same [TextInputEditText]
*/
fun TextInputEditText.addEmojiFilter(): TextInputEditText {
filters += EmojiFilter()
return this
}

class EmojiFilter : InputFilter {
override fun filter(
source: CharSequence,
sStart: Int,
sEnd: Int,
destination: Spanned,
dStart: Int,
dEnd: Int
): CharSequence? {
for (index in sStart until sEnd) {
val type = Character.getType(source[index]).toByte()
if (type in listOf(SURROGATE, OTHER_SYMBOL)) return ""
}
return null
}
}

0 comments on commit 61b8e18

Please sign in to comment.