Skip to content

Commit 954b5d1

Browse files
authored
Merge pull request #2 from FutureMind/feature/switch-to-coroutines
Feature/switch to coroutines
2 parents d5506a7 + 5c9d92e commit 954b5d1

File tree

7 files changed

+87
-141
lines changed

7 files changed

+87
-141
lines changed

MaskedEditText/build.gradle

Lines changed: 15 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
plugins {
2-
id "com.jfrog.bintray" version "1.7.3"
3-
}
4-
51
apply plugin: 'com.android.library'
62
apply plugin: 'com.github.dcendents.android-maven'
73

8-
String projectVersion = "1.0.5"
4+
String projectVersion = "1.0.16"
95
String projectGroup = "ru.egslava"
106

117
version = projectVersion
128
group = projectGroup
139

1410
android {
15-
compileSdkVersion 25
16-
buildToolsVersion '25.0.2'
11+
compileSdkVersion 30
12+
buildToolsVersion '28.0.3'
1713

1814

1915
defaultConfig {
20-
minSdkVersion 9
21-
targetSdkVersion 25
16+
minSdkVersion 21
17+
targetSdkVersion 30
2218
versionCode 1
2319
versionName projectVersion
2420
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -30,100 +26,19 @@ dependencies {
3026
exclude group: 'com.android.support', module: 'support-annotations'
3127
})
3228

33-
compile fileTree(dir: 'libs', include: ['*.jar'])
34-
compile 'com.android.support:appcompat-v7:25.2.0'
35-
36-
testCompile 'junit:junit:4.12'
37-
}
38-
39-
40-
// bintray deploy ....
41-
String projectName = "edittext-mask"
42-
String projectDescription = GFileUtils.readFile(new File("README.md"))
43-
String webUrl = "https://github.com/egslava/edittext-mask"
44-
String gitUrl = "https://github.com/egslava/edittext-mask.git"
45-
46-
47-
install {
48-
repositories.mavenInstaller {
49-
pom {
50-
project {
51-
packaging 'aar'
52-
53-
name projectName
54-
description projectDescription
55-
url webUrl
56-
57-
inceptionYear '2017' // HARDCODED
58-
59-
licenses {
60-
license {
61-
name 'MIT'
62-
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
63-
distribution 'repo'
64-
}
65-
}
29+
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation 'androidx.appcompat:appcompat:1.1.0'
6631

67-
scm {
68-
connection gitUrl
69-
developerConnection gitUrl
70-
url webUrl
71-
}
72-
developers {
73-
developer {
74-
id 'egslava'
75-
name 'Slava Egorenkov'
76-
email 'egslava@gmail.com'
77-
}
78-
}
79-
}
80-
}
81-
}
82-
}
83-
84-
// The end of the gradle file is for JCenter publication.
85-
// The original code was written with look at Alexander Matveychuk's code
86-
// and
87-
// https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/
88-
89-
task sourcesJar(type: Jar) {
90-
from android.sourceSets.main.java.srcDirs
91-
classifier = 'sources'
92-
}
32+
// Rx
33+
implementation 'io.reactivex.rxjava3:rxjava:3.0.4'
34+
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
9335

94-
task javadoc(type: Javadoc) {
95-
source = android.sourceSets.main.java.srcDirs
96-
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
97-
}
36+
// Coroutines
37+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx3:1.4.3'
9838

99-
task javadocJar(type: Jar, dependsOn: javadoc) {
100-
classifier = 'javadoc'
101-
from javadoc.destinationDir
102-
}
103-
artifacts {
104-
archives javadocJar
105-
archives sourcesJar
39+
testImplementation 'junit:junit:4.12'
10640
}
10741

108-
bintray {
109-
user = bintrayUser
110-
key = bintrayApikey
111-
112-
configurations = ['archives']
113-
pkg {
114-
repo = "maven"
115-
name = projectName
116-
userOrg = user
117-
// projectUrl = webUrl
118-
vcsUrl = gitUrl
119-
licenses = ["MIT"]
120-
group = projectGroup
121-
publish = true
122-
123-
version {
124-
name = projectVersion
125-
// desc = projectDescription
126-
vcsTag = projectVersion
127-
}
128-
}
42+
repositories {
43+
mavenCentral()
12944
}

MaskedEditText/src/main/java/br/com/sapereaude/maskedEditText/MaskedEditText.java

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import android.content.res.TypedArray;
55
import android.os.Bundle;
66
import android.os.Parcelable;
7-
import android.support.v4.text.TextUtilsCompat;
8-
import android.support.v7.widget.AppCompatEditText;
97
import android.text.Editable;
108
import android.text.SpannableStringBuilder;
119
import android.text.TextWatcher;
@@ -16,6 +14,12 @@
1614
import android.view.View;
1715
import android.widget.TextView;
1816

17+
import androidx.appcompat.widget.AppCompatEditText;
18+
19+
import io.reactivex.rxjava3.processors.BehaviorProcessor;
20+
import kotlinx.coroutines.flow.Flow;
21+
import kotlinx.coroutines.reactive.ReactiveFlowKt;
22+
1923
import static android.content.ContentValues.TAG;
2024

2125
public class MaskedEditText extends AppCompatEditText implements TextWatcher {
@@ -49,10 +53,11 @@ public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
4953
private int lastValidMaskPosition;
5054
private boolean selectionChanged;
5155
private OnFocusChangeListener focusChangeListener;
52-
private String allowedChars;
53-
private String deniedChars;
54-
private boolean shouldKeepText;
55-
56+
private String allowedChars;
57+
private String deniedChars;
58+
private BehaviorProcessor<String> rawTextState = BehaviorProcessor.create();
59+
private boolean blockFurtherSelectionChanges = false;
60+
5661
public MaskedEditText(Context context) {
5762
super(context);
5863
init();
@@ -102,6 +107,11 @@ public Parcelable onSaveInstanceState() {
102107

103108
@Override
104109
public void onRestoreInstanceState(Parcelable state) {
110+
if (!(state instanceof Bundle)) {
111+
super.onRestoreInstanceState(state);
112+
return;
113+
}
114+
105115
Bundle bundle = (Bundle) state;
106116
keepHint = bundle.getBoolean("keepHint", false);
107117
super.onRestoreInstanceState(((Bundle) state).getParcelable("super"));
@@ -299,7 +309,15 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
299309
if(count > 0) {
300310
int startingPosition = maskToRaw[nextValidPosition(start)];
301311
String addedString = s.subSequence(start, start + count).toString();
302-
count = rawText.addToString(clear(addedString), startingPosition, maxRawLength);
312+
try {
313+
count = rawText.addToString(clear(addedString), startingPosition, maxRawLength);
314+
} catch (IllegalArgumentException e) {
315+
// when exception is caught, reset view
316+
cleanUp();
317+
setText("");
318+
return;
319+
}
320+
303321
if(initialized) {
304322
int currentPosition;
305323
if(startingPosition + count < rawToMask.length)
@@ -329,6 +347,7 @@ public void afterTextChanged(Editable s) {
329347
editingOnChanged = false;
330348
editingAfter = false;
331349
ignore = false;
350+
notifyRawTextChanged(rawText.getText());
332351
}
333352
}
334353

@@ -346,7 +365,7 @@ protected void onSelectionChanged(int selStart, int selEnd) {
346365
// On Android 4+ this method is being called more than 1 time if there is a hint in the EditText, what moves the cursor to left
347366
// Using the boolean var selectionChanged to limit to one execution
348367

349-
if(initialized ){
368+
if(initialized){
350369
if(!selectionChanged) {
351370
selStart = fixSelection(selStart);
352371
selEnd = fixSelection(selEnd);
@@ -361,8 +380,11 @@ protected void onSelectionChanged(int selStart, int selEnd) {
361380

362381
setSelection(selStart, selEnd);
363382
selectionChanged = true;
364-
} else{
365-
//check to see if the current selection is outside the already entered text
383+
blockFurtherSelectionChanges = true;
384+
} else if (blockFurtherSelectionChanges) {
385+
blockFurtherSelectionChanges = false;
386+
} else {
387+
//check to see if the current selection is outside the already entered text
366388
if(selStart > rawText.length() - 1){
367389
final int start = fixSelection(selStart);
368390
final int end = fixSelection(selEnd);
@@ -465,10 +487,7 @@ private Range calculateRange(int start, int end) {
465487
range.setEnd(rawText.length());
466488
}
467489
if(range.getStart() == range.getEnd() && start < end) {
468-
int newStart = previousValidPosition(range.getStart() - 1);
469-
if(newStart < range.getStart()) {
470-
range.setStart(newStart);
471-
}
490+
range.setEnd(range.getEnd() + 1);
472491
}
473492
return range;
474493
}
@@ -494,4 +513,12 @@ private String clear(String string) {
494513

495514
return string;
496515
}
516+
517+
private void notifyRawTextChanged(String text) {
518+
rawTextState.onNext(text == null ? "" : text);
519+
}
520+
521+
public Flow<String> observeRawTextChanges() {
522+
return ReactiveFlowKt.asFlow(rawTextState);
523+
}
497524
}

app/build.gradle

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion '25.0.2'
4+
compileSdkVersion 30
5+
buildToolsVersion '28.0.3'
66

77
defaultConfig {
88
applicationId "ru.egslava.edittextphonenumber"
9-
minSdkVersion 9
10-
targetSdkVersion 25
9+
minSdkVersion 21
10+
targetSdkVersion 30
1111
versionCode 1
1212
versionName "1.0.0"
1313
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
@@ -18,13 +18,17 @@ android {
1818
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1919
}
2020
}
21+
compileOptions {
22+
sourceCompatibility = 1.8
23+
targetCompatibility = 1.8
24+
}
2125
}
2226

2327
dependencies {
24-
compile fileTree(dir: 'libs', include: ['*.jar'])
25-
compile 'com.android.support:appcompat-v7:25.2.0'
26-
compile project(':MaskedEditText')
27-
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
28+
implementation fileTree(dir: 'libs', include: ['*.jar'])
29+
implementation 'androidx.appcompat:appcompat:1.1.0'
30+
implementation project(':MaskedEditText')
31+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2', {
2832
exclude group: 'com.android.support', module: 'support-annotations'
2933
}
3034
}

app/src/main/java/ru/egslava/edittextphonenumber/MainActivity.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
package ru.egslava.edittextphonenumber;
22

3-
import android.support.v7.app.ActionBarActivity;
43
import android.os.Bundle;
5-
import android.text.InputFilter;
6-
import android.text.Spanned;
74
import android.view.Menu;
85
import android.view.MenuItem;
9-
import android.view.View;
10-
import android.widget.EditText;
116

7+
import androidx.appcompat.app.AppCompatActivity;
128

139
import br.com.sapereaude.maskedEditText.MaskedEditText;
1410

15-
public class MainActivity extends ActionBarActivity {
11+
public class MainActivity extends AppCompatActivity {
1612

1713
@Override
1814
protected void onCreate(Bundle savedInstanceState) {
1915
super.onCreate(savedInstanceState);
2016
setContentView(R.layout.activity_main);
21-
MaskedEditText phone = (MaskedEditText)findViewById(R.id.phone_input);
17+
MaskedEditText phone = findViewById(R.id.phone_input);
2218
}
2319

2420
@Override

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
android:inputType="phone"
2525
android:typeface="monospace"
2626
mask:allowed_chars="1234567890"
27-
android:hint="9081234567"
27+
android:hint="123456789"
2828
android:textColorHint="@android:color/darker_gray"
29-
mask:mask="+7(###)###-##-##"
30-
mask:keep_hint="false" />
29+
mask:mask="(+48)###-###-###"
30+
mask:keep_hint="true" />
3131

3232
</LinearLayout>

build.gradle

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
45
repositories {
6+
google()
57
jcenter()
68
}
79
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.3'
9-
// classpath 'com.github.dcendents:android-maven-plugin:1.2'
10-
// https://mvnrepository.com/artifact/com.github.dcendents/android-maven-gradle-plugin
11-
classpath group: 'com.github.dcendents', name: 'android-maven-gradle-plugin', version: '1.5'
12-
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
13-
// NOTE: Do not place your application dependencies here; they belong
14-
// in the individual module build.gradle files
10+
classpath 'com.android.tools.build:gradle:3.5.3'
11+
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1512
}
1613
}
14+
1715
allprojects {
1816
repositories {
17+
google()
1918
jcenter()
19+
maven { url 'https://jitpack.io' }
2020
}
21+
}
22+
23+
task clean(type: Delete) {
24+
delete rootProject.buildDir
2125
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Apr 10 15:27:10 PDT 2013
1+
#Wed Jan 08 10:22:06 CET 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

0 commit comments

Comments
 (0)