diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml
index b600225..0f9fbd3 100644
--- a/.idea/assetWizardSettings.xml
+++ b/.idea/assetWizardSettings.xml
@@ -147,7 +147,7 @@
@@ -157,8 +157,7 @@
diff --git a/.idea/caches/gradle_models.ser b/.idea/caches/gradle_models.ser
index 7b67983..71bfa86 100644
Binary files a/.idea/caches/gradle_models.ser and b/.idea/caches/gradle_models.ser differ
diff --git a/app/build.gradle b/app/build.gradle
index dffabc9..0c63f7e 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -6,8 +6,8 @@ android {
applicationId "ru.volgorobot.vrcatalog"
minSdkVersion 19
targetSdkVersion 27
- versionCode 5
- versionName "0.3.1"
+ versionCode 7
+ versionName "0.5alpha"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
@@ -35,4 +35,4 @@ dependencies {
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.github.chrisbanes:PhotoView:2.1.4'
-}
\ No newline at end of file
+}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index ac11670..371f056 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -1,6 +1,5 @@
-
@@ -12,6 +11,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
+
+ android:theme="@style/AppTheme.Fullscreen" />
+ android:name=".view.ItemActivity"
+ android:theme="@style/ItemActivityTheme" />
+ android:parentActivityName=".view.MainActivity">
+
-
+
-
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java
index 62247c0..991ebef 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java
@@ -27,12 +27,21 @@ public interface MainContract {
CoreModel getCoreModel();
}
- interface ViewBinderPresenter {
+ interface LevelLoaderPresenter {
void fetchSecondLevel(FirstLevelModel firstLevelModel, TreeNode treeNode);
void fetchThirdLevel(SecondLevelModel secondLevelModel, TreeNode treeNode);
void fetchDetail(ThirdLevelModel thirdLevelModel);
}
+ interface FavoritesPresenter {
+ void fetchItem(int itemID);
+ }
+
+ interface FavoritesActivityView {
+ void onFailruleAnswer(int errorCode);
+ void startActivityByIntent(Intent intent);
+ }
+
interface ViewBinder {
void rotateImageView();
}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/DatabaseProperties.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/DatabaseProperties.java
new file mode 100644
index 0000000..b5165df
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/DatabaseProperties.java
@@ -0,0 +1,11 @@
+package ru.volgorobot.vrcatalog.additional;
+
+public class DatabaseProperties {
+ public final static String DATABASE_NAME = "database";
+ public final static String FAVORITES_TABLE_NAME = "favorites";
+ public final static int DATABASE_VERSION = 1;
+
+ public final static String FAVORITES_COLUMN_ID = "_id";
+ public final static String FAVORITES_COLUMN_ITEM_ID = "itemID";
+ public final static String FAVORITES_COLUMN_ITEM_NAME = "itemName";
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/SqliteHelper.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/SqliteHelper.java
new file mode 100644
index 0000000..02148f2
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/SqliteHelper.java
@@ -0,0 +1,27 @@
+package ru.volgorobot.vrcatalog.additional;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+
+public class SqliteHelper extends SQLiteOpenHelper {
+ public SqliteHelper(Context context) {
+ super(context, DatabaseProperties.DATABASE_NAME, null, DatabaseProperties.DATABASE_VERSION);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase sqLiteDatabase) {
+ String query = "CREATE TABLE " +
+ DatabaseProperties.FAVORITES_TABLE_NAME + "(" +
+ DatabaseProperties.FAVORITES_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
+ DatabaseProperties.FAVORITES_COLUMN_ITEM_ID + " INTEGER," +
+ DatabaseProperties.FAVORITES_COLUMN_ITEM_NAME + " TEXT);";
+ sqLiteDatabase.execSQL(query);
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
+ sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DatabaseProperties.FAVORITES_TABLE_NAME);
+ onCreate(sqLiteDatabase);
+ }
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/FavoritesListAdapter.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/FavoritesListAdapter.java
new file mode 100644
index 0000000..49395d5
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/FavoritesListAdapter.java
@@ -0,0 +1,23 @@
+package ru.volgorobot.vrcatalog.additional.adapters;
+
+import android.content.Context;
+import android.widget.ArrayAdapter;
+
+import java.util.List;
+
+import ru.volgorobot.vrcatalog.model.FavoritesListModel;
+import ru.volgorobot.vrcatalog.presenters.FavoritesDBPresenter;
+
+public class FavoritesListAdapter extends ArrayAdapter {
+ private FavoritesDBPresenter db;
+
+ public FavoritesListAdapter(Context context, int resource, List objects) {
+ super(context, resource, objects);
+ db = new FavoritesDBPresenter(context);
+ }
+
+ public void updateData() {
+ super.clear();
+ super.addAll(db.selectAll());
+ }
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/ImagePagerAdapter.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/ImagePagerAdapter.java
index 506f8eb..2f182f9 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/ImagePagerAdapter.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/adapters/ImagePagerAdapter.java
@@ -13,16 +13,14 @@ import android.widget.ImageView;
import com.squareup.picasso.Picasso;
-import java.io.FileNotFoundException;
import java.io.IOException;
import ru.volgorobot.vrcatalog.R;
-import ru.volgorobot.vrcatalog.view.DetailActivity;
import ru.volgorobot.vrcatalog.view.FullScreenImageActivity;
public class ImagePagerAdapter extends PagerAdapter {
/**
- * This class is designed to control the ViewPager (images of item) in the activity of viewing details of items (DetailActivity).
+ * This class is designed to control the ViewPager (images of item) in the activity of viewing details of items (ItemActivity).
*/
private Context context;
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/FavoritesListModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/FavoritesListModel.java
new file mode 100644
index 0000000..75cfb7b
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/FavoritesListModel.java
@@ -0,0 +1,35 @@
+package ru.volgorobot.vrcatalog.model;
+
+public class FavoritesListModel {
+ private int id;
+ private int itemID;
+ private String itemName;
+
+ public FavoritesListModel(int itemID, String itemName) {
+ this.itemID = itemID;
+ this.itemName = itemName;
+ }
+
+ public FavoritesListModel(int id, int itemID, String itemName) {
+ this.id = id;
+ this.itemID = itemID;
+ this.itemName = itemName;
+ }
+
+ public int getItemID() {
+ return itemID;
+ }
+
+ public String getItemName() {
+ return itemName;
+ }
+
+ public int getID() {
+ return id;
+ }
+
+ @Override
+ public String toString() {
+ return getItemName();
+ }
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java
index c42b182..9dacb60 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java
@@ -15,7 +15,7 @@ import ru.volgorobot.vrcatalog.model.SecondLevelModel;
public class SecondLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
TextView textView;
ImageView imageView;
- MainContract.ViewBinderPresenter viewBinderPresenter;
+ MainContract.LevelLoaderPresenter viewBinderPresenter;
public SecondLevelNodeViewBinder(View itemView, MainContract.MainActivityView mView, Context context) {
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java
index 036fac0..f18c3e6 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java
@@ -13,7 +13,7 @@ import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
TextView textView;
- MainContract.ViewBinderPresenter viewBinderPresenter;
+ MainContract.LevelLoaderPresenter viewBinderPresenter;
public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) {
super(itemView);
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/presenters/FavoritesDBPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/presenters/FavoritesDBPresenter.java
new file mode 100644
index 0000000..ba5e70c
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/presenters/FavoritesDBPresenter.java
@@ -0,0 +1,83 @@
+package ru.volgorobot.vrcatalog.presenters;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+
+import java.util.ArrayList;
+
+import ru.volgorobot.vrcatalog.additional.DatabaseProperties;
+import ru.volgorobot.vrcatalog.additional.SqliteHelper;
+import ru.volgorobot.vrcatalog.model.FavoritesListModel;
+
+public class FavoritesDBPresenter {
+ private SqliteHelper mHelper;
+ private SQLiteDatabase mDB;
+
+ public FavoritesDBPresenter(Context context) {
+ this.mHelper = new SqliteHelper(context);
+ this.mDB = mHelper.getWritableDatabase();
+ }
+
+ public ArrayList selectAll() {
+ ArrayList models = new ArrayList<>();
+
+ String[] columns = {
+ DatabaseProperties.FAVORITES_COLUMN_ID,
+ DatabaseProperties.FAVORITES_COLUMN_ITEM_ID,
+ DatabaseProperties.FAVORITES_COLUMN_ITEM_NAME
+ };
+
+ Cursor cursor = mDB.query(DatabaseProperties.FAVORITES_TABLE_NAME, columns, null, null, null, null, null);
+
+ if(cursor == null)
+ return new ArrayList<>();
+
+ while(cursor.moveToNext()) {
+ models.add(new FavoritesListModel(
+ cursor.getInt(cursor.getColumnIndex(DatabaseProperties.FAVORITES_COLUMN_ID)),
+ cursor.getInt(cursor.getColumnIndex(DatabaseProperties.FAVORITES_COLUMN_ITEM_ID)),
+ cursor.getString(cursor.getColumnIndex(DatabaseProperties.FAVORITES_COLUMN_ITEM_NAME))));
+ }
+
+ cursor.close();
+
+ return models;
+ }
+
+ public void add(FavoritesListModel model) {
+ ContentValues contentValues = new ContentValues();
+ contentValues.put(DatabaseProperties.FAVORITES_COLUMN_ITEM_ID, model.getItemID());
+ contentValues.put(DatabaseProperties.FAVORITES_COLUMN_ITEM_NAME, model.getItemName());
+ mDB.insert(DatabaseProperties.FAVORITES_TABLE_NAME, null, contentValues);
+ }
+
+ public void removeItem(int id) {
+ mDB.delete(DatabaseProperties.FAVORITES_TABLE_NAME, "_id = " + id, null);
+ }
+
+ public boolean inFavoritesList(int itemID) {
+ String[] columns = { DatabaseProperties.FAVORITES_COLUMN_ITEM_ID };
+ String[] selectionArgs = { Integer.toString(itemID) };
+ Cursor cursor = mDB.query(DatabaseProperties.FAVORITES_TABLE_NAME, columns, DatabaseProperties.FAVORITES_COLUMN_ITEM_ID + " = ?", selectionArgs, null, null, null);
+ while(cursor.moveToNext()) {
+ if(cursor.getInt(cursor.getColumnIndex(DatabaseProperties.FAVORITES_COLUMN_ITEM_ID)) == itemID)
+ return true;
+ }
+ cursor.close();
+
+ return false;
+ }
+
+ public int getFavoriteID(int itemID) {
+ String[] columns = { DatabaseProperties.FAVORITES_COLUMN_ID, DatabaseProperties.FAVORITES_COLUMN_ITEM_ID };
+ Cursor cursor = mDB.query(DatabaseProperties.FAVORITES_TABLE_NAME, columns, DatabaseProperties.FAVORITES_COLUMN_ITEM_ID + " = " + Integer.toString(itemID), null, null, null, null);
+ int id = 0;
+ if( cursor != null && cursor.moveToFirst() ){
+ id = cursor.getInt(cursor.getColumnIndex(DatabaseProperties.FAVORITES_COLUMN_ID));
+ cursor.close();
+ }
+ return id;
+ }
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/presenters/FavoritesPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/presenters/FavoritesPresenter.java
new file mode 100644
index 0000000..d61fb58
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/presenters/FavoritesPresenter.java
@@ -0,0 +1,77 @@
+package ru.volgorobot.vrcatalog.presenters;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.AsyncTask;
+
+import java.util.ArrayList;
+
+import ru.volgorobot.vrcatalog.MainContract;
+import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
+import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
+import ru.volgorobot.vrcatalog.model.CoreModel;
+import ru.volgorobot.vrcatalog.model.DetailModel;
+import ru.volgorobot.vrcatalog.view.FavoritesActivity;
+import ru.volgorobot.vrcatalog.view.ItemActivity;
+
+public class FavoritesPresenter implements MainContract.FavoritesPresenter {
+ private Context mContext;
+ private MainContract.MainModel mainModel;
+ private MainContract.FavoritesActivityView mView;
+
+ public FavoritesPresenter(Context context, FavoritesActivity view) {
+ this.mContext = context;
+ this.mainModel = new CoreModel(context);
+ this.mView = view;
+ }
+
+ @Override
+ public void fetchItem(int itemID) {
+ new AsyncTask>() {
+ @Override
+ protected ResultWithErrorCode doInBackground(Integer... integers) {
+ ArrayList detail = null;
+ try {
+ detail = mainModel.fetchDetailByID(integers[0]);
+ } catch (NetworkErrorException networkErrorException) {
+ return new ResultWithErrorCode<>(detail.get(0), networkErrorException.getErrorCode());
+ } catch (NullPointerException nullPointerException) {
+ return new ResultWithErrorCode<>(detail.get(0), 3);
+ }
+ return new ResultWithErrorCode<>(detail.get(0), 0);
+ }
+
+ @Override
+ protected void onPostExecute(ResultWithErrorCode result) {
+ super.onPostExecute(result);
+ switch (result.getErrorCode()) {
+ case 0: {
+ Intent intent = new Intent(mContext, ItemActivity.class);
+ intent.putExtra("itemID", result.getData().getID().toString());
+ intent.putExtra("detailName", result.getData().getName());
+ intent.putExtra("detailQuantity", result.getData().getNumber().toString());
+ intent.putExtra("detailPrice", result.getData().getPrice());
+ intent.putExtra("detailCountry", result.getData().getCountry());
+ intent.putExtra("detailAnalogue", result.getData().getAnalogue());
+ intent.putExtra("detailDatasheet", result.getData().getDocURL());
+ intent.putExtra("detailNotes", result.getData().getNotes());
+ mView.startActivityByIntent(intent);
+ break;
+ }
+ case 1: {
+ mView.onFailruleAnswer(1);
+ break;
+ }
+ case 2: {
+ mView.onFailruleAnswer(2);
+ break;
+ }
+ case 3: {
+ mView.onFailruleAnswer(3);
+ break;
+ }
+ }
+ }
+ }.execute(itemID);
+ }
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/presenters/ViewBinderPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/presenters/ViewBinderPresenter.java
index fd5f3a8..0c26546 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/presenters/ViewBinderPresenter.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/presenters/ViewBinderPresenter.java
@@ -15,9 +15,9 @@ import ru.volgorobot.vrcatalog.model.DetailModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
-import ru.volgorobot.vrcatalog.view.DetailActivity;
+import ru.volgorobot.vrcatalog.view.ItemActivity;
-public class ViewBinderPresenter implements MainContract.ViewBinderPresenter {
+public class ViewBinderPresenter implements MainContract.LevelLoaderPresenter {
private MainContract.MainActivityView mView;
private MainContract.MainModel coreModel;
private MainContract.ViewBinder viewBinder;
@@ -170,7 +170,7 @@ public class ViewBinderPresenter implements MainContract.ViewBinderPresenter {
super.onPostExecute(result);
switch (result.getErrorCode()) {
case 0: {
- Intent intent = new Intent(context, DetailActivity.class);
+ Intent intent = new Intent(context, ItemActivity.class);
intent.putExtra("itemID", result.getData().getID().toString());
intent.putExtra("detailName", result.getData().getName());
intent.putExtra("detailQuantity", result.getData().getNumber().toString());
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/FavoritesActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/FavoritesActivity.java
new file mode 100644
index 0000000..e0fc419
--- /dev/null
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/FavoritesActivity.java
@@ -0,0 +1,121 @@
+package ru.volgorobot.vrcatalog.view;
+
+import android.content.Intent;
+import android.support.v7.app.AppCompatActivity;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.ContextMenu;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.ListView;
+import android.widget.Toast;
+
+import java.util.ArrayList;
+
+import ru.volgorobot.vrcatalog.MainContract;
+import ru.volgorobot.vrcatalog.R;
+import ru.volgorobot.vrcatalog.additional.adapters.FavoritesListAdapter;
+import ru.volgorobot.vrcatalog.model.FavoritesListModel;
+import ru.volgorobot.vrcatalog.presenters.FavoritesDBPresenter;
+import ru.volgorobot.vrcatalog.presenters.FavoritesPresenter;
+
+public class FavoritesActivity extends AppCompatActivity implements MainContract.FavoritesActivityView {
+ private FavoritesDBPresenter db;
+ private ListView listView;
+ private FavoritesListAdapter adapter;
+ private MainContract.FavoritesPresenter mPresenter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_favorites);
+
+ mPresenter = new FavoritesPresenter(FavoritesActivity.this, this);
+
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+ getSupportActionBar().setHomeButtonEnabled(true);
+
+ db = new FavoritesDBPresenter(FavoritesActivity.this);
+ listView = findViewById(R.id.favorites_list_view);
+ registerForContextMenu(listView);
+ listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
+ FavoritesListModel model = (FavoritesListModel) listView.getItemAtPosition(i);
+ mPresenter.fetchItem(model.getItemID());
+ }
+ });
+
+ ArrayList models = db.selectAll();
+ adapter = new FavoritesListAdapter(FavoritesActivity.this, android.R.layout.simple_list_item_1, models);
+ listView.setAdapter(adapter);
+ }
+
+ @Override
+ public boolean onContextItemSelected(MenuItem item) {
+ AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
+ switch (item.getItemId()) {
+ case 0: {
+ FavoritesListModel model = (FavoritesListModel) listView.getItemAtPosition(info.position);
+ db.removeItem(model.getID());
+ updateListView();
+ }
+ }
+ return true;
+ }
+
+ private void updateListView() {
+ adapter.updateData();
+ adapter.notifyDataSetChanged();
+ }
+
+ @Override
+ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
+ if (v.getId() == R.id.favorites_list_view) {
+ menu.add(0, 0, 0,"Удалить");
+ }
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch(item.getItemId()) {
+ case android.R.id.home:
+ finish();
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+
+ @Override
+ public void onFailruleAnswer(int errorCode) {
+ switch (errorCode) {
+ case 1: {
+ Toast.makeText(FavoritesActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
+ Log.e("FavoritesActivity", "Network Error! Re-check your connection credentials or network settings!");
+ break;
+ }
+ case 2: {
+ Toast.makeText(FavoritesActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
+ Log.e("FavoritesActivity", "Answer of server is wrong! Re-check your connection credentials!");
+ break;
+ }
+ case 3: {
+ Toast.makeText(FavoritesActivity.this, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show();
+ Log.e("MainActivity", "Invalid-formatted URL. Please, check URL (change his if it need) and try again.");
+ break;
+ }
+ }
+ }
+
+ @Override
+ public void startActivityByIntent(Intent intent) {
+ startActivity(intent);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ updateListView();
+ }
+}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/ItemActivity.java
similarity index 68%
rename from app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java
rename to app/src/main/java/ru/volgorobot/vrcatalog/view/ItemActivity.java
index 98b3870..c9154e5 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/ItemActivity.java
@@ -9,24 +9,33 @@ import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.LinkedHashMap;
+import java.util.Map;
+import ru.volgorobot.vrcatalog.model.FavoritesListModel;
+import ru.volgorobot.vrcatalog.presenters.FavoritesDBPresenter;
import ru.volgorobot.vrcatalog.presenters.ItemPresenter;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.additional.adapters.ImagePagerAdapter;
import ru.volgorobot.vrcatalog.additional.adapters.PropertiesListAdapter;
-public class DetailActivity extends AppCompatActivity implements MainContract.ItemView {
+public class ItemActivity extends AppCompatActivity implements MainContract.ItemView {
private MainContract.ItemPresenter mPresenter;
private ViewPager viewPager;
private ListView propertiesList;
private ImagePagerAdapter imagePagerAdapter;
+ private boolean inFavorites;
+ private FavoritesDBPresenter db;
+ private int itemID;
+ private String itemName;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -38,10 +47,11 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
initializeViews();
- mPresenter = new ItemPresenter(DetailActivity.this, this);
+ mPresenter = new ItemPresenter(ItemActivity.this, this);
Intent intent = getIntent();
- ((TextView) findViewById(R.id.itemName)).setText(intent.getStringExtra("detailName"));
+ itemName = intent.getStringExtra("detailName");
+ ((TextView) findViewById(R.id.itemName)).setText(itemName);
String tmp = intent.getStringExtra("detailPrice");
if(tmp.equals("0")) {
tmp = "Уточните у продавца";
@@ -50,13 +60,18 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
}
((TextView) findViewById(R.id.itemPrice)).setText(tmp);
- LinkedHashMap extrasMap = getIntentExtras(intent);
+ LinkedHashMap intentDataForListView = getIntentExtras(intent);
+ itemID = Integer.parseInt(intent.getStringExtra("itemID"));
+ intentDataForListView.get("detailName");
+ db = new FavoritesDBPresenter(ItemActivity.this);
propertiesList = findViewById(R.id.propertiesList);
- PropertiesListAdapter propertiesListAdapter = new PropertiesListAdapter(extrasMap);
+ PropertiesListAdapter propertiesListAdapter = new PropertiesListAdapter(intentDataForListView);
propertiesList.setAdapter(propertiesListAdapter);
mPresenter.getImagesByItemID(Integer.parseInt(intent.getStringExtra("itemID")));
+
+ inFavorites = db.inFavoritesList(itemID);
}
void initializeViews() {
@@ -116,18 +131,18 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
public void onFailureAnswer(int errorCode) {
switch (errorCode) {
case 1: {
- Toast.makeText(DetailActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
- Log.e("DetailActivity", "Network Error! Re-check your connection credentials or network settings!");
+ Toast.makeText(ItemActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
+ Log.e("ItemActivity", "Network Error! Re-check your connection credentials or network settings!");
break;
}
case 2: {
- Toast.makeText(DetailActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
- Log.e("DetailActivity", "Answer of server is wrong! Re-check your connection credentials!");
+ Toast.makeText(ItemActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
+ Log.e("ItemActivity", "Answer of server is wrong! Re-check your connection credentials!");
break;
}
case 3: {
- Toast.makeText(DetailActivity.this, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show();
- Log.e("DetailActivity", "Invalid-formatted URL. Please, check URL (change his if it need) and try again.");
+ Toast.makeText(ItemActivity.this, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show();
+ Log.e("ItemActivity", "Invalid-formatted URL. Please, check URL (change his if it need) and try again.");
break;
}
}
@@ -135,7 +150,7 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
@Override
public void setViewPagerContent(String[] uris) {
- imagePagerAdapter = new ImagePagerAdapter(DetailActivity.this, uris);
+ imagePagerAdapter = new ImagePagerAdapter(ItemActivity.this, uris);
TabLayout tabLayout = findViewById(R.id.tabDots);
viewPager.setAdapter(imagePagerAdapter);
setViewPagerScrollListener();
@@ -200,4 +215,34 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
};
viewPager.setOnPageChangeListener(listener);
}
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.item_activity_menu, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onPrepareOptionsMenu(Menu menu) {
+ MenuItem addToFavorites = menu.findItem(R.id.action_favorite);
+ MenuItem deleteFromFavorites = menu.findItem(R.id.action_unfavorite);
+
+ addToFavorites.setOnMenuItemClickListener((MenuItem menuItem) -> {
+ db.add(new FavoritesListModel(0, itemID, itemName));
+ inFavorites = true;
+ this.invalidateOptionsMenu();
+ return true;
+ });
+ deleteFromFavorites.setOnMenuItemClickListener((MenuItem menuItem) -> {
+ db.removeItem(db.getFavoriteID(itemID));
+ inFavorites = false;
+ this.invalidateOptionsMenu();
+ return true;
+ });
+
+ addToFavorites.setVisible(!inFavorites);
+ deleteFromFavorites.setVisible(inFavorites);
+ return true;
+ }
}
diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java
index 71a2ea2..12ec8bd 100644
--- a/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java
+++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java
@@ -113,6 +113,8 @@ public class MainActivity extends AppCompatActivity
} else if (id == R.id.nav_about) {
startActivity(new Intent(this, AboutActivity.class));
+ } else if(id == R.id.nav_favorites) {
+ startActivity(new Intent(this, FavoritesActivity.class));
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
diff --git a/app/src/main/res/drawable/ic_favorite_black_24dp.xml b/app/src/main/res/drawable/ic_favorite_black_24dp.xml
new file mode 100644
index 0000000..cfba5d8
--- /dev/null
+++ b/app/src/main/res/drawable/ic_favorite_black_24dp.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/app/src/main/res/drawable/ic_favorite_border_white_24dp.xml b/app/src/main/res/drawable/ic_favorite_border_white_24dp.xml
new file mode 100644
index 0000000..63fb576
--- /dev/null
+++ b/app/src/main/res/drawable/ic_favorite_border_white_24dp.xml
@@ -0,0 +1,5 @@
+
+
+
diff --git a/app/src/main/res/drawable/ic_favorite_white_24dp.xml b/app/src/main/res/drawable/ic_favorite_white_24dp.xml
new file mode 100644
index 0000000..67a25e7
--- /dev/null
+++ b/app/src/main/res/drawable/ic_favorite_white_24dp.xml
@@ -0,0 +1,5 @@
+
+
+
diff --git a/app/src/main/res/layout/activity_favorites.xml b/app/src/main/res/layout/activity_favorites.xml
new file mode 100644
index 0000000..14f501e
--- /dev/null
+++ b/app/src/main/res/layout/activity_favorites.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml
index 0bf6303..4bcd925 100644
--- a/app/src/main/res/menu/activity_main_drawer.xml
+++ b/app/src/main/res/menu/activity_main_drawer.xml
@@ -8,6 +8,10 @@
android:icon="@drawable/ic_home"
android:title="Главная"
android:checked="true" />
+
-
diff --git a/app/src/main/res/menu/item_activity_menu.xml b/app/src/main/res/menu/item_activity_menu.xml
new file mode 100644
index 0000000..d97f786
--- /dev/null
+++ b/app/src/main/res/menu/item_activity_menu.xml
@@ -0,0 +1,16 @@
+
+
\ No newline at end of file