11package ru .coolsoft .iris2go ;
22
33import 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 ;
94import android .util .Pair ;
10- import android .view .KeyEvent ;
115import android .view .View ;
126import 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 ;
1825import ru .coolsoft .iris2go .rest .Engine ;
1926import 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 {
0 commit comments