Initial commit

This commit is contained in:
2017-06-05 21:03:23 +01:00
commit c0f912d320
43 changed files with 1882 additions and 0 deletions

1
app/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

30
app/build.gradle Normal file
View File

@@ -0,0 +1,30 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.h_mal.inventoryapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:design:25.3.0'
testCompile 'junit:junit:4.12'
}

17
app/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\h_mal\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View File

@@ -0,0 +1,26 @@
package com.example.h_mal.inventoryapp;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.h_mal.inventoryapp", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.h_mal.inventoryapp">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddProduct"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<provider
android:name=".data.ProductProvider"
android:authorities="com.example.h_mal.inventoryapp"
android:exported="false" />
</application>
</manifest>

View File

@@ -0,0 +1,380 @@
package com.example.h_mal.inventoryapp;
import android.Manifest;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NavUtils;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.h_mal.inventoryapp.data.ProductContract.ProductEntry;
public class AddProduct extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor>{
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
private static final int EXISTING_PRODUCT_LOADER = 0;
private Uri mCurrentProductUri;
private EditText mNameEditText;
private EditText mQuantityEditText;
private EditText mPriceEditText;
private ImageView mImageView;
private Uri mImageURI;
private static final int RESULT_LOAD_IMAGE = 1;
private boolean mProductHasChanged = false;
private static final int PICK_IMAGE_REQUEST = 0;
private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mProductHasChanged = true;
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
mNameEditText = (EditText) findViewById(R.id.productName);
mQuantityEditText = (EditText) findViewById(R.id.productQuantity);
mPriceEditText = (EditText) findViewById(R.id.productPrice);
mImageView = (ImageView) findViewById(R.id.imageView2);
Intent intent = getIntent();
mCurrentProductUri = intent.getData();
if (mCurrentProductUri == null) {
setTitle(getString(R.string.add_product_title));
invalidateOptionsMenu();
} else {
setTitle(getString(R.string.edit_product_title));
getLoaderManager().initLoader(EXISTING_PRODUCT_LOADER, null, this);
}
Button Browse = (Button) findViewById(R.id.BrowseImage);
Browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tryToOpenImageSelector();
}
});
Button SubmitProduct = (Button) findViewById(R.id.submit);
SubmitProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveProduct();
}
});
ImageButton ContactSupplier = (ImageButton) findViewById(R.id.ContactSupplier);
ContactSupplier.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Order Stock " + mNameEditText.getText().toString().trim());
intent.putExtra(Intent.EXTRA_TEXT, "I would like to order more " + mNameEditText.getText().toString().trim());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
Button Incrament = (Button) findViewById(R.id.buttonAdd);
Incrament.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IncramentQuantity();
}
});
Button decrament = (Button) findViewById(R.id.buttonMinus);
decrament.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DecramentQuantity();
}
});
mNameEditText.setOnTouchListener(mTouchListener);
mQuantityEditText.setOnTouchListener(mTouchListener);
mPriceEditText.setOnTouchListener(mTouchListener);
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setTitle("Discard changes?")
.setMessage("Are you sure you want to discard changes?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
AddProduct.super.onBackPressed();
}
}).create().show();
}
private void saveProduct() {
String nameString = mNameEditText.getText().toString().trim();
String quantityString = mQuantityEditText.getText().toString().trim();
String priceString = mPriceEditText.getText().toString().trim();
if (
TextUtils.isEmpty(nameString) || TextUtils.isEmpty(quantityString) ||
TextUtils.isEmpty(priceString) || mImageURI == null) {
Toast.makeText(AddProduct.this, "please insert all product data", Toast.LENGTH_SHORT).show();
return;
}
int quantity = Integer.parseInt(quantityString);
int price = Integer.parseInt(priceString);
ContentValues values = new ContentValues();
values.put(ProductEntry.COLUMN_PRODUCT_NAME, nameString);
values.put(ProductEntry.COLUMN_PRODUCT_PRICE, price);
values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, quantity);
values.put(ProductEntry.COLUMN_PRODUCT_IMAGE, String.valueOf(mImageURI));
if (mCurrentProductUri == null) {
Uri newUri = getContentResolver().insert(ProductEntry.CONTENT_URI, values);
if (newUri == null) {
Toast.makeText(this, getString(R.string.insert_product_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.insert_product_successful),
Toast.LENGTH_SHORT).show();
}
} else {
int rowsAffected = getContentResolver().update(mCurrentProductUri, values, null, null);
if (rowsAffected == 0) {
Toast.makeText(this, getString(R.string.update_product_failed),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, getString(R.string.update_product_successful),
Toast.LENGTH_SHORT).show();
}
}
NavUtils.navigateUpFromSameTask(AddProduct.this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_editor, menu);
return true;
}
public void tryToOpenImageSelector() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
return;
}
openImageSelector();
}
private void openImageSelector() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openImageSelector();
}
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
DeleteDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void DeleteDialog() {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete");
builder.setPositiveButton("delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
deleteProduct();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
}
}
});
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void deleteProduct() {
if (mCurrentProductUri != null) {
int rowsDeleted = getContentResolver().delete(mCurrentProductUri, null, null);
if (rowsDeleted == 0) {
Toast.makeText(this, "error deleting product", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Product deleted", Toast.LENGTH_SHORT).show();
}
}
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
mImageView.setImageURI(selectedImage);
mImageURI = Uri.parse(selectedImage.toString());
}
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {
ProductEntry._ID,
ProductEntry.COLUMN_PRODUCT_IMAGE,
ProductEntry.COLUMN_PRODUCT_NAME,
ProductEntry.COLUMN_PRODUCT_QUANTITY,
ProductEntry.COLUMN_PRODUCT_PRICE };
return new CursorLoader(this,
mCurrentProductUri,
projection,
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
if (cursor == null || cursor.getCount() < 1) {
return;
}
if (cursor.moveToFirst()) {
int nameColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_NAME);
int quantityColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_QUANTITY);
int priceColumnIndex = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_PRICE);
int selectedImage = cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_IMAGE);
String name = cursor.getString(nameColumnIndex);
int quantity = cursor.getInt(quantityColumnIndex);
int price = cursor.getInt(priceColumnIndex);
String image = cursor.getString(selectedImage);
mNameEditText.setText(name);
mQuantityEditText.setText(Integer.toString(quantity));
mPriceEditText.setText(Integer.toString(price));
mImageView.setImageURI(Uri.parse(image));
mImageURI = Uri.parse(image);
}
}
private void IncramentQuantity() {
String quantityString = mQuantityEditText.getText().toString();
int initialQuantity;
if (quantityString.isEmpty()) {
initialQuantity = 0;
} else {
initialQuantity = Integer.parseInt(quantityString);
}
mQuantityEditText.setText(String.valueOf(initialQuantity + 1));
}
private void DecramentQuantity() {
String quantityString = mQuantityEditText.getText().toString();
int initialQuantity;
if (quantityString.isEmpty()) {
return;
} else if (quantityString.equals("0")) {
return;
} else {
initialQuantity = Integer.parseInt(quantityString);
mQuantityEditText.setText(String.valueOf(initialQuantity - 1));
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}

View File

@@ -0,0 +1,136 @@
package com.example.h_mal.inventoryapp;
import android.app.LoaderManager;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.example.h_mal.inventoryapp.data.ProductContract.ProductEntry;
import com.example.h_mal.inventoryapp.data.ProductDbHelper;
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor>{
private static final int PRODUCT_LOADER = 0;
ProductCursorAdapter mCursorAdapter;
ProductDbHelper productDbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddProduct.class);
startActivity(intent);
}
});
ListView productListView = (ListView) findViewById(R.id.list_item_view);
View emptyView = findViewById(R.id.empty_view);
productListView.setEmptyView(emptyView);
mCursorAdapter = new ProductCursorAdapter(this, null);
productListView.setAdapter(mCursorAdapter);
getLoaderManager().initLoader(PRODUCT_LOADER, null, this);
}
private void insertProduct() {
ContentValues values = new ContentValues();
values.put(ProductEntry.COLUMN_PRODUCT_NAME, "demoItem");
values.put(ProductEntry.COLUMN_PRODUCT_PRICE, 8);
values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, 4);
Uri SadImageuri = Uri.parse("android.resource://com.example.h_mal.inventoryapp/drawable/sad");
values.put(ProductEntry.COLUMN_PRODUCT_IMAGE, String.valueOf(SadImageuri));
Uri newUri = getContentResolver().insert(ProductEntry.CONTENT_URI, values);
}
private void deleteAllProducts() {
int rowsDeleted = getContentResolver().delete(ProductEntry.CONTENT_URI, null, null);
Log.v("MainActivity", rowsDeleted + " rows deleted");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void clickOnViewItem(long id) {
Intent intent = new Intent(MainActivity.this, AddProduct.class);
Uri currentProductUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI, id);
intent.setData(currentProductUri);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
deleteAllProducts();
return true;
case R.id.insert_placeholder_data:
insertProduct();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Define a projection that specifies the columns from the table we care about.
String[] projection = {
ProductEntry._ID,
ProductEntry.COLUMN_PRODUCT_IMAGE,
ProductEntry.COLUMN_PRODUCT_NAME,
ProductEntry.COLUMN_PRODUCT_QUANTITY,
ProductEntry.COLUMN_PRODUCT_PRICE};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this, // Parent activity context
ProductEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mCursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}

View File

@@ -0,0 +1,98 @@
package com.example.h_mal.inventoryapp;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.h_mal.inventoryapp.data.ProductContract.ProductEntry;
/**
* Created by h_mal on 04/04/2017.
*/
public class ProductCursorAdapter extends CursorAdapter {
private final MainActivity activity;
private Context mContext;
public ProductCursorAdapter(MainActivity context, Cursor c) {
super(context, c, 0);
this.activity = context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
mContext = context;
ImageView picImageView = (ImageView) view.findViewById(R.id.imageView);
TextView nameTextView = (TextView) view.findViewById(R.id.Product);
TextView priceTextView = (TextView) view.findViewById(R.id.price);
TextView quantityTextView = (TextView) view.findViewById(R.id.Qn);
Button sellButton = (Button) view.findViewById(R.id.button);
final String nameColumnIndex = cursor.getString(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_PRODUCT_NAME));
final Integer priceColumnIndex = cursor.getInt(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_PRODUCT_PRICE));
final Integer quantityColumnIndex = cursor.getInt(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_PRODUCT_QUANTITY));
final String imageColumnIndex = cursor.getString(cursor.getColumnIndexOrThrow(ProductEntry.COLUMN_PRODUCT_IMAGE));
nameTextView.setText(nameColumnIndex);
priceTextView.setText(Integer.toString(priceColumnIndex));
quantityTextView.setText(String.valueOf(quantityColumnIndex));
// picImageView.setImageURI(Uri.parse(cursor.getString(cursor.getColumnIndex(ProductEntry.COLUMN_PRODUCT_IMAGE))));
if (imageColumnIndex == null) {
picImageView.setVisibility(View.GONE);
}else {
picImageView.setVisibility(View.VISIBLE);
picImageView.setImageURI(Uri.parse(imageColumnIndex));
}
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(ProductEntry._ID));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
activity.clickOnViewItem(id);
}
});
sellButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view != null) {
Object obj = view.getTag();
String st = obj.toString();
ContentValues values = new ContentValues();
values.put(ProductEntry.COLUMN_PRODUCT_NAME, nameColumnIndex);
values.put(ProductEntry.COLUMN_PRODUCT_IMAGE, imageColumnIndex);
values.put(ProductEntry.COLUMN_PRODUCT_QUANTITY, quantityColumnIndex >= 1? quantityColumnIndex-1: 0);
values.put(ProductEntry.COLUMN_PRODUCT_PRICE, priceColumnIndex);
Uri currentPetUri = ContentUris.withAppendedId(ProductEntry.CONTENT_URI, Integer.parseInt(st));
int rowsAffected = mContext.getContentResolver().update(currentPetUri, values, null, null);
}
}
});
Object obj = cursor.getInt(cursor.getColumnIndex(ProductEntry._ID));
sellButton.setTag(obj);
}
}

View File

@@ -0,0 +1,47 @@
package com.example.h_mal.inventoryapp.data;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.BaseColumns;
/**
* Created by h_mal on 04/04/2017.
*/
public class ProductContract {
private ProductContract() {}
public static final String CONTENT_AUTHORITY = "com.example.h_mal.inventoryapp";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_PRODUCTS = "products";
public static final class ProductEntry implements BaseColumns {
public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_PRODUCTS);
public static final String CONTENT_LIST_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PRODUCTS;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_PRODUCTS;
public final static String TABLE_NAME = "products";
public final static String _ID = BaseColumns._ID;
public final static String COLUMN_PRODUCT_IMAGE = "image";
public final static String COLUMN_PRODUCT_NAME ="name";
public final static String COLUMN_PRODUCT_QUANTITY = "quantity";
public final static String COLUMN_PRODUCT_PRICE = "price";
}
}

View File

@@ -0,0 +1,45 @@
package com.example.h_mal.inventoryapp.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.h_mal.inventoryapp.data.ProductContract.ProductEntry;
/**
* Created by h_mal on 04/04/2017.
*/
public class ProductDbHelper extends SQLiteOpenHelper{
public static final String LOG_TAG = ProductDbHelper.class.getSimpleName();
private static final String DATABASE_NAME = "inventory.db";
private static final int DATABASE_VERSION = 1;
public ProductDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_PRODUCTS_TABLE = "CREATE TABLE " + ProductEntry.TABLE_NAME + " ("
+ ProductEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ProductEntry.COLUMN_PRODUCT_IMAGE + " TEXT, "
+ ProductEntry.COLUMN_PRODUCT_NAME + " TEXT NOT NULL, "
+ ProductEntry.COLUMN_PRODUCT_QUANTITY + " INTEGER NOT NULL DEFAULT 0, "
+ ProductEntry.COLUMN_PRODUCT_PRICE + " INTEGER NOT NULL DEFAULT 0)";
db.execSQL(SQL_CREATE_PRODUCTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

View File

@@ -0,0 +1,214 @@
package com.example.h_mal.inventoryapp.data;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import com.example.h_mal.inventoryapp.data.ProductContract.ProductEntry;
public class ProductProvider extends ContentProvider {
public static final String LOG_TAG = ProductProvider.class.getSimpleName();
private static final int PRODUCTS = 100;
private static final int PRODUCT_ID = 101;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI(ProductContract.CONTENT_AUTHORITY, ProductContract.PATH_PRODUCTS, PRODUCTS);
sUriMatcher.addURI(ProductContract.CONTENT_AUTHORITY, ProductContract.PATH_PRODUCTS + "/#", PRODUCT_ID);
}
ProductDbHelper mDbHelper;
@Override
public boolean onCreate() {
mDbHelper = new ProductDbHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
SQLiteDatabase database = mDbHelper.getReadableDatabase();
Cursor cursor;
int match = sUriMatcher.match(uri);
switch (match) {
case PRODUCTS:
cursor = database.query(ProductEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
case PRODUCT_ID:
selection = ProductEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
cursor = database.query(ProductEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Cannot query " + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
final int match = sUriMatcher.match(uri);
switch (match) {
case PRODUCTS:
return insertProduct(uri, contentValues);
default:
throw new IllegalArgumentException("Insertion is not supported for " + uri);
}
}
private Uri insertProduct(Uri uri, ContentValues values) {
String name = values.getAsString(ProductEntry.COLUMN_PRODUCT_NAME);
if (name == null) {
throw new IllegalArgumentException("name required");
}
Integer quantity = values.getAsInteger(ProductEntry.COLUMN_PRODUCT_QUANTITY);
if (quantity != null && quantity < 0) {
throw new IllegalArgumentException("quantity required");
}
Integer price = values.getAsInteger(ProductEntry.COLUMN_PRODUCT_PRICE);
if (price != null && price < 0) {
throw new IllegalArgumentException("Price required");
}
String image = values.getAsString(ProductEntry.COLUMN_PRODUCT_IMAGE);
if (image == null) {
throw new IllegalArgumentException("image required");
}
SQLiteDatabase database = mDbHelper.getWritableDatabase();
long id = database.insert(ProductEntry.TABLE_NAME, null, values);
if (id == -1) {
Log.e(LOG_TAG, "row failed " + uri);
return null;
}
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
@Override
public int update(Uri uri, ContentValues contentValues, String selection,
String[] selectionArgs) {
final int match = sUriMatcher.match(uri);
switch (match) {
case PRODUCTS:
return updateProduct(uri, contentValues, selection, selectionArgs);
case PRODUCT_ID:
selection = ProductEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
return updateProduct(uri, contentValues, selection, selectionArgs);
default:
throw new IllegalArgumentException("Update is not supported for " + uri);
}
}
private int updateProduct(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
if (values.containsKey(ProductEntry.COLUMN_PRODUCT_NAME)) {
String name = values.getAsString(ProductEntry.COLUMN_PRODUCT_NAME);
if (name == null) {
throw new IllegalArgumentException("Name required");
}
}
if (values.containsKey(ProductEntry.COLUMN_PRODUCT_QUANTITY)) {
Integer quantity = values.getAsInteger(ProductEntry.COLUMN_PRODUCT_QUANTITY);
if (quantity != null && quantity < 0) {
throw new IllegalArgumentException("quantity required");
}
}
if (values.containsKey(ProductEntry.COLUMN_PRODUCT_PRICE)) {
Integer price = values.getAsInteger(ProductEntry.COLUMN_PRODUCT_PRICE);
if (price != null && price < 0) {
throw new IllegalArgumentException("Price required");
}
}
if (values.size() == 0) {
return 0;
}
SQLiteDatabase database = mDbHelper.getWritableDatabase();
int rowsUpdated = database.update(ProductEntry.TABLE_NAME, values, selection, selectionArgs);
if (rowsUpdated != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsUpdated;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase database = mDbHelper.getWritableDatabase();
int rowsDeleted;
final int match = sUriMatcher.match(uri);
switch (match) {
case PRODUCTS:
rowsDeleted = database.delete(ProductEntry.TABLE_NAME, selection, selectionArgs);
break;
case PRODUCT_ID:
selection = ProductEntry._ID + "=?";
selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
rowsDeleted = database.delete(ProductEntry.TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Deletion is not supported for " + uri);
}
if (rowsDeleted != 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return rowsDeleted;
}
@Override
public String getType(Uri uri) {
final int match = sUriMatcher.match(uri);
switch (match) {
case PRODUCTS:
return ProductEntry.CONTENT_LIST_TYPE;
case PRODUCT_ID:
return ProductEntry.CONTENT_ITEM_TYPE;
default:
throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View File

@@ -0,0 +1,168 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_add_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.h_mal.inventoryapp.AddProduct"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
app:srcCompat="@mipmap/ic_launcher" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_horizontal_margin">
<TextView
android:text="Image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.AppCompat" />
<Button
android:text="Browse"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/BrowseImage"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_horizontal_margin">
<TextView
android:text="Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.AppCompat" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Product name"
android:ems="10"
android:id="@+id/productName"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_horizontal_margin">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="quantity"
android:textAppearance="@style/TextAppearance.AppCompat" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<Button
android:id="@+id/buttonMinus"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<EditText
android:id="@+id/productQuantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:ems="10"
android:hint="Quantity"
android:inputType="number" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_horizontal_margin">
<TextView
android:text="Price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.AppCompat" />
<TextView
android:text="£"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textView8"
android:layout_weight="1"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Price"
android:ems="10"
android:layout_weight="4"
android:id="@+id/productPrice" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_dialog_email"
android:id="@+id/ContactSupplier"
android:layout_weight="1" />
<Button
android:text="submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/submit"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_item_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<RelativeLayout
android:id="@+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/empty_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="Inventory list empty"
android:textAppearance="?android:textAppearanceMedium"/>
<TextView
android:id="@+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/empty_title_text"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="add products to begin"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@android:drawable/ic_menu_add" />
</RelativeLayout>

View File

@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="112sp"
android:padding="@dimen/activity_horizontal_margin">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitCenter"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/imageView" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/Product"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_weight="1"
android:gravity="center"
android:text="Product Name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price: £ " />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00.00" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Sell" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity: " />
<TextView
android:id="@+id/Qn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qn" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Options menu for the EditorActivity -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".EditorActivity">
<item
android:id="@+id/action_delete"
android:title="@string/delete_item"
app:showAsAction="never" />
</menu>

View File

@@ -0,0 +1,15 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.h_mal.inventoryapp.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="delete all data"
app:showAsAction="never" />
<item
android:id="@+id/insert_placeholder_data"
android:orderInCategory="100"
android:title="insert test data"
app:showAsAction="never" />
</menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -0,0 +1,9 @@
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>

View File

@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>

View File

@@ -0,0 +1,6 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>

View File

@@ -0,0 +1,12 @@
<resources>
<string name="app_name">Inventory app</string>
<string name="action_settings">Settings</string>
<string name="add_product_title">Add Product</string>
<string name="edit_product_title">Edit product</string>
<string name="insert_product_failed">Inserting product failed</string>
<string name="insert_product_successful">Product added</string>
<string name="update_product_failed">Product update failed</string>
<string name="update_product_successful">Product updated successful</string>
<string name="save_item">Save product</string>
<string name="delete_item">Delete product</string>
</resources>

View File

@@ -0,0 +1,20 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

View File

@@ -0,0 +1,17 @@
package com.example.h_mal.inventoryapp;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}