From e7795ca4c5dad9ad1ed714cd48dfbf8ac9ebd1db Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 17:37:29 +0400 Subject: [PATCH] Added the ability to view details in detail. --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes app/src/main/AndroidManifest.xml | 5 +- .../ru/volgorobot/vrcatalog/MainContract.java | 7 ++ .../volgorobot/vrcatalog/MainPresenter.java | 7 +- .../vrcatalog/additional/NodeViewFactory.java | 11 ++- .../ru/volgorobot/vrcatalog/api/VRApi.java | 4 + .../volgorobot/vrcatalog/model/CoreModel.java | 21 ++++- .../FirstLevelNodeViewBinder.java | 5 +- .../SecondLevelNodeViewBinder.java | 4 +- .../ThirdLevelNodeViewBinder.java | 74 ++++++++++++++- .../vrcatalog/view/DetailActivity.java | 50 ++++++++++ .../vrcatalog/view/MainActivity.java | 7 +- app/src/main/res/layout/activity_detail.xml | 87 ++++++++++++++++++ 13 files changed, 266 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java create mode 100644 app/src/main/res/layout/activity_detail.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 1b56c5d76906aca8b9bd67fc9759fefd1ac50436..4d8407c9fc8e10ed9d50bc92fd154b877dc483e6 100644 GIT binary patch delta 14 WcmbQqGLvP(Oy+;vrf!^5%Lo82!v=c* delta 14 VcmbQqGLvP(Oy)+t(2a9y8388f1vUTx diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6b73d4a..1539b9a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,7 +2,7 @@ - + - + + \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java index 2e7232f..4c4a379 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java @@ -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 firstLevelModels); void onFailureAnswer(int errorCode); void refreshTree(); + void startActivityByIntent(Intent intent); } interface Presenter { void getFirstLevel(); + CoreModel getCoreModel(); } interface MainModel { ArrayList getFirstLevel() throws NetworkErrorException, NullPointerException; ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; ArrayList getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList getDetailByID(int detailID) throws NetworkErrorException, NullPointerException; void reinitializeApi() throws IllegalStateException; boolean getApiState(); } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java index 3fa59d7..8bac518 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -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; + } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java index 0537306..6343fe4 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java @@ -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; } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java index 8d6f164..80e062d 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java @@ -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> getDetails(@Query("parentID") int parentID); + + @GET("/API/Api.php?action=getDetailByID") + Call> getDetailByID(@Query("detailID") int detailID); } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java index 90e2498..349dded 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java @@ -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 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 getDetailByID(int detailID) throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + ArrayList detail = new ArrayList<>(); + try { + Response> 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); diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java index 52865a2..72a2a32 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -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 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 670fcb1..1494cb2 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -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; } 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 238cabe..d7c55ae 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java @@ -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>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode doInBackground(Void... voids) { + ArrayList 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 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(); + } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java new file mode 100644 index 0000000..44e90fc --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java @@ -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); + } +} 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 dffbd16..88a25af 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java @@ -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); + } } diff --git a/app/src/main/res/layout/activity_detail.xml b/app/src/main/res/layout/activity_detail.xml new file mode 100644 index 0000000..1d1daf9 --- /dev/null +++ b/app/src/main/res/layout/activity_detail.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file