Made favorites system

This commit is contained in:
ChronosX88 2019-02-22 20:52:00 +04:00
parent 88a9da5f47
commit 7c1d686be6
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
24 changed files with 523 additions and 36 deletions

View File

@ -147,7 +147,7 @@
<PersistentState> <PersistentState>
<option name="values"> <option name="values">
<map> <map>
<entry key="url" value="jar:file:/home/chronosx/%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D1%8B/bin/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/file/ic_cloud_off_black_24dp.xml" /> <entry key="url" value="jar:file:/home/chronosx/%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D1%8B/bin/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/action/ic_favorite_black_24dp.xml" />
</map> </map>
</option> </option>
</PersistentState> </PersistentState>
@ -157,8 +157,7 @@
</option> </option>
<option name="values"> <option name="values">
<map> <map>
<entry key="color" value="a9a9a9" /> <entry key="outputName" value="ic_favorite_black_24dp" />
<entry key="outputName" value="ic_cloud_off_black_24dp" />
<entry key="sourceFile" value="$USER_HOME$" /> <entry key="sourceFile" value="$USER_HOME$" />
</map> </map>
</option> </option>

Binary file not shown.

View File

@ -6,8 +6,8 @@ android {
applicationId "ru.volgorobot.vrcatalog" applicationId "ru.volgorobot.vrcatalog"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 27 targetSdkVersion 27
versionCode 5 versionCode 7
versionName "0.3.1" versionName "0.5alpha"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
compileOptions { compileOptions {

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.volgorobot.vrcatalog"> package="ru.volgorobot.vrcatalog">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
@ -12,6 +11,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".view.FavoritesActivity"></activity>
<activity <activity
android:name=".view.MainActivity" android:name=".view.MainActivity"
android:label="@string/app_name" android:label="@string/app_name"
@ -26,26 +26,31 @@
<activity android:name=".view.AboutActivity" /> <activity android:name=".view.AboutActivity" />
<activity <activity
android:name=".view.FullScreenImageActivity" android:name=".view.FullScreenImageActivity"
android:theme="@style/AppTheme.Fullscreen"/> android:theme="@style/AppTheme.Fullscreen" />
<activity <activity
android:name=".view.DetailActivity" android:name=".view.ItemActivity"
android:theme="@style/ItemActivityTheme"/> android:theme="@style/ItemActivityTheme" />
<activity <activity
android:name=".view.SearchableActivity" android:name=".view.SearchableActivity"
android:label="Поиск" android:label="Поиск"
android:parentActivityName="ru.volgorobot.vrcatalog.view.MainActivity"> android:parentActivityName=".view.MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower --> <!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value="ru.volgorobot.vrcatalog.view.MainActivity" /> android:value="ru.volgorobot.vrcatalog.view.MainActivity" />
<!-- meta tag and intent filter go into results activity --> <!-- meta tag and intent filter go into results activity -->
<meta-data android:name="android.app.searchable" <meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" /> android:resource="@xml/searchable" />
<intent-filter> <intent-filter>
<action android:name="android.intent.action.SEARCH" /> <action android:name="android.intent.action.SEARCH" />
</intent-filter> </intent-filter>
</activity> </activity>
<provider android:name=".additional.RecentSuggestionProvider"
<provider
android:name=".additional.RecentSuggestionProvider"
android:authorities="ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider" /> android:authorities="ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider" />
</application> </application>

View File

@ -27,12 +27,21 @@ public interface MainContract {
CoreModel getCoreModel(); CoreModel getCoreModel();
} }
interface ViewBinderPresenter { interface LevelLoaderPresenter {
void fetchSecondLevel(FirstLevelModel firstLevelModel, TreeNode treeNode); void fetchSecondLevel(FirstLevelModel firstLevelModel, TreeNode treeNode);
void fetchThirdLevel(SecondLevelModel secondLevelModel, TreeNode treeNode); void fetchThirdLevel(SecondLevelModel secondLevelModel, TreeNode treeNode);
void fetchDetail(ThirdLevelModel thirdLevelModel); void fetchDetail(ThirdLevelModel thirdLevelModel);
} }
interface FavoritesPresenter {
void fetchItem(int itemID);
}
interface FavoritesActivityView {
void onFailruleAnswer(int errorCode);
void startActivityByIntent(Intent intent);
}
interface ViewBinder { interface ViewBinder {
void rotateImageView(); void rotateImageView();
} }

View File

@ -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";
}

View File

@ -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);
}
}

View File

@ -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<FavoritesListModel> {
private FavoritesDBPresenter db;
public FavoritesListAdapter(Context context, int resource, List<FavoritesListModel> objects) {
super(context, resource, objects);
db = new FavoritesDBPresenter(context);
}
public void updateData() {
super.clear();
super.addAll(db.selectAll());
}
}

View File

@ -13,16 +13,14 @@ import android.widget.ImageView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import ru.volgorobot.vrcatalog.R; import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.view.DetailActivity;
import ru.volgorobot.vrcatalog.view.FullScreenImageActivity; import ru.volgorobot.vrcatalog.view.FullScreenImageActivity;
public class ImagePagerAdapter extends PagerAdapter { 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; private Context context;

View File

@ -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();
}
}

View File

@ -15,7 +15,7 @@ import ru.volgorobot.vrcatalog.model.SecondLevelModel;
public class SecondLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { public class SecondLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
TextView textView; TextView textView;
ImageView imageView; ImageView imageView;
MainContract.ViewBinderPresenter viewBinderPresenter; MainContract.LevelLoaderPresenter viewBinderPresenter;
public SecondLevelNodeViewBinder(View itemView, MainContract.MainActivityView mView, Context context) { public SecondLevelNodeViewBinder(View itemView, MainContract.MainActivityView mView, Context context) {

View File

@ -13,7 +13,7 @@ import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
TextView textView; TextView textView;
MainContract.ViewBinderPresenter viewBinderPresenter; MainContract.LevelLoaderPresenter viewBinderPresenter;
public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) { public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) {
super(itemView); super(itemView);

View File

@ -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<FavoritesListModel> selectAll() {
ArrayList<FavoritesListModel> 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;
}
}

View File

@ -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<Integer, Void, ResultWithErrorCode<DetailModel>>() {
@Override
protected ResultWithErrorCode<DetailModel> doInBackground(Integer... integers) {
ArrayList<DetailModel> 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<DetailModel> 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);
}
}

View File

@ -15,9 +15,9 @@ import ru.volgorobot.vrcatalog.model.DetailModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel;
import ru.volgorobot.vrcatalog.model.ThirdLevelModel; 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.MainActivityView mView;
private MainContract.MainModel coreModel; private MainContract.MainModel coreModel;
private MainContract.ViewBinder viewBinder; private MainContract.ViewBinder viewBinder;
@ -170,7 +170,7 @@ public class ViewBinderPresenter implements MainContract.ViewBinderPresenter {
super.onPostExecute(result); super.onPostExecute(result);
switch (result.getErrorCode()) { switch (result.getErrorCode()) {
case 0: { 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("itemID", result.getData().getID().toString());
intent.putExtra("detailName", result.getData().getName()); intent.putExtra("detailName", result.getData().getName());
intent.putExtra("detailQuantity", result.getData().getNumber().toString()); intent.putExtra("detailQuantity", result.getData().getNumber().toString());

View File

@ -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<FavoritesListModel> 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();
}
}

View File

@ -9,24 +9,33 @@ import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log; import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.util.LinkedHashMap; 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.presenters.ItemPresenter;
import ru.volgorobot.vrcatalog.MainContract; import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R; import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.additional.adapters.ImagePagerAdapter; import ru.volgorobot.vrcatalog.additional.adapters.ImagePagerAdapter;
import ru.volgorobot.vrcatalog.additional.adapters.PropertiesListAdapter; 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 MainContract.ItemPresenter mPresenter;
private ViewPager viewPager; private ViewPager viewPager;
private ListView propertiesList; private ListView propertiesList;
private ImagePagerAdapter imagePagerAdapter; private ImagePagerAdapter imagePagerAdapter;
private boolean inFavorites;
private FavoritesDBPresenter db;
private int itemID;
private String itemName;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -38,10 +47,11 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false);
initializeViews(); initializeViews();
mPresenter = new ItemPresenter(DetailActivity.this, this); mPresenter = new ItemPresenter(ItemActivity.this, this);
Intent intent = getIntent(); 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"); String tmp = intent.getStringExtra("detailPrice");
if(tmp.equals("0")) { if(tmp.equals("0")) {
tmp = "Уточните у продавца"; tmp = "Уточните у продавца";
@ -50,13 +60,18 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
} }
((TextView) findViewById(R.id.itemPrice)).setText(tmp); ((TextView) findViewById(R.id.itemPrice)).setText(tmp);
LinkedHashMap<String, String> extrasMap = getIntentExtras(intent); LinkedHashMap<String, String> intentDataForListView = getIntentExtras(intent);
itemID = Integer.parseInt(intent.getStringExtra("itemID"));
intentDataForListView.get("detailName");
db = new FavoritesDBPresenter(ItemActivity.this);
propertiesList = findViewById(R.id.propertiesList); propertiesList = findViewById(R.id.propertiesList);
PropertiesListAdapter propertiesListAdapter = new PropertiesListAdapter(extrasMap); PropertiesListAdapter propertiesListAdapter = new PropertiesListAdapter(intentDataForListView);
propertiesList.setAdapter(propertiesListAdapter); propertiesList.setAdapter(propertiesListAdapter);
mPresenter.getImagesByItemID(Integer.parseInt(intent.getStringExtra("itemID"))); mPresenter.getImagesByItemID(Integer.parseInt(intent.getStringExtra("itemID")));
inFavorites = db.inFavoritesList(itemID);
} }
void initializeViews() { void initializeViews() {
@ -116,18 +131,18 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
public void onFailureAnswer(int errorCode) { public void onFailureAnswer(int errorCode) {
switch (errorCode) { switch (errorCode) {
case 1: { case 1: {
Toast.makeText(DetailActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); Toast.makeText(ItemActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
Log.e("DetailActivity", "Network Error! Re-check your connection credentials or network settings!"); Log.e("ItemActivity", "Network Error! Re-check your connection credentials or network settings!");
break; break;
} }
case 2: { case 2: {
Toast.makeText(DetailActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); Toast.makeText(ItemActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
Log.e("DetailActivity", "Answer of server is wrong! Re-check your connection credentials!"); Log.e("ItemActivity", "Answer of server is wrong! Re-check your connection credentials!");
break; break;
} }
case 3: { case 3: {
Toast.makeText(DetailActivity.this, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show(); Toast.makeText(ItemActivity.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."); Log.e("ItemActivity", "Invalid-formatted URL. Please, check URL (change his if it need) and try again.");
break; break;
} }
} }
@ -135,7 +150,7 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
@Override @Override
public void setViewPagerContent(String[] uris) { public void setViewPagerContent(String[] uris) {
imagePagerAdapter = new ImagePagerAdapter(DetailActivity.this, uris); imagePagerAdapter = new ImagePagerAdapter(ItemActivity.this, uris);
TabLayout tabLayout = findViewById(R.id.tabDots); TabLayout tabLayout = findViewById(R.id.tabDots);
viewPager.setAdapter(imagePagerAdapter); viewPager.setAdapter(imagePagerAdapter);
setViewPagerScrollListener(); setViewPagerScrollListener();
@ -200,4 +215,34 @@ public class DetailActivity extends AppCompatActivity implements MainContract.It
}; };
viewPager.setOnPageChangeListener(listener); 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;
}
} }

View File

@ -113,6 +113,8 @@ public class MainActivity extends AppCompatActivity
} else if (id == R.id.nav_about) { } else if (id == R.id.nav_about) {
startActivity(new Intent(this, AboutActivity.class)); 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); DrawerLayout drawer = findViewById(R.id.drawer_layout);

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M16.5,3c-1.74,0 -3.41,0.81 -4.5,2.09C10.91,3.81 9.24,3 7.5,3 4.42,3 2,5.42 2,8.5c0,3.78 3.4,6.86 8.55,11.54L12,21.35l1.45,-1.32C18.6,15.36 22,12.28 22,8.5 22,5.42 19.58,3 16.5,3zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>

View File

@ -0,0 +1,14 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.FavoritesActivity">
<ListView
android:id="@+id/favorites_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

View File

@ -8,6 +8,10 @@
android:icon="@drawable/ic_home" android:icon="@drawable/ic_home"
android:title="Главная" android:title="Главная"
android:checked="true" /> android:checked="true" />
<item
android:id="@+id/nav_favorites"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="Избранные" />
<item <item
android:id="@+id/nav_settings" android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_applications" android:icon="@drawable/ic_settings_applications"
@ -16,5 +20,4 @@
android:id="@+id/nav_about" android:id="@+id/nav_about"
android:icon="@drawable/ic_help" android:icon="@drawable/ic_help"
android:title="О программе" /> android:title="О программе" />
</menu> </menu>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_favorite"
android:icon="@drawable/ic_favorite_border_white_24dp"
app:showAsAction="always|collapseActionView"
android:title="Добавить в избранные"
android:visible="false"/>
<item
android:id="@+id/action_unfavorite"
android:icon="@drawable/ic_favorite_white_24dp"
app:showAsAction="always|collapseActionView"
android:title="Удалить из избранного"
android:visible="false"/>
</menu>