Skip to content

Commit

Permalink
-errory u shipment itemu v listu
Browse files Browse the repository at this point in the history
-posílání fotek
-oprava chyb
-rozpoznání akcí podle stavu shipmentu
  • Loading branch information
Martin Meravý committed Aug 27, 2018
1 parent 094e01c commit 7012249
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package nohe.nohe_android.activity.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
Expand All @@ -20,7 +22,8 @@
import nohe.nohe_android.activity.services.ProgressDialogService;

public class AddShipmentActivity extends AppCompatActivity {
private Button add_shipment_btn;
private Button save_shipment_btn;
private Button delete_shipment_btn;
private NavigationView navigationView;
private ImageView opener_menu_btn;
private EditText start_address_tb;
Expand All @@ -46,7 +49,8 @@ protected void onCreate(Bundle savedInstanceState) {
start_address_tb = (EditText) findViewById(R.id.start_address_tb);
finish_address_tb = (EditText) findViewById(R.id.finish_address_tb);
id_shipment_tb = (EditText) findViewById(R.id.id_shipment_tb);
add_shipment_btn = (Button) findViewById(R.id.add_shipment_btn);
save_shipment_btn = (Button) findViewById(R.id.save_shipment_btn);
delete_shipment_btn = (Button) findViewById(R.id.delete_shipment_btn);
opener_menu_btn = (ImageView) findViewById(R.id.opener_menu_btn);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
Expand All @@ -64,7 +68,7 @@ protected void onCreate(Bundle savedInstanceState) {
* Set GUI event
*/
private void setGuiEvents() {
add_shipment_btn.setOnClickListener(new View.OnClickListener() {
save_shipment_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String id_shipment = id_shipment_tb.getText().toString().trim();
String start_address = start_address_tb.getText().toString().trim();
Expand All @@ -79,11 +83,16 @@ public void onClick(View view) {
}
}
});
delete_shipment_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
deleteShipmentDialog();
}
});
}

private void setTexts() {
if (isEditMode) {
ShipmentModel shipment = database.getShipment(idShipment);
ShipmentModel shipment = database.getShipmentByIdShipment(idShipment);

id_shipment_tb.setText(shipment.ID.toString());
start_address_tb.setText(shipment.address_from);
Expand All @@ -98,6 +107,40 @@ private void setEditMode() {
isEditMode = idShipment != 0;
}

private void deleteShipmentDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(AddShipmentActivity.this);

alert.setTitle(errorController.getStringFromResourcesByName("delete_dialog_title"));
alert.setMessage(errorController.getStringFromResourcesByName("delete_dialog_text"));
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
deleteShipment();
activityController.openListShipmentActivity();
dialog.dismiss();
}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

alert.show();
}

private void deleteShipment() {
ShipmentModel shipment = new ShipmentModel(idShipment,
"", "", "", "", 0, ShipmentModel.State.NEW,
"", "", 0, true);

database.deleteShipment(shipment);
}

private void saveShipment(String id_shipment, String start_address, String finish_address) {
ShipmentModel shipment = new ShipmentModel(Integer.parseInt(id_shipment),
start_address, finish_address, "", "", 0, ShipmentModel.State.NEW,
Expand All @@ -121,18 +164,18 @@ private void insertShipment(ShipmentModel shipment) {
}

private void updateShipment(ShipmentModel shipment) {
ShipmentModel existingShipment = database.getShipment(shipment.ID);
ShipmentModel existingShipment = database.getShipmentByIdShipment(shipment.ID);
if (existingShipment != null) {
if (existingShipment.ID.equals(idShipment)) {
database.updateLocalShipment(shipment);
database.updateLocalShipment(idShipment, shipment);
activityController.openListShipmentActivity();
} else {
Toast.makeText(getApplicationContext(),
errorController.getStringFromResourcesByName("insert_shipment_error"), Toast.LENGTH_LONG)
errorController.getStringFromResourcesByName("insert_shipment_error"), Toast.LENGTH_SHORT)
.show();
}
} else {
database.updateLocalShipment(shipment);
database.updateLocalShipment(idShipment, shipment);
activityController.openListShipmentActivity();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ private void getCurrentShipments() {
ShipmentService.getCurrentService(new GetCurrentShipment() {
@Override
public void onResponse(List<ShipmentModel> shipments) {
database.deleteAllShipments();
database.deleteAllNewShipments();
if (shipments != null) {
database.insertShipments(shipments);
} else {
no_shipments.setVisibility(View.VISIBLE);
}
afterSynchronization();
}
Expand Down Expand Up @@ -161,17 +159,21 @@ private void finishShipments() {
}

private void finishShipment(final ShipmentModel shipment) {
final String[] photosBeforePaths = TextUtils.split(shipment.photos_before, AppConfig.PHOTOS_DIVIDER);
final String[] photosAfterPaths = TextUtils.split(shipment.photos_after, AppConfig.PHOTOS_DIVIDER);
final String[] photosBeforePaths = shipment.photos_before.split(AppConfig.PHOTOS_DIVIDER);
final String[] photosAfterPaths = shipment.photos_after.split(AppConfig.PHOTOS_DIVIDER);
final ArrayList<Bitmap> beforeBitmaps = new ArrayList<Bitmap>();
final ArrayList<Bitmap> afterBitmaps = new ArrayList<Bitmap>();

for(Integer i = 0; i < photosBeforePaths.length; i++) {
beforeBitmaps.add(photoConverter.loadImageFromStorage(photosBeforePaths[i], i.toString()));
beforeBitmaps.add(photoConverter.loadImageFromStorage(photosBeforePaths[i], shipment.getDbId().toString() +
ShipmentModel.State.IN_PROGRESS.toString() +
i.toString()));
}

for(Integer i = 0; i < photosAfterPaths.length; i++) {
afterBitmaps.add(photoConverter.loadImageFromStorage(photosAfterPaths[i], i.toString()));
afterBitmaps.add(photoConverter.loadImageFromStorage(photosAfterPaths[i], shipment.getDbId().toString() +
ShipmentModel.State.DONE.toString() +
i.toString()));
}

ShipmentService.finishShipmentService(new FinishShipment() {
Expand All @@ -185,7 +187,7 @@ public void onResponse() {
@Override
public void onError(VolleyError message) {
counter++;
shipment.error_code = 5;
shipment.error_code = errorController.getErrorCodeFromResponse(message);
database.updateShipment(shipment);
getCurrentShipments();
}
Expand All @@ -210,10 +212,16 @@ public Map<String, String> getHeaders() {
}

private void showShipments() {
List<ShipmentModel> shipments = database.getAllShipments();
if (shipments.size() == 0) {
no_shipments.setVisibility(View.VISIBLE);
} else {
no_shipments.setVisibility(View.INVISIBLE);
}
rv_shipment.setClickable(true);

RecyclerView.Adapter adapter;
adapter = new RVAdapterShipment(database.getAllShipments(), this, activityController);
adapter = new RVAdapterShipment(shipments, this, activityController, errorController);
LinearLayoutManager llm = new LinearLayoutManager(this);

rv_shipment.setLayoutManager(llm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,17 @@ public void onClick(View v) {
}

private void setTextsAndPhotos() {
String[] photoPaths = TextUtils.split(shipment.photos_after, AppConfig.PHOTOS_DIVIDER);

shipment_from_vw.setText(shipment.address_from);
shipment_to_vw.setText(shipment.address_to);
shipment_unload_note_vw.setText(shipment.unload_note);
shipment_load_note_vw.setText(shipment.load_note);
shipment_price_vw.setText(shipment.price.toString());

for(Integer i = 0; i < photoPaths.length; i++) {
this.photosController.addPhoto(photoConverter.loadImageFromStorage(photoPaths[i], i.toString()));
}
}

private ShipmentModel getShipment() {
Bundle bundle = getIntent().getExtras();

return new ShipmentModel(bundle.getInt("id"),
ShipmentModel shipmentModel = new ShipmentModel(bundle.getInt("id"),
bundle.getString("address_from"),
bundle.getString("address_to"),
bundle.getString("load_note"),
Expand All @@ -137,6 +131,9 @@ private ShipmentModel getShipment() {
bundle.getString("photos_after"),
bundle.getInt("error_code"),
bundle.getInt("local") == 1);
shipmentModel.setDbId(bundle.getInt("db_id"));

return shipmentModel;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
Expand Down Expand Up @@ -170,7 +167,9 @@ private void finishShipment(ArrayList<Bitmap> bitmaps) {
shipment.state = ShipmentModel.State.DONE;
for(Integer i = 0; i < bitmaps.size(); i++) {
paths.add(photoConverter.saveImageToInternalStorage(bitmaps.get(i),
ShipmentModel.State.DONE.toString() + i.toString()));
shipment.getDbId().toString() +
ShipmentModel.State.DONE.toString() +
i.toString()));
}

shipment.photos_after = TextUtils.join(AppConfig.PHOTOS_DIVIDER, paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ private void setTextsAndPhotos() {

private ShipmentModel getShipment() {
Bundle bundle = getIntent().getExtras();

return new ShipmentModel(bundle.getInt("id"),
ShipmentModel shipmentModel = new ShipmentModel(bundle.getInt("id"),
bundle.getString("address_from"),
bundle.getString("address_to"),
bundle.getString("load_note"),
Expand All @@ -134,6 +133,9 @@ private ShipmentModel getShipment() {
bundle.getString("photos_after"),
bundle.getInt("error_code"),
bundle.getInt("local") == 1);
shipmentModel.setDbId(bundle.getInt("db_id"));

return shipmentModel;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
Expand Down Expand Up @@ -167,7 +169,9 @@ private void startShipment(ArrayList<Bitmap> bitmaps) {
shipment.state = ShipmentModel.State.IN_PROGRESS;
for(Integer i = 0; i < bitmaps.size(); i++) {
paths.add(photoConverter.saveImageToInternalStorage(bitmaps.get(i),
ShipmentModel.State.IN_PROGRESS.toString() + i.toString()));
shipment.getDbId().toString() +
ShipmentModel.State.IN_PROGRESS.toString() +
i.toString()));
}

shipment.photos_before = TextUtils.join(AppConfig.PHOTOS_DIVIDER, paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public static class UserData {
public static UserModel user;
}

public static String PHOTOS_DIVIDER = "|";
public static String PHOTOS_DIVIDER = "";
public static String CURRENT_LANGUAGE = "cs";
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void openInProgressShipmentActivity(ShipmentModel shipment) {
private Bundle createShipmentBundle(ShipmentModel shipment) {
Bundle b = new Bundle();

b.putInt("db_id", shipment.getDbId());
b.putInt("id", shipment.ID);
b.putString("address_from", shipment.address_from);
b.putString("address_to", shipment.address_to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,60 @@ public String getErrorKeyByCode(VolleyError response) {
return getStringFromResourcesByName("server_error_fatal");
}

public int getErrorCodeFromResponse(VolleyError response) {
if (response instanceof NoConnectionError) {
return 32;
}
if (response.getMessage() == null) {
return 0;
}
if (response.networkResponse != null) {
if (response.networkResponse.statusCode == 401) {
return 22;
}
}
try {
JSONObject jObj = new JSONObject(response.getMessage());
String code = jObj.getString("errorCode");
return Integer.parseInt(code);
} catch (JSONException e) {
// JSON error
e.printStackTrace();
if (!AppConfig.IS_PRODUCTION) {
Toast.makeText(this.context, "Json error: in ErrorController " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}

return 0;
}

public String getTextByErrorCode(Integer errorCode) {
String errorString = "server_error_";

if (isSupportedError(errorCode)) {
errorString += errorCode.toString();
} else {
return "";
}

return getStringFromResourcesByName(errorString);
}

private boolean isSupportedError(Integer errorCode) {
switch (errorCode) {
case 11:
case 12:
case 22:
case 30:
case 32:
case 35:
return true;
default:
return false;
}
}


private String alternativeMessage() {
return "server_error_fatal";
}
Expand Down
Loading

0 comments on commit 7012249

Please sign in to comment.