Skip to content

Commit

Permalink
Merge pull request #1 from SteliosPapamichail/dev-1.1.0
Browse files Browse the repository at this point in the history
New digit masking feature & dependency updates
  • Loading branch information
SteliosPapamichail committed Jun 5, 2023
2 parents 72a50bf + 9684d1f commit d773c83
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 59 deletions.
21 changes: 12 additions & 9 deletions CreditCardHelper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
minSdk 23
targetSdk 32
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -28,17 +28,20 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
composeOptions {
kotlinCompilerExtensionVersion = compose_version
}
namespace 'com.steliospapamichail.creditcardmasker'
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation "androidx.compose.ui:ui:$compose_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

afterEvaluate {
Expand All @@ -49,7 +52,7 @@ afterEvaluate {

groupId = 'com.steliospapamichail.creditcardmasker'
artifactId = 'credit-card-helper'
version = '1.0.0'
version = '1.1.0'
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions CreditCardHelper/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.steliospapamichail.creditcardmasker">
<manifest>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,57 @@ import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation

class CardNumberMask(private val separator: String = " ") : VisualTransformation {
private const val MAX_CREDIT_CARD_NUMBER_LENGTH = 16

/**
* @param separator The separator to use when breaking up a card number in groups of four
* @param displayOnlyLastFourDigits When set to true, the first twelve (12) numbers will be masked using the
* digitMask param.
* @param digitMask The string to use as a mask when displayOnlyLastFourDigits is set to true.
* @author Stelios Papamichail
*/
class CardNumberMask(
private val separator: String = " ",
private val displayOnlyLastFourDigits: Boolean = false,
private val digitMask: String = "X",
) : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText {
return makeCardNumberFilter(text, separator)
return makeCardNumberFilter(text, separator, displayOnlyLastFourDigits)
}

private fun makeCardNumberFilter(text: AnnotatedString, separator: String): TransformedText {
private fun makeCardNumberFilter(
text: AnnotatedString,
separator: String,
displayOnlyLastFourDigits: Boolean
): TransformedText {
// format: XXXX XXXX XXXX XXXX by default
val trimmed = if (text.text.length >= 16) text.text.substring(0..15) else text.text
val trimmed =
if (text.text.length >= MAX_CREDIT_CARD_NUMBER_LENGTH) text.text.substring(0..15) else text.text
var out = ""
for (i in trimmed.indices) {
out += trimmed[i]
out += if (displayOnlyLastFourDigits && i !in 12..16) digitMask else trimmed[i]
if (i == 3 || i == 7 || i == 11) out += separator
}

val offsetMapping = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
return if (offset <= 3) offset
else if (offset <= 7) offset + 1
else if (offset <= 11) offset + 2
else if (offset <= 16) offset + 3
else 19
return when {
offset <= 3 -> offset
offset <= 7 -> offset + 1
offset <= 11 -> offset + 2
offset <= 16 -> offset + 3
else -> 19
}
}

override fun transformedToOriginal(offset: Int): Int {
return if (offset <= 4) offset
else if (offset <= 9) offset - 1
else if (offset <= 14) offset - 2
else if (offset <= 19) offset - 3
else 16
return when {
offset <= 4 -> offset
offset <= 9 -> offset - 1
offset <= 14 -> offset - 2
offset <= 19 -> offset - 3
else -> 16
}
}
}

Expand Down
31 changes: 17 additions & 14 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
applicationId "com.steliospapamichail.creditcardmask"
minSdk 23
targetSdk 32
targetSdk 33
versionCode 1
versionName "1.0"

Expand All @@ -37,13 +37,14 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerExtensionVersion = compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
namespace 'com.steliospapamichail.creditcardmask'
}

sonarqube {
Expand All @@ -56,16 +57,18 @@ sonarqube {

dependencies {
implementation project(':CreditCardHelper')
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.4.0'
// Import the Compose BOM
implementation platform('androidx.compose:compose-bom:2023.05.01')
implementation 'androidx.core:core-ktx:1.10.1'
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.material:material"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4"
debugImplementation "androidx.compose.ui:ui-tooling"
debugImplementation "androidx.compose.ui:ui-test-manifest"
}
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.steliospapamichail.creditcardmask">
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
Expand All @@ -12,11 +11,11 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CreditCardMask"
tools:targetApi="31">
tools:targetApi="31"
android:usesCleartextTraffic="false">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.CreditCardMask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.steliospapamichail.creditcardmask

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
Expand All @@ -31,17 +41,17 @@ class MainActivity : ComponentActivity() {
.padding(20.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Expiration()
expiration()
Spacer(modifier = Modifier.height(10.dp))
CardNumber()
cardNumber()
}
}
}
}
}

@Composable
fun Expiration() {
fun expiration() {
var expiration by remember { mutableStateOf("") }
OutlinedTextField(
value = expiration,
Expand All @@ -53,11 +63,11 @@ fun Expiration() {
}

@Composable
fun CardNumber() {
fun cardNumber() {
var number by remember { mutableStateOf("") }
OutlinedTextField(
value = number,
visualTransformation = CardNumberMask("-"),
visualTransformation = CardNumberMask("-", true),
trailingIcon = {
val iconRes = when (getCardTypeFromNumber(number)) {
CardType.VISA -> R.drawable.visa
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
buildscript {
ext {
compose_version = '1.2.0-alpha04'
compose_version = '1.4.3'
}
repositories {
google()
maven { url 'https://jitpack.io' }
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
}

task clean(type: Delete) {
tasks.register('clean', Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue May 10 22:47:42 EEST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit d773c83

Please sign in to comment.