commit c0f912d3202a3ef4194eaad38b46808c783210ba Author: H Malik Date: Mon Jun 5 21:03:23 2017 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..7ac24c7 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5d19981 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..16a28ec --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..189d74c --- /dev/null +++ b/app/build.gradle @@ -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' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..05e856b --- /dev/null +++ b/app/proguard-rules.pro @@ -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 *; +#} diff --git a/app/src/androidTest/java/com/example/h_mal/inventoryapp/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/h_mal/inventoryapp/ExampleInstrumentedTest.java new file mode 100644 index 0000000..787e363 --- /dev/null +++ b/app/src/androidTest/java/com/example/h_mal/inventoryapp/ExampleInstrumentedTest.java @@ -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 Testing documentation + */ +@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()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..70edb27 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/h_mal/inventoryapp/AddProduct.java b/app/src/main/java/com/example/h_mal/inventoryapp/AddProduct.java new file mode 100644 index 0000000..384e746 --- /dev/null +++ b/app/src/main/java/com/example/h_mal/inventoryapp/AddProduct.java @@ -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{ + + 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 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 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 loader) { + + } +} diff --git a/app/src/main/java/com/example/h_mal/inventoryapp/MainActivity.java b/app/src/main/java/com/example/h_mal/inventoryapp/MainActivity.java new file mode 100644 index 0000000..addfeda --- /dev/null +++ b/app/src/main/java/com/example/h_mal/inventoryapp/MainActivity.java @@ -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{ + + 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 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 loader, Cursor data) { + mCursorAdapter.swapCursor(data); + } + + @Override + public void onLoaderReset(Loader loader) { + mCursorAdapter.swapCursor(null); + } + + +} diff --git a/app/src/main/java/com/example/h_mal/inventoryapp/ProductCursorAdapter.java b/app/src/main/java/com/example/h_mal/inventoryapp/ProductCursorAdapter.java new file mode 100644 index 0000000..7a19d1e --- /dev/null +++ b/app/src/main/java/com/example/h_mal/inventoryapp/ProductCursorAdapter.java @@ -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); + + } +} diff --git a/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductContract.java b/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductContract.java new file mode 100644 index 0000000..125c029 --- /dev/null +++ b/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductContract.java @@ -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"; + + + } +} diff --git a/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductDbHelper.java b/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductDbHelper.java new file mode 100644 index 0000000..70d8adf --- /dev/null +++ b/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductDbHelper.java @@ -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) { + + } +} diff --git a/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductProvider.java b/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductProvider.java new file mode 100644 index 0000000..6cfa7a4 --- /dev/null +++ b/app/src/main/java/com/example/h_mal/inventoryapp/data/ProductProvider.java @@ -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); + } + } + + +} \ No newline at end of file diff --git a/app/src/main/res/drawable/sad.jpg b/app/src/main/res/drawable/sad.jpg new file mode 100644 index 0000000..90dde20 Binary files /dev/null and b/app/src/main/res/drawable/sad.jpg differ diff --git a/app/src/main/res/layout/activity_add_product.xml b/app/src/main/res/layout/activity_add_product.xml new file mode 100644 index 0000000..94eeef0 --- /dev/null +++ b/app/src/main/res/layout/activity_add_product.xml @@ -0,0 +1,168 @@ + + + + + + + + + + +