списание бонусов при покупке(часть)
This commit is contained in:
@@ -61,279 +61,278 @@ import okhttp3.ResponseBody;
|
|||||||
|
|
||||||
public abstract class AbstractScannerActivity extends AppCompatActivity {
|
public abstract class AbstractScannerActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private final static String TAG = "Checker.ScannerActivity";
|
private final static String TAG = "Checker.ScannerActivity";
|
||||||
|
|
||||||
private int counter;
|
private int counter;
|
||||||
|
|
||||||
public static final String SCAN_MODES = "SCAN_MODES";
|
public static final String SCAN_MODES = "SCAN_MODES";
|
||||||
public static final String ERROR_INFO = "ERROR_INFO";
|
public static final String ERROR_INFO = "ERROR_INFO";
|
||||||
|
|
||||||
protected LogoutDialogFragment logoutDialog;
|
protected LogoutDialogFragment logoutDialog;
|
||||||
private ApiClient mClient;
|
private ApiClient mClient;
|
||||||
|
|
||||||
boolean isCameraAvailable() {
|
boolean isCameraAvailable() {
|
||||||
Log.d(TAG, "isCameraAvailable");
|
Log.d(TAG, "isCameraAvailable");
|
||||||
PackageManager pm = getPackageManager();
|
PackageManager pm = getPackageManager();
|
||||||
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
|
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancelRequest(final @NonNull String message) {
|
||||||
|
Log.d(TAG, "cancelRequest: " + message);
|
||||||
|
final Intent response = new Intent();
|
||||||
|
response.putExtra(ERROR_INFO, message);
|
||||||
|
setResult(RESULT_CANCELED, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
final String appToken = getIntent().getStringExtra(MainActivity.PREF_APP_TOKEN);
|
||||||
|
final String token = getIntent().getStringExtra(MainActivity.PREF_POS_TOKEN);
|
||||||
|
final String url = getIntent().getStringExtra(MainActivity.PREF_API_URL) + "/users/";
|
||||||
|
mClient = new ApiClient(url, appToken, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initialize activity
|
||||||
|
* - removes windows title
|
||||||
|
* - set content view to layout id
|
||||||
|
*
|
||||||
|
* @param layoutID layout to use
|
||||||
|
*/
|
||||||
|
protected final boolean init(final int layoutID) {
|
||||||
|
Log.d(TAG, "init");
|
||||||
|
if (!isCameraAvailable()) {
|
||||||
|
// Cancel request if there is no rear-facing camera.
|
||||||
|
cancelRequest("Camera unavailable");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Hide the window title.
|
||||||
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
setContentView(layoutID);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure toolbar of app
|
||||||
|
*/
|
||||||
|
protected final void initToolbar(Intent intent) {
|
||||||
|
|
||||||
|
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||||
|
toolbar.setBackgroundColor((int) intent.getLongExtra(MainActivity.PREF_APP_BAR_COLOR, 0xffffff));
|
||||||
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
|
final ActionBar actionBar = getSupportActionBar();
|
||||||
|
|
||||||
|
if (actionBar != null) {
|
||||||
|
actionBar.setTitle(null);
|
||||||
|
actionBar.setDisplayHomeAsUpEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cancelRequest(final @NonNull String message) {
|
initManualInput();
|
||||||
Log.d(TAG, "cancelRequest: " + message);
|
setupSecretClickHandler(toolbar);
|
||||||
final Intent response = new Intent();
|
}
|
||||||
response.putExtra(ERROR_INFO, message);
|
|
||||||
setResult(RESULT_CANCELED, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
private void initManualInput() {
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
EditText manualInput = (EditText) findViewById(R.id.manual_input);
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
final String appToken = getIntent().getStringExtra(MainActivity.PREF_APP_TOKEN);
|
|
||||||
final String token = getIntent().getStringExtra(MainActivity.PREF_POS_TOKEN);
|
|
||||||
final String url = getIntent().getStringExtra(MainActivity.PREF_API_URL) + "/users/";
|
|
||||||
mClient = new ApiClient(url, appToken, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* initialize activity
|
manualInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||||
* - removes windows title
|
@Override
|
||||||
* - set content view to layout id
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||||
*
|
handleBarcode(v.getText().toString());
|
||||||
* @param layoutID layout to use
|
return false;
|
||||||
*/
|
}
|
||||||
protected final boolean init(final int layoutID) {
|
});
|
||||||
Log.d(TAG, "init");
|
|
||||||
if (!isCameraAvailable()) {
|
}
|
||||||
// Cancel request if there is no rear-facing camera.
|
|
||||||
cancelRequest("Camera unavailable");
|
private void setupSecretClickHandler(final @NonNull View toolbar) {
|
||||||
return false;
|
// Configure increment handler
|
||||||
|
toolbar.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
if (counter == 15) {
|
||||||
|
switchScanner();
|
||||||
|
} else {
|
||||||
|
counter++;
|
||||||
}
|
}
|
||||||
// Hide the window title.
|
Log.d(TAG, "toolbar clicked " + counter + " times");
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
}
|
||||||
setContentView(layoutID);
|
});
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private void switchScanner() {
|
||||||
* Configure toolbar of app
|
|
||||||
*/
|
|
||||||
protected final void initToolbar(Intent intent) {
|
|
||||||
|
|
||||||
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
final SharedPreferences prefs = getSharedPreferences("scanner", Context.MODE_PRIVATE);
|
||||||
toolbar.setBackgroundColor((int) intent.getLongExtra(MainActivity.PREF_APP_BAR_COLOR, 0xffffff));
|
|
||||||
setSupportActionBar(toolbar);
|
|
||||||
|
|
||||||
final ActionBar actionBar = getSupportActionBar();
|
int idx = (prefs.getInt(MainActivity.SCANNER_BACKEND_KEY, 0) == MainActivity.ZXING)
|
||||||
|
? MainActivity.ZBAR
|
||||||
|
: MainActivity.ZXING;
|
||||||
|
|
||||||
if (actionBar != null) {
|
Log.d(TAG, "switch to scanner backend " + idx + ", " + MainActivity.SCANNER_BACKEND[idx].toString());
|
||||||
actionBar.setTitle(null);
|
prefs.edit().putInt(MainActivity.SCANNER_BACKEND_KEY, idx).apply();
|
||||||
actionBar.setDisplayHomeAsUpEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
initManualInput();
|
cancelRequest("Scanner backend changed");
|
||||||
setupSecretClickHandler(toolbar);
|
setResult(RESULT_OK);
|
||||||
}
|
finish();
|
||||||
|
}
|
||||||
private void initManualInput() {
|
|
||||||
EditText manualInput = (EditText) findViewById(R.id.manual_input);
|
|
||||||
|
|
||||||
|
|
||||||
manualInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
/**
|
||||||
@Override
|
* Adds scanner view to target frame layout
|
||||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
*/
|
||||||
handleBarcode(v.getText().toString());
|
protected final void addScanner(final @NonNull View view, final int targetFrameId) {
|
||||||
return false;
|
Log.d(TAG, "addScanner");
|
||||||
}
|
final FrameLayout root = (FrameLayout) findViewById(targetFrameId);
|
||||||
});
|
root.addView(view, 0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
protected abstract View initScanner();
|
||||||
|
|
||||||
private void setupSecretClickHandler(final @NonNull View toolbar) {
|
public void handleBarcode(final @NonNull String card) {
|
||||||
// Configure increment handler
|
mClient.findUser(card, new Callback() {
|
||||||
toolbar.setOnClickListener(new View.OnClickListener() {
|
@Override
|
||||||
@Override
|
public void onFailure(Call call, IOException e) {
|
||||||
public void onClick(View view) {
|
handleFail(card);
|
||||||
if (counter == 15) {
|
}
|
||||||
switchScanner();
|
@Override
|
||||||
|
public void onResponse(Call call, Response response) throws IOException {
|
||||||
|
try {
|
||||||
|
ResponseBody body = response.body();
|
||||||
|
if (body != null) {
|
||||||
|
switch (response.code()) {
|
||||||
|
case 200:
|
||||||
|
final JSONArray users = new JSONArray(body.string());
|
||||||
|
if (users.length() > 0) {
|
||||||
|
handleSuccess(card, users.get(0).toString());
|
||||||
} else {
|
} else {
|
||||||
counter++;
|
handleFail(card);
|
||||||
}
|
}
|
||||||
Log.d(TAG, "toolbar clicked " + counter + " times");
|
break;
|
||||||
}
|
case 204:
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void switchScanner() {
|
|
||||||
|
|
||||||
final SharedPreferences prefs = getSharedPreferences("scanner", Context.MODE_PRIVATE);
|
|
||||||
|
|
||||||
int idx = (prefs.getInt(MainActivity.SCANNER_BACKEND_KEY, 0) == MainActivity.ZXING)
|
|
||||||
? MainActivity.ZBAR
|
|
||||||
: MainActivity.ZXING;
|
|
||||||
|
|
||||||
Log.d(TAG, "switch to scanner backend " + idx + ", " + MainActivity.SCANNER_BACKEND[idx].toString());
|
|
||||||
prefs.edit().putInt(MainActivity.SCANNER_BACKEND_KEY, idx).apply();
|
|
||||||
|
|
||||||
cancelRequest("Scanner backend changed");
|
|
||||||
setResult(RESULT_OK);
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds scanner view to target frame layout
|
|
||||||
*/
|
|
||||||
protected final void addScanner(final @NonNull View view, final int targetFrameId) {
|
|
||||||
Log.d(TAG, "addScanner");
|
|
||||||
final FrameLayout root = (FrameLayout) findViewById(targetFrameId);
|
|
||||||
root.addView(view, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract View initScanner();
|
|
||||||
|
|
||||||
public void handleBarcode(final @NonNull String card) {
|
|
||||||
mClient.findUser(card, new Callback() {
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call call, IOException e) {
|
|
||||||
handleFail(card);
|
handleFail(card);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Override
|
} catch (final IOException | JSONException e) {
|
||||||
public void onResponse(Call call, Response response) throws IOException {
|
Log.e(TAG, e.getMessage(), e);
|
||||||
try {
|
handleFail(card);
|
||||||
ResponseBody body = response.body();
|
|
||||||
if (body != null) {
|
|
||||||
switch (response.code()) {
|
|
||||||
case 200:
|
|
||||||
final JSONArray users = new JSONArray(body.string());
|
|
||||||
if (users.length() > 0) {
|
|
||||||
handleSuccess(card, users.get(0).toString());
|
|
||||||
} else {
|
|
||||||
handleFail(card);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 204:
|
|
||||||
handleFail(card);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (final IOException | JSONException e) {
|
|
||||||
Log.e(TAG, e.getMessage(), e);
|
|
||||||
handleFail(card);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final void handleSuccess(final String card, final String user) {
|
|
||||||
runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
setResult(RESULT_OK, new Intent().putExtra("user", user).putExtra("card", card));
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final void handleFail(final String card) {
|
|
||||||
runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
String message = String.format(getString(R.string.identifier_not_found), card)
|
|
||||||
+ ".\n"
|
|
||||||
+ String.format(getString(R.string.error_contact_support), BuildConfig.supportPhone);
|
|
||||||
Toast.makeText(AbstractScannerActivity.this, message, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
|
||||||
getMenuInflater().inflate(R.menu.menu, menu);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
||||||
menu.findItem(R.id.settings).setIcon(getResources().getDrawable(R.drawable.settings));
|
|
||||||
menu.findItem(R.id.faq).setIcon(getResources().getDrawable(R.drawable.help));
|
|
||||||
menu.findItem(R.id.logout).setIcon(getResources().getDrawable(R.drawable.logout));
|
|
||||||
return super.onPrepareOptionsMenu(menu);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
if (item.getItemId() == R.id.settings) {
|
|
||||||
final Intent intent = new Intent();
|
|
||||||
intent.putExtra("item", "settings");
|
|
||||||
setResult(RESULT_OK, intent);
|
|
||||||
finish();
|
|
||||||
return true;
|
|
||||||
} else if (item.getItemId() == R.id.logout) {
|
|
||||||
int color = (int) getIntent().getLongExtra(MainActivity.PREF_APP_BAR_COLOR, 0xffffff);
|
|
||||||
logoutDialog = LogoutDialogFragment.newInstance(color);
|
|
||||||
logoutDialog.show(getFragmentManager(), "logout");
|
|
||||||
return true;
|
|
||||||
} else if (item.getItemId() == R.id.faq) {
|
|
||||||
final Intent intent = new Intent();
|
|
||||||
intent.putExtra("item", "faq");
|
|
||||||
setResult(RESULT_OK, intent);
|
|
||||||
finish();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
}
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void dismissDialog() {
|
protected final void handleSuccess(final String card, final String user) {
|
||||||
if (logoutDialog != null) {
|
runOnUiThread(new Runnable() {
|
||||||
logoutDialog.dismiss();
|
@Override
|
||||||
logoutDialog = null;
|
public void run() {
|
||||||
}
|
setResult(RESULT_OK, new Intent().putExtra("user", user).putExtra("card", card));
|
||||||
}
|
|
||||||
|
|
||||||
void logout() {
|
|
||||||
final Intent intent = new Intent();
|
|
||||||
intent.putExtra("item", "logout");
|
|
||||||
setResult(RESULT_OK, intent);
|
|
||||||
finish();
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void handleFail(final String card) {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String message = String.format(getString(R.string.identifier_not_found), card)
|
||||||
|
+ ".\n"
|
||||||
|
+ String.format(getString(R.string.error_contact_support), BuildConfig.supportPhone);
|
||||||
|
Toast.makeText(AbstractScannerActivity.this, message, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
getMenuInflater().inflate(R.menu.menu, menu);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||||
|
menu.findItem(R.id.settings).setIcon(getResources().getDrawable(R.drawable.settings));
|
||||||
|
menu.findItem(R.id.faq).setIcon(getResources().getDrawable(R.drawable.help));
|
||||||
|
menu.findItem(R.id.logout).setIcon(getResources().getDrawable(R.drawable.logout));
|
||||||
|
return super.onPrepareOptionsMenu(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
if (item.getItemId() == R.id.settings) {
|
||||||
|
final Intent intent = new Intent();
|
||||||
|
intent.putExtra("item", "settings");
|
||||||
|
setResult(RESULT_OK, intent);
|
||||||
|
finish();
|
||||||
|
return true;
|
||||||
|
} else if (item.getItemId() == R.id.logout) {
|
||||||
|
int color = (int) getIntent().getLongExtra(MainActivity.PREF_APP_BAR_COLOR, 0xffffff);
|
||||||
|
logoutDialog = LogoutDialogFragment.newInstance(color);
|
||||||
|
logoutDialog.show(getFragmentManager(), "logout");
|
||||||
|
return true;
|
||||||
|
} else if (item.getItemId() == R.id.faq) {
|
||||||
|
final Intent intent = new Intent();
|
||||||
|
intent.putExtra("item", "faq");
|
||||||
|
setResult(RESULT_OK, intent);
|
||||||
|
finish();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dismissDialog() {
|
||||||
|
if (logoutDialog != null) {
|
||||||
|
logoutDialog.dismiss();
|
||||||
|
logoutDialog = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void logout() {
|
||||||
|
final Intent intent = new Intent();
|
||||||
|
intent.putExtra("item", "logout");
|
||||||
|
setResult(RESULT_OK, intent);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LogoutDialogFragment extends DialogFragment {
|
||||||
|
|
||||||
|
static LogoutDialogFragment newInstance(int color) {
|
||||||
|
LogoutDialogFragment f = new LogoutDialogFragment();
|
||||||
|
Bundle bundle = new Bundle();
|
||||||
|
bundle.putInt(MainActivity.PREF_APP_BAR_COLOR, color);
|
||||||
|
f.setArguments(bundle);
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class LogoutDialogFragment extends DialogFragment {
|
@Override
|
||||||
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
static LogoutDialogFragment newInstance(int color) {
|
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||||
LogoutDialogFragment f = new LogoutDialogFragment();
|
final LayoutInflater inflater = getActivity().getLayoutInflater();
|
||||||
Bundle bundle = new Bundle();
|
final View content = inflater.inflate(R.layout.f_logout_dialog, null);
|
||||||
bundle.putInt(MainActivity.PREF_APP_BAR_COLOR, color);
|
builder.setView(content);
|
||||||
f.setArguments(bundle);
|
final TextView positiveButton = (TextView) content.findViewById(R.id.positiveButton);
|
||||||
return f;
|
final TextView negativeButton = (TextView) content.findViewById(R.id.negativeButton);
|
||||||
}
|
// Можно делать через стили, но я не уверен, что необходимо в нашем случае
|
||||||
|
positiveButton.setTextColor(getArguments().getInt(MainActivity.PREF_APP_BAR_COLOR));
|
||||||
|
negativeButton.setTextColor(getArguments().getInt(MainActivity.PREF_APP_BAR_COLOR));
|
||||||
|
|
||||||
|
negativeButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public void onClick(View view) {
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
((AbstractScannerActivity) getActivity()).dismissDialog();
|
||||||
final LayoutInflater inflater = getActivity().getLayoutInflater();
|
|
||||||
final View content = inflater.inflate(R.layout.f_logout_dialog, null);
|
|
||||||
builder.setView(content);
|
|
||||||
final TextView positiveButton = (TextView) content.findViewById(R.id.positiveButton);
|
|
||||||
final TextView negativeButton = (TextView) content.findViewById(R.id.negativeButton);
|
|
||||||
// Можно делать через стили, но я не уверен, что необходимо в нашем случае
|
|
||||||
positiveButton.setTextColor(getArguments().getInt(MainActivity.PREF_APP_BAR_COLOR));
|
|
||||||
negativeButton.setTextColor(getArguments().getInt(MainActivity.PREF_APP_BAR_COLOR));
|
|
||||||
|
|
||||||
negativeButton.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View view) {
|
|
||||||
((AbstractScannerActivity) getActivity()).dismissDialog();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
positiveButton.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View view) {
|
|
||||||
((AbstractScannerActivity) getActivity()).logout();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return builder.create();
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
positiveButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
((AbstractScannerActivity) getActivity()).logout();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return builder.create();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ public class MainActivity extends FlutterActivity {
|
|||||||
break;
|
break;
|
||||||
case "startScanner":
|
case "startScanner":
|
||||||
mScannerArgs = call.arguments();
|
mScannerArgs = call.arguments();
|
||||||
|
System.out.println(mScannerArgs.toString());
|
||||||
startScannerActivity();
|
startScannerActivity();
|
||||||
break;
|
break;
|
||||||
case "isOnline":
|
case "isOnline":
|
||||||
|
|||||||
@@ -139,7 +139,8 @@ abstract class BaseState<T extends StatefulWidget> extends State<T> {
|
|||||||
return new Container(margin: new EdgeInsets.only(left: verticalMargin, right: verticalMargin),
|
return new Container(margin: new EdgeInsets.only(left: verticalMargin, right: verticalMargin),
|
||||||
padding: getInputFieldContainerPadding(),
|
padding: getInputFieldContainerPadding(),
|
||||||
decoration: getInputFieldContainerDecoration(),
|
decoration: getInputFieldContainerDecoration(),
|
||||||
child: getTextWidget());
|
child: getTextWidget()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Возвращает поле ввода.
|
/// Возвращает поле ввода.
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ startScanner(BuildContext context, String app, SqliteHelper helper) async {
|
|||||||
String userString = call.arguments[0];
|
String userString = call.arguments[0];
|
||||||
print(userString);
|
print(userString);
|
||||||
String card = call.arguments[1];
|
String card = call.arguments[1];
|
||||||
|
print('$userString, $card');
|
||||||
var route = new MaterialPageRoute<Null>(
|
var route = new MaterialPageRoute<Null>(
|
||||||
builder: (BuildContext context) =>
|
builder: (BuildContext context) =>
|
||||||
new PurchaseScreen(helper, app, userString, card));
|
new PurchaseScreen(helper, app, userString, card));
|
||||||
@@ -155,7 +156,7 @@ startScanner(BuildContext context, String app, SqliteHelper helper) async {
|
|||||||
});
|
});
|
||||||
|
|
||||||
platform.invokeMethod('startScanner', {
|
platform.invokeMethod('startScanner', {
|
||||||
'token': await platform.invokeMethod('getAppToken'),
|
'token': token,
|
||||||
'url': await platform.invokeMethod('getEndpoint'),
|
'url': await platform.invokeMethod('getEndpoint'),
|
||||||
'appToken': await platform.invokeMethod('getAppToken'),
|
'appToken': await platform.invokeMethod('getAppToken'),
|
||||||
'locale': Intl.defaultLocale,
|
'locale': Intl.defaultLocale,
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ class MessageLookup extends MessageLookupByLibrary {
|
|||||||
"discount_sum" : MessageLookupByLibrary.simpleMessage("Dicount Sum"),
|
"discount_sum" : MessageLookupByLibrary.simpleMessage("Dicount Sum"),
|
||||||
"bonus_plus" : MessageLookupByLibrary.simpleMessage("Bonus points"),
|
"bonus_plus" : MessageLookupByLibrary.simpleMessage("Bonus points"),
|
||||||
"bonus_minus" : MessageLookupByLibrary.simpleMessage("Bonus was charged"),
|
"bonus_minus" : MessageLookupByLibrary.simpleMessage("Bonus was charged"),
|
||||||
|
"bonus_hint" : MessageLookupByLibrary.simpleMessage("Points to charge"),
|
||||||
|
"bonus_explanation" : MessageLookupByLibrary.simpleMessage("If not set, points will be added"),
|
||||||
"scan" : MessageLookupByLibrary.simpleMessage("Scan"),
|
"scan" : MessageLookupByLibrary.simpleMessage("Scan"),
|
||||||
"sign_up" : MessageLookupByLibrary.simpleMessage("Sign Up"),
|
"sign_up" : MessageLookupByLibrary.simpleMessage("Sign Up"),
|
||||||
"specify_din_store" : MessageLookupByLibrary.simpleMessage("Specify the store ID"),
|
"specify_din_store" : MessageLookupByLibrary.simpleMessage("Specify the store ID"),
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ class MessageLookup extends MessageLookupByLibrary {
|
|||||||
"discount_sum" : MessageLookupByLibrary.simpleMessage("Сумма скидки"),
|
"discount_sum" : MessageLookupByLibrary.simpleMessage("Сумма скидки"),
|
||||||
"bonus_plus" : MessageLookupByLibrary.simpleMessage("Бонусов начислено"),
|
"bonus_plus" : MessageLookupByLibrary.simpleMessage("Бонусов начислено"),
|
||||||
"bonus_minus" : MessageLookupByLibrary.simpleMessage("Бонусов списано"),
|
"bonus_minus" : MessageLookupByLibrary.simpleMessage("Бонусов списано"),
|
||||||
|
"bonus_hint" : MessageLookupByLibrary.simpleMessage("Сколько баллов списать?"),
|
||||||
|
"bonus_explanation" : MessageLookupByLibrary.simpleMessage("Если не указано сколько баллов списать, баллы будут начислены"),
|
||||||
"scan" : MessageLookupByLibrary.simpleMessage("Сканировать"),
|
"scan" : MessageLookupByLibrary.simpleMessage("Сканировать"),
|
||||||
"sign_up" : MessageLookupByLibrary.simpleMessage("Зарегистрироваться"),
|
"sign_up" : MessageLookupByLibrary.simpleMessage("Зарегистрироваться"),
|
||||||
"specify_din_store" : MessageLookupByLibrary.simpleMessage("Необходимо указать ID магазина"),
|
"specify_din_store" : MessageLookupByLibrary.simpleMessage("Необходимо указать ID магазина"),
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
/// (удаляет запрещенные символы до их отображаения).
|
/// (удаляет запрещенные символы до их отображаения).
|
||||||
TextEditingController controller = new TextEditingController();
|
TextEditingController controller = new TextEditingController();
|
||||||
|
|
||||||
|
TextEditingController bonusController = new TextEditingController();
|
||||||
|
|
||||||
PurchaseScreenState(SqliteHelper helper, String app, String userString, String card) {
|
PurchaseScreenState(SqliteHelper helper, String app, String userString, String card) {
|
||||||
this.user = JSON.decode(userString);
|
this.user = JSON.decode(userString);
|
||||||
this.card = card;
|
this.card = card;
|
||||||
@@ -62,13 +64,19 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
));
|
));
|
||||||
widgets.add(getValueWithDescription(StringsLocalization.card(), card));
|
widgets.add(getValueWithDescription(StringsLocalization.card(), card));
|
||||||
|
|
||||||
|
widgets.add(getValueWithDescription(StringsLocalization.reward(), loyalty));
|
||||||
|
|
||||||
if (bonus.length > 0) {
|
if (bonus.length > 0) {
|
||||||
widgets.add(getValueWithDescription(StringsLocalization.bonus(), bonus));
|
widgets.add(getValueWithDescription(StringsLocalization.bonus(), bonus));
|
||||||
}
|
}
|
||||||
|
|
||||||
widgets.add(getValueWithDescription(StringsLocalization.reward(), loyalty));
|
|
||||||
widgets.add(getHintLabel());
|
widgets.add(getHintLabel());
|
||||||
widgets.add(getInputField());
|
|
||||||
|
widgets.add(getInputField()); // Нельзя добавить еще одно поле таким же способом
|
||||||
|
|
||||||
|
widgets.add(getBonusInputField());
|
||||||
|
|
||||||
|
|
||||||
widgets.add(wrapButton(getScreenMargins(36.0), getCompleteButton()));
|
widgets.add(wrapButton(getScreenMargins(36.0), getCompleteButton()));
|
||||||
widgets.add(wrapButton(
|
widgets.add(wrapButton(
|
||||||
getScreenMargins(24.0),
|
getScreenMargins(24.0),
|
||||||
@@ -84,6 +92,47 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getBonusInputField() {
|
||||||
|
return new Column(
|
||||||
|
children: <Widget>[
|
||||||
|
new Container(
|
||||||
|
margin: new EdgeInsets.only(
|
||||||
|
left: verticalMargin,
|
||||||
|
right: verticalMargin,
|
||||||
|
top: verticalMargin
|
||||||
|
),
|
||||||
|
padding: getInputFieldContainerPadding(),
|
||||||
|
decoration: getInputFieldContainerDecoration(),
|
||||||
|
child: new TextField(
|
||||||
|
keyboardType: TextInputType.number,
|
||||||
|
decoration: new InputDecoration.collapsed(
|
||||||
|
hintText: StringsLocalization.bonusHint(),
|
||||||
|
hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0)),
|
||||||
|
controller: bonusController,
|
||||||
|
onSubmitted: (String text) {
|
||||||
|
setState(() {
|
||||||
|
bonusController.text = text;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
autofocus: true,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
new Container(
|
||||||
|
margin: new EdgeInsets.only(
|
||||||
|
top: 5.0
|
||||||
|
),
|
||||||
|
child: new Text(
|
||||||
|
StringsLocalization.bonusExplanation(),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
style: new TextStyle(color: greyTextColor, fontSize: 11.0),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
getScreenMargins(double top) {
|
getScreenMargins(double top) {
|
||||||
double side = 42.0;
|
double side = 42.0;
|
||||||
return new EdgeInsets.only(top: top, left: side, right: side);
|
return new EdgeInsets.only(top: top, left: side, right: side);
|
||||||
@@ -118,6 +167,7 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
getTextWidget() {
|
getTextWidget() {
|
||||||
|
// ?? TODO переделать, т.к. позволяет иметь только одно поле ввода на странице
|
||||||
return new TextField(
|
return new TextField(
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
decoration: new InputDecoration.collapsed(
|
decoration: new InputDecoration.collapsed(
|
||||||
@@ -195,7 +245,9 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
child: new AlertDialog(
|
child: new AlertDialog(
|
||||||
title: new Text(StringsLocalization.confirmation()),
|
title: new Text(StringsLocalization.confirmation()),
|
||||||
content:
|
content:
|
||||||
new Text(StringsLocalization.confirmPurchase(val, currency)),
|
new Text(
|
||||||
|
StringsLocalization.confirmPurchase(val, currency)
|
||||||
|
),
|
||||||
actions: <Widget>[
|
actions: <Widget>[
|
||||||
new FlatButton(
|
new FlatButton(
|
||||||
child: new Text(StringsLocalization.no()),
|
child: new Text(StringsLocalization.no()),
|
||||||
@@ -213,6 +265,24 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apiErrorAlert(String errorText) {
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
child: new AlertDialog(
|
||||||
|
//title: new Text(StringsLocalization.()),
|
||||||
|
content: new Text(errorText),
|
||||||
|
actions: <Widget>[
|
||||||
|
new FlatButton(
|
||||||
|
child: new Text('Ok'),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
purchase(String sumTotal) async {
|
purchase(String sumTotal) async {
|
||||||
if (await platform.invokeMethod('isOnline')) {
|
if (await platform.invokeMethod('isOnline')) {
|
||||||
if (!purchaseInProgress) {
|
if (!purchaseInProgress) {
|
||||||
@@ -229,17 +299,28 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
'sum_total': sumTotal
|
'sum_total': sumTotal
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (bonusController.text.length > 0) {
|
||||||
|
body['bonus_payment'] = bonusController.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
print(body['bonus_payment']);
|
||||||
|
|
||||||
var purchaseResponse;
|
var purchaseResponse;
|
||||||
|
Map purchase;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
purchaseResponse = await getPurchaseRequest(user['purchases_url'], body, token);
|
purchaseResponse = await getPurchaseRequest(user['purchases_url'], body, token);
|
||||||
print(purchaseResponse.body);
|
purchase = JSON.decode(purchaseResponse.body);
|
||||||
|
if (purchase['errors'] is List && purchase['errors'].length > 0) {
|
||||||
|
print(purchase['errors'][0]);
|
||||||
|
//Navigator.of(context).pop();
|
||||||
|
apiErrorAlert(purchase['errors'][0]);
|
||||||
|
}
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
purchaseInProgress = false;
|
purchaseInProgress = false;
|
||||||
print(error.toString());
|
print(error.toString());
|
||||||
}
|
|
||||||
|
|
||||||
Map purchase = JSON.decode(purchaseResponse.body);
|
}
|
||||||
|
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
|
|
||||||
|
|||||||
@@ -101,6 +101,8 @@ class StringsLocalization {
|
|||||||
static String completePurchase() => Intl.message('complite_purchase', name: 'complite_purchase', locale: Intl.defaultLocale);
|
static String completePurchase() => Intl.message('complite_purchase', name: 'complite_purchase', locale: Intl.defaultLocale);
|
||||||
static String scan() => Intl.message('scan', name: 'scan', locale: Intl.defaultLocale);
|
static String scan() => Intl.message('scan', name: 'scan', locale: Intl.defaultLocale);
|
||||||
static String buyer() => Intl.message('buyer', name: 'buyer', locale: Intl.defaultLocale);
|
static String buyer() => Intl.message('buyer', name: 'buyer', locale: Intl.defaultLocale);
|
||||||
|
static String bonusHint() => Intl.message('bonus_hint', name: 'bonus_hint', locale: Intl.defaultLocale);
|
||||||
|
static String bonusExplanation() => Intl.message('bonus_explanation', name: 'bonus_explanation', locale: Intl.defaultLocale);
|
||||||
static String discountRate() => Intl.message('discount_rate', name: 'discount_rate', locale: Intl.defaultLocale);
|
static String discountRate() => Intl.message('discount_rate', name: 'discount_rate', locale: Intl.defaultLocale);
|
||||||
static String discountSum() => Intl.message('discount_sum', name: 'discount_sum', locale: Intl.defaultLocale);
|
static String discountSum() => Intl.message('discount_sum', name: 'discount_sum', locale: Intl.defaultLocale);
|
||||||
static String bonusPlus() => Intl.message('bonus_plus', name: 'bonus_plus', locale: Intl.defaultLocale);
|
static String bonusPlus() => Intl.message('bonus_plus', name: 'bonus_plus', locale: Intl.defaultLocale);
|
||||||
|
|||||||
Reference in New Issue
Block a user