Skip to content

Commit

Permalink
-do aplikace se muže prihlasit vice driveru
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Meravý committed Aug 27, 2018
1 parent a072d86 commit 55e7dc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
import java.util.List;
import nohe.nohe_android.activity.database.model.Shipment;
import nohe.nohe_android.activity.models.ShipmentModel;
import nohe.nohe_android.activity.services.LoginService;

public class DatabaseHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 6;
private static final int DATABASE_VERSION = 7;

// Database Name
private static final String DATABASE_NAME = "shipments_db";

private LoginService loginService;


public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
loginService = new LoginService(context.getApplicationContext());
}

// Creating Tables
Expand Down Expand Up @@ -136,13 +140,14 @@ public ShipmentModel getShipmentByIdShipment(long id) {

public List<ShipmentModel> getAllShipments() {
List<ShipmentModel> shipments = new ArrayList<>();
Integer driver = loginService.getUserId();

// Select All Query
String selectQuery = "SELECT * FROM " + Shipment.TABLE_NAME + " ORDER BY " +
String selectQuery = "SELECT * FROM " + Shipment.TABLE_NAME + " WHERE driver = ? ORDER BY " +
Shipment.COLUMN_ID + " DESC";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
Cursor cursor = db.rawQuery(selectQuery, new String[] {driver.toString()});

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
Expand All @@ -160,11 +165,11 @@ public List<ShipmentModel> getAllShipments() {

public List<ShipmentModel> getFinishedShipments() {
List<ShipmentModel> shipments = new ArrayList<>();

Integer driver = loginService.getUserId();

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + Shipment.TABLE_NAME + " WHERE state = ?",
new String[] {String.valueOf(ShipmentModel.State.DONE.getValue())});
Cursor cursor = db.rawQuery("SELECT * FROM " + Shipment.TABLE_NAME + " WHERE state = ? and driver = ?",
new String[] {String.valueOf(ShipmentModel.State.DONE.getValue()), driver.toString()});

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
Expand Down Expand Up @@ -245,9 +250,10 @@ public void deleteShipment(ShipmentModel shipment) {

public void deleteAllNewShipments() {
SQLiteDatabase db = this.getWritableDatabase();
Integer driver = loginService.getUserId();

db.delete(Shipment.TABLE_NAME, Shipment.COLUMN_STATE + " = ?",
new String[]{String.valueOf(ShipmentModel.State.NEW.getValue())});
db.delete(Shipment.TABLE_NAME, Shipment.COLUMN_STATE + " = ? and driver = ?",
new String[]{String.valueOf(ShipmentModel.State.NEW.getValue()), driver.toString()});
db.close();
}

Expand All @@ -271,6 +277,8 @@ private ShipmentModel getShipmentModelFromDB(Cursor cursor) {

private ContentValues setShipmentValues(ShipmentModel shipment) {
ContentValues values = new ContentValues();
Integer driver = loginService.getUserId();

if (shipment.state == null) {
shipment.state = ShipmentModel.State.NEW;
}
Expand All @@ -286,6 +294,7 @@ private ContentValues setShipmentValues(ShipmentModel shipment) {
values.put(Shipment.COLUMN_PHOTOS_AFTER, shipment.photos_after);
values.put(Shipment.COLUMN_ERROR_CODE, shipment.error_code);
values.put(Shipment.COLUMN_LOCAL, shipment.local ? 1 : 0);
values.put(Shipment.COLUMN_DRIVER, driver.toString());

return values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Shipment {
public static final String COLUMN_PHOTOS_AFTER = "photos_after";
public static final String COLUMN_ERROR_CODE = "error_code";
public static final String COLUMN_LOCAL = "local";
public static final String COLUMN_DRIVER = "driver";

private int id;
private int id_shipment;
Expand All @@ -30,6 +31,7 @@ public class Shipment {
private String photos_after;
private int error_code;
private boolean local;
private int driver;


// Create table SQL query
Expand All @@ -46,7 +48,8 @@ public class Shipment {
+ COLUMN_PHOTOS_BEFORE + " TEXT,"
+ COLUMN_PHOTOS_AFTER + " TEXT,"
+ COLUMN_ERROR_CODE + " INTEGER,"
+ COLUMN_LOCAL + " INTEGER"
+ COLUMN_LOCAL + " INTEGER,"
+ COLUMN_DRIVER + " INTEGER"
+ ")";

public Shipment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class LoginService {
private static final String PREF_NAME = "login_pref";
private static final String IS_LOGGED_IN = "isLoggedIn";
private static final String TOKEN = "Token";
private static final String USER_ID = "user_id";
private static final String USERNAME = "UserName";
private static final String USER_FIRST_NAME = "UserName";
private static final String USER_LAST_NAME = "UserSurname";
Expand All @@ -32,6 +33,7 @@ public void login(String token, UserModel user) {
editor.putBoolean(IS_LOGGED_IN, true);
editor.putString(TOKEN, token);
editor.putString(USERNAME, user.username);
editor.putInt(USER_ID, user.ID);
editor.putString(USER_FIRST_NAME, user.name);
editor.putString(USER_LAST_NAME, user.surname);
editor.putInt(USER_ROLE, user.role);
Expand All @@ -42,6 +44,7 @@ public void login(String token, UserModel user) {
public void logout() {
editor.putBoolean(IS_LOGGED_IN, false);
editor.putString(TOKEN, "");
editor.putInt(USER_ID, 0);
editor.putString(USERNAME, "");
editor.putString(USER_FIRST_NAME, "");
editor.putString(USER_LAST_NAME, "");
Expand Down Expand Up @@ -73,4 +76,8 @@ public String getUserFirstName() {
public String getUserSurname() {
return pref.getString(USER_LAST_NAME, "");
}

public int getUserId() {
return pref.getInt(USER_ID, 0);
}
}

0 comments on commit 55e7dc4

Please sign in to comment.