Added the ability to view details in detail.

This commit is contained in:
ChronosX88 2019-01-05 17:37:29 +04:00
parent 90dae086e4
commit e7795ca4c5
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
13 changed files with 266 additions and 16 deletions

View File

@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.volgorobot.vrcatalog">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
@ -22,7 +22,8 @@
</intent-filter>
</activity>
<activity android:name=".view.SettingsActivity" />
<activity android:name=".view.AboutActivity"></activity>
<activity android:name=".view.AboutActivity" />
<activity android:name=".view.DetailActivity"></activity>
</application>
</manifest>

View File

@ -1,8 +1,12 @@
package ru.volgorobot.vrcatalog;
import android.content.Intent;
import java.util.ArrayList;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.model.CoreModel;
import ru.volgorobot.vrcatalog.model.DetailModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
@ -13,16 +17,19 @@ public interface MainContract {
void buildFirstLevelTree(ArrayList<FirstLevelModel> firstLevelModels);
void onFailureAnswer(int errorCode);
void refreshTree();
void startActivityByIntent(Intent intent);
}
interface Presenter {
void getFirstLevel();
CoreModel getCoreModel();
}
interface MainModel {
ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException;
ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<DetailModel> getDetailByID(int detailID) throws NetworkErrorException, NullPointerException;
void reinitializeApi() throws IllegalStateException;
boolean getApiState();
}

View File

@ -12,7 +12,7 @@ import ru.volgorobot.vrcatalog.model.FirstLevelModel;
public class MainPresenter implements MainContract.Presenter {
private MainContract.MainActivityView mView;
private MainContract.MainModel coreModel;
private CoreModel coreModel;
public MainPresenter(MainContract.MainActivityView mainActivityView, Context context) {
this.mView = mainActivityView;
@ -66,4 +66,9 @@ public class MainPresenter implements MainContract.Presenter {
}
}.execute();
}
@Override
public CoreModel getCoreModel() {
return this.coreModel;
}
}

View File

@ -6,28 +6,31 @@ import android.view.View;
import me.texy.treeview.base.BaseNodeViewBinder;
import me.texy.treeview.base.BaseNodeViewFactory;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.model.CoreModel;
import ru.volgorobot.vrcatalog.nodeViewBinders.FirstLevelNodeViewBinder;
import ru.volgorobot.vrcatalog.nodeViewBinders.SecondLevelNodeViewBinder;
import ru.volgorobot.vrcatalog.nodeViewBinders.ThirdLevelNodeViewBinder;
public class NodeViewFactory extends BaseNodeViewFactory {
MainContract.MainActivityView mView;
CoreModel coreModel;
Context context;
public NodeViewFactory(Context context, MainContract.MainActivityView mView) {
public NodeViewFactory(Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
this.mView = mView;
this.context = context;
this.coreModel = coreModel;
}
@Override
public BaseNodeViewBinder getNodeViewBinder(View view, int level) {
switch (level) {
case 0:
return new FirstLevelNodeViewBinder(view, context, mView);
return new FirstLevelNodeViewBinder(view, context, mView, coreModel);
case 1:
return new SecondLevelNodeViewBinder(view, context, mView);
return new SecondLevelNodeViewBinder(view, context, mView, coreModel);
case 2:
return new ThirdLevelNodeViewBinder(view);
return new ThirdLevelNodeViewBinder(view, context, mView, coreModel);
default:
return null;
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import ru.volgorobot.vrcatalog.model.DetailModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
@ -19,4 +20,7 @@ public interface VRApi {
@GET("/API/Api.php?action=getDetailsByParentID")
Call<List<ThirdLevelModel>> getDetails(@Query("parentID") int parentID);
@GET("/API/Api.php?action=getDetailByID")
Call<List<DetailModel>> getDetailByID(@Query("detailID") int detailID);
}

View File

@ -3,7 +3,6 @@ package ru.volgorobot.vrcatalog.model;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.github.msteinbeck.sig4j.signal.Signal1;
import java.io.IOException;
import java.util.ArrayList;
@ -19,7 +18,6 @@ public class CoreModel implements MainContract.MainModel {
private Context context;
private VRApi vrApi;
private String baseURL;
public final Signal1<Integer> errorSignal = new Signal1<>();
public CoreModel(Context context) {
this.context = context;
@ -89,6 +87,25 @@ public class CoreModel implements MainContract.MainModel {
return thirdLevelModels;
}
@Override
public ArrayList<DetailModel> getDetailByID(int detailID) throws NetworkErrorException, NullPointerException {
if(vrApi == null) {
throw new NullPointerException();
}
ArrayList<DetailModel> detail = new ArrayList<>();
try {
Response<List<DetailModel>> response = vrApi.getDetailByID(detailID).execute();
if(response.isSuccessful()) {
detail.addAll(response.body());
} else {
throw new NetworkErrorException(2);
}
} catch (IOException e) {
throw new NetworkErrorException(1);
}
return detail;
}
@Override
public void reinitializeApi() throws IllegalStateException {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

View File

@ -24,13 +24,12 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {
CoreModel coreModel;
MainContract.MainActivityView mView;
public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) {
public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.node_name_view);
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
this.coreModel = new CoreModel(context);
this.coreModel = coreModel;
this.mView = mView;
coreModel.errorSignal.connect(mView::onFailureAnswer);
}
@Override

View File

@ -25,11 +25,11 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder {
MainContract.MainActivityView mView;
public SecondLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) {
public SecondLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.node_name_view);
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
this.coreModel = new CoreModel(context);
this.coreModel = coreModel;
this.mView = mView;
}

View File

@ -1,17 +1,35 @@
package ru.volgorobot.vrcatalog.nodeViewBinders;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import me.texy.treeview.TreeNode;
import me.texy.treeview.base.BaseNodeViewBinder;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R;
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.model.ThirdLevelModel;
import ru.volgorobot.vrcatalog.view.DetailActivity;
public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder {
TextView textView;
Context context;
MainContract.MainModel coreModel;
MainContract.MainActivityView mView;
public ThirdLevelNodeViewBinder(View itemView) {
public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
super(itemView);
this.context = context;
this.coreModel = coreModel;
this.mView = mView;
textView = (TextView) itemView.findViewById(R.id.node_name_view);
}
@ -24,4 +42,58 @@ public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder {
public void bindView(final TreeNode treeNode) {
textView.setText(treeNode.getValue().toString());
}
@Override
public void onNodeToggled(TreeNode treeNode, boolean expand) {
super.onNodeToggled(treeNode, expand);
new AsyncTask<Void, Void, ResultWithErrorCode<DetailModel>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
protected ResultWithErrorCode<DetailModel> doInBackground(Void... voids) {
ArrayList<DetailModel> detail = null;
try {
detail = coreModel.getDetailByID(((ThirdLevelModel)treeNode.getValue()).getID());
} 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(context, DetailActivity.class);
intent.putExtra("detailName", result.getData().getName());
intent.putExtra("detailQuantity", result.getData().getNumber().toString());
intent.putExtra("detailPrice", result.getData().getPrice());
intent.putExtra("detailNotes", result.getData().getNotes());
mView.startActivityByIntent(intent);
break;
}
case 1: {
mView.onFailureAnswer(1);
break;
}
case 2: {
mView.onFailureAnswer(2);
break;
}
case 3: {
mView.onFailureAnswer(3);
break;
}
}
mView.swipeLayoutSetRefreshing(false);
}
}.execute();
}
}

View File

@ -0,0 +1,50 @@
package ru.volgorobot.vrcatalog.view;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import ru.volgorobot.vrcatalog.R;
public class DetailActivity extends AppCompatActivity {
TextView nameView;
TextView quantityView;
TextView priceView;
EditText notesView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
initializeViews();
Intent intent = getIntent();
nameView.setText(intent.getStringExtra("detailName"));
quantityView.setText(intent.getStringExtra("detailQuantity"));
priceView.setText(intent.getStringExtra("detailPrice"));
notesView.setText(intent.getStringExtra("detailNotes"));
}
void initializeViews() {
nameView = findViewById(R.id.name);
quantityView = findViewById(R.id.quantity);
priceView = findViewById(R.id.price);
notesView = findViewById(R.id.notes);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -55,7 +55,7 @@ public class MainActivity extends AppCompatActivity
mPresenter = new MainPresenter(this, MainActivity.this);
treeView = new TreeView(rootNode, MainActivity.this, new NodeViewFactory(MainActivity.this, this));
treeView = new TreeView(rootNode, MainActivity.this, new NodeViewFactory(MainActivity.this, this, this.mPresenter.getCoreModel()));
treeView.setItemAnimator(new DefaultItemAnimator());
((ViewGroup) findViewById(R.id.treeViewContainer)).addView(treeView.getView());
mSwipeRefreshLayout.setRefreshing(true);
@ -150,6 +150,11 @@ public class MainActivity extends AppCompatActivity
public void refreshTree() {
treeView.refreshTreeView();
}
@Override
public void startActivityByIntent(Intent intent) {
startActivity(intent);
}
}

View File

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Название: "
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintBaseline_toBaselineOf="@+id/name"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/name_text"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/notes_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="Описание: "
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintStart_toStartOf="@+id/price_text"
app:layout_constraintTop_toBottomOf="@+id/price_text" />
<TextView
android:id="@+id/quantity_text"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:text="Количество на складе: "
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintBaseline_toBaselineOf="@+id/quantity"
app:layout_constraintStart_toStartOf="@+id/name_text" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/quantity_text"
app:layout_constraintTop_toBottomOf="@+id/name" />
<EditText
android:id="@+id/notes"
android:layout_width="286dp"
android:layout_height="99dp"
android:layout_marginTop="3dp"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="textMultiLine"
app:layout_constraintStart_toStartOf="@+id/notes_text"
app:layout_constraintTop_toBottomOf="@+id/notes_text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/price_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Цена: "
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
app:layout_constraintBaseline_toBaselineOf="@+id/price"
app:layout_constraintStart_toStartOf="@+id/quantity_text" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/price_text"
app:layout_constraintTop_toBottomOf="@+id/quantity_text" />
</android.support.constraint.ConstraintLayout>