Skip to content

Commit 643157b

Browse files
author
Bob
committed
Migrated to AndroidX
added semitransparent loader ToDo: - add history controls (forward, backward, list)
1 parent 006ae40 commit 643157b

File tree

10 files changed

+382
-55
lines changed

10 files changed

+382
-55
lines changed

app/build.gradle

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

33
android {
4-
compileSdkVersion 27
4+
compileSdkVersion 31
55
defaultConfig {
66
applicationId "ru.coolsoft.iris2go"
77
minSdkVersion 19
8-
targetSdkVersion 27
8+
targetSdkVersion 31
99
versionCode 1
1010
versionName "1.0"
11-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
11+
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1212
}
1313
buildTypes {
1414
release {
@@ -20,11 +20,11 @@ android {
2020

2121
dependencies {
2222
implementation fileTree(dir: 'libs', include: ['*.jar'])
23-
implementation 'com.android.support:appcompat-v7:27.1.1'
24-
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
23+
implementation 'androidx.appcompat:appcompat:1.4.2'
24+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
2525
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
2626
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
2727
testImplementation 'junit:junit:4.12'
28-
androidTestImplementation 'com.android.support.test:runner:1.0.2'
29-
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
29+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
3030
}

app/src/androidTest/java/ru/coolsoft/iris2go/ExampleInstrumentedTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package ru.coolsoft.iris2go;
22

33
import android.content.Context;
4-
import android.support.test.InstrumentationRegistry;
5-
import android.support.test.runner.AndroidJUnit4;
4+
import androidx.test.platform.app.InstrumentationRegistry;
5+
import androidx.test.ext.junit.runners.AndroidJUnit4;
66

77
import org.junit.Test;
88
import org.junit.runner.RunWith;
@@ -19,7 +19,7 @@ public class ExampleInstrumentedTest {
1919
@Test
2020
public void useAppContext() {
2121
// Context of the app under test.
22-
Context appContext = InstrumentationRegistry.getTargetContext();
22+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
2323

2424
assertEquals("ru.coolsoft.iris2go", appContext.getPackageName());
2525
}

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
<activity
1717
android:name=".MainActivity"
18-
android:icon="@drawable/logo">
18+
android:icon="@drawable/logo"
19+
android:exported="false">
1920
<intent-filter>
2021
<action android:name="android.intent.action.MAIN" />
2122

app/src/main/java/ru/coolsoft/iris2go/MainActivity.java

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
package ru.coolsoft.iris2go;
22

33
import android.os.Bundle;
4-
import android.support.annotation.DrawableRes;
5-
import android.support.annotation.NonNull;
6-
import android.support.v7.app.ActionBar;
7-
import android.support.v7.app.AppCompatActivity;
8-
import android.support.v7.widget.Toolbar;
94
import android.util.Pair;
10-
import android.view.KeyEvent;
115
import android.view.View;
126
import android.webkit.WebView;
13-
import android.widget.*;
14-
15-
import java.util.*;
16-
17-
import retrofit2.*;
7+
import android.widget.CompoundButton;
8+
import android.widget.ImageView;
9+
import android.widget.Switch;
10+
import android.widget.TextView;
11+
12+
import androidx.annotation.DrawableRes;
13+
import androidx.annotation.NonNull;
14+
import androidx.appcompat.app.ActionBar;
15+
import androidx.appcompat.app.AppCompatActivity;
16+
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
import retrofit2.Call;
23+
import retrofit2.Callback;
24+
import retrofit2.Response;
1825
import ru.coolsoft.iris2go.rest.Engine;
1926
import ru.coolsoft.iris2go.rest.ResponseDto;
2027

@@ -60,8 +67,8 @@ public class MainActivity extends AppCompatActivity {
6067
"\"");
6168
}};
6269

70+
private final ArrayList<Pair<Lang, String>> mHistory = new ArrayList<>();
6371
private Lang mLangFrom = DEFAULT_LANG_FROM;
64-
private ArrayList<Pair<Lang, String>> mHistory = new ArrayList<>();
6572
private int mHistoryHeadIndex;
6673

6774
private TextView mInput;
@@ -78,31 +85,26 @@ protected void onCreate(Bundle savedInstanceState) {
7885
mLangFrom = Lang.valueOf(savedInstanceState.getInt(KEY_LANG, DEFAULT_LANG_FROM.mLang));
7986
}
8087

81-
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
88+
setSupportActionBar(findViewById(R.id.toolbar));
8289
ActionBar actionBar = Objects.requireNonNull(getSupportActionBar());
8390
actionBar.setIcon(R.drawable.logo);
8491

8592
mInput = findViewById(R.id.input);
8693
mResult = findViewById(R.id.result);
87-
Objects.requireNonNull(findViewById(R.id.input)).setOnKeyListener(new View.OnKeyListener() {
88-
@Override
89-
public boolean onKey(View v, int keyCode, KeyEvent event) {
90-
if (keyCode == 66 && event.getAction() == ACTION_UP) {
91-
translate(true);
92-
return true;
94+
Objects.requireNonNull((View) findViewById(R.id.input)).setOnKeyListener(
95+
(v, keyCode, event) -> {
96+
if (keyCode == 66 && event.getAction() == ACTION_UP) {
97+
translate(true);
98+
return true;
99+
}
100+
return false;
93101
}
94-
return false;
95-
}
96-
});
102+
);
97103

98104
mTranscript = findViewById(R.id.transcript);
99105
mNekudot = findViewById(R.id.nekudot);
100-
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
101-
@Override
102-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
103-
translate(true);
104-
}
105-
};
106+
CompoundButton.OnCheckedChangeListener checkedChangeListener =
107+
(buttonView, isChecked) -> translate(true);
106108
mTranscript.setOnCheckedChangeListener(checkedChangeListener);
107109
mNekudot.setOnCheckedChangeListener(checkedChangeListener);
108110

@@ -137,13 +139,19 @@ private void translate(boolean updateHistory) {
137139
mHistory.add(mHistoryHeadIndex++, historyItem);
138140
}
139141
}
142+
143+
findViewById(R.id.loader).setVisibility(View.VISIBLE);
140144
Engine.translate(text.toString(),
141145
mLangFrom.mLang,
142146
text.charAt(0) >= 'А' && text.charAt(0) <= 'я',
143147
mTranscript.isChecked(),
144148
mNekudot.isChecked(),
145149

146150
new Callback<ResponseDto>() {
151+
private void doFinally() {
152+
findViewById(R.id.loader).setVisibility(View.GONE);
153+
}
154+
147155
@Override
148156
public void onResponse(@NonNull Call<ResponseDto> call, @NonNull Response<ResponseDto> response) {
149157
ResponseDto dto = response.body();
@@ -153,37 +161,39 @@ public void onResponse(@NonNull Call<ResponseDto> call, @NonNull Response<Respon
153161
}
154162

155163
mResult.loadDataWithBaseURL(null, text, "text/html; charset=utf-8", "UTF-8", null);
164+
doFinally();
156165
}
157166

158167
@Override
159168
public void onFailure(@NonNull Call<ResponseDto> call, @NonNull Throwable t) {
160169
t.printStackTrace();
161170

162171
mResult.loadDataWithBaseURL(null, t.getMessage(), "text/html", null, null);
172+
doFinally();
163173
}
164174
}
165175
);
166176
}
167177

168178
private void updateLangs() {
169-
Lang langTo = get2ndParty(DEFAULT_LANG_PAIR);
170-
((ImageView) Objects.requireNonNull(findViewById(R.id.flag_from))).setImageResource(mLangFrom.mIcon);
171-
((ImageView) Objects.requireNonNull(findViewById(R.id.flag_to))).setImageResource(langTo.mIcon);
179+
Lang langTo = get2ndParty();
180+
((ImageView) Objects.requireNonNull((View) findViewById(R.id.flag_from))).setImageResource(mLangFrom.mIcon);
181+
((ImageView) Objects.requireNonNull((View) findViewById(R.id.flag_to))).setImageResource(langTo.mIcon);
172182
}
173183

174184
@Override
175-
protected void onSaveInstanceState(Bundle outState) {
185+
protected void onSaveInstanceState(@NonNull Bundle outState) {
176186
super.onSaveInstanceState(outState);
177187
outState.putInt(KEY_LANG, mLangFrom.mLang);
178188
}
179189

180190
public void onSwap(View view) {
181-
mLangFrom = get2ndParty(DEFAULT_LANG_PAIR);
191+
mLangFrom = get2ndParty();
182192
updateLangs();
183193
}
184194

185-
private Lang get2ndParty(Lang pair) {
186-
return Lang.valueOf(pair.mLang - mLangFrom.mLang);
195+
private Lang get2ndParty() {
196+
return Lang.valueOf(MainActivity.DEFAULT_LANG_PAIR.mLang - mLangFrom.mLang);
187197
}
188198

189199
private enum Lang {

app/src/main/java/ru/coolsoft/iris2go/rest/Engine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.coolsoft.iris2go.rest;
22

3-
import android.support.annotation.NonNull;
4-
import android.support.annotation.Nullable;
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
55

66
import retrofit2.*;
77
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<android.support.v7.widget.Toolbar
9+
<androidx.appcompat.widget.Toolbar
1010
android:id="@+id/toolbar"
1111
android:layout_width="match_parent"
1212
android:layout_height="?attr/actionBarSize"
@@ -72,7 +72,8 @@
7272
android:layout_margin="@dimen/indent"
7373
app:layout_constraintTop_toBottomOf="@id/txt_from"
7474
android:hint="@string/input_hint"
75-
android:inputType="text|textNoSuggestions" />
75+
android:inputType="text|textNoSuggestions"
76+
android:importantForAutofill="no" />
7677

7778
<Switch
7879
android:id="@+id/nekudot"
@@ -98,4 +99,28 @@
9899
app:layout_constraintTop_toBottomOf="@+id/transcript"
99100
app:layout_constraintBottom_toBottomOf="parent" />
100101

101-
</android.support.constraint.ConstraintLayout>
102+
<androidx.constraintlayout.widget.ConstraintLayout
103+
android:id="@+id/loader"
104+
android:layout_width="0dp"
105+
android:layout_height="0dp"
106+
android:visibility="gone"
107+
android:alpha="0.7"
108+
android:background="@android:color/white"
109+
app:layout_constraintBottom_toBottomOf="@id/result"
110+
app:layout_constraintEnd_toEndOf="@id/result"
111+
app:layout_constraintStart_toStartOf="@id/result"
112+
app:layout_constraintTop_toTopOf="@id/result">
113+
114+
<ProgressBar
115+
android:id="@+id/progressBar"
116+
style="?android:attr/progressBarStyle"
117+
android:layout_width="wrap_content"
118+
android:layout_height="wrap_content"
119+
app:layout_constraintTop_toTopOf="parent"
120+
app:layout_constraintBottom_toBottomOf="parent"
121+
app:layout_constraintStart_toStartOf="parent"
122+
app:layout_constraintEnd_toEndOf="parent"
123+
/>
124+
</androidx.constraintlayout.widget.ConstraintLayout>
125+
126+
</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ buildscript {
44

55
repositories {
66
google()
7-
jcenter()
7+
mavenCentral()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.1.3'
10+
classpath 'com.android.tools.build:gradle:4.2.2'
1111

1212

1313
// NOTE: Do not place your application dependencies here; they belong
@@ -18,7 +18,7 @@ buildscript {
1818
allprojects {
1919
repositories {
2020
google()
21-
jcenter()
21+
mavenCentral()
2222
}
2323
}
2424

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# http://www.gradle.org/docs/current/userguide/build_environment.html
77
# Specifies the JVM arguments used for the daemon process.
88
# The setting is particularly useful for tweaking memory settings.
9+
android.enableJetifier=true
10+
android.useAndroidX=true
911
org.gradle.jvmargs=-Xmx1536m
1012
# When configured, Gradle will run in incubating parallel mode.
1113
# This option should only be used with decoupled projects. More details, visit

0 commit comments

Comments
 (0)