From a9396c9c116d709bd66557f05a3e767ccd6ee033 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 16 Nov 2018 16:30:58 +0400 Subject: [PATCH 01/27] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=8E=D0=BD=D0=B8=D1=82-=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=B2=20(=D0=BE=D0=BD=D0=B8=20=D0=BF=D0=BE=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=87=D1=82=D0=BE=20=D0=BD=D0=B5=20=D0=BD=D1=83=D0=B6?= =?UTF-8?q?=D0=BD=D1=8B),=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20ActionBar=20=D0=B2=20SettingsActivity=20(?= =?UTF-8?q?=D1=81=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D1=8C=D1=8E=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20AppCompatPreferenceActivity).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/build_file_checksums.ser | Bin 531 -> 537 bytes app/build.gradle | 2 +- .../vrcatalog/ExampleInstrumentedTest.java | 26 ---- .../vrcatalog/SettingsActivity.java | 12 +- .../AppCompatPreferenceActivity.java | 125 ++++++++++++++++++ .../volgorobot/vrcatalog/ExampleUnitTest.java | 17 --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 8 files changed, 133 insertions(+), 55 deletions(-) delete mode 100644 app/src/androidTest/java/ru/volgorobot/vrcatalog/ExampleInstrumentedTest.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/AppCompatPreferenceActivity.java delete mode 100644 app/src/test/java/ru/volgorobot/vrcatalog/ExampleUnitTest.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 513b45630436762e7de8f50a7601af2a53243d4f..2f6f833776601a3c9893c6a53bcb25efc52fc661 100644 GIT binary patch delta 124 zcmV-?0E7RN1epYom;@YLk~@){hz}?)Xm4$0FJowOZ*FgMc$0$xHYW^Xb!lv5E@yIK zWNc-1aR7b*1^@s65N804UGtLG8*JGaElD)<(34969TOxD8XH1f#TivHn_oTyB{_5e e5tEDo8I;}n-&x2bg1(rm1gFo=%p7WrsSlS7BbW^urV+&2&k+IPTQ_zEp2ch ziCIlasdVxLMp>1(gkFb8S%K}}A6j3ZHUCw834@Y;Mt*LpezBoG)CB#>2NTesting documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("ru.volgorobot.vrcatalog", appContext.getPackageName()); - } -} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java index 928ab76..d74d8a9 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java @@ -1,23 +1,19 @@ package ru.volgorobot.vrcatalog; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; -import android.support.v7.app.AppCompatDelegate; import android.view.Menu; import android.view.MenuItem; -import android.preference.PreferenceActivity; +import ru.volgorobot.vrcatalog.additional.AppCompatPreferenceActivity; -public class SettingsActivity extends PreferenceActivity { - - private AppCompatDelegate mDelegate; +public class SettingsActivity extends AppCompatPreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.activity_settings); - //getActionBar().setDisplayHomeAsUpEnabled(true); - //getActionBar().setHomeButtonEnabled(true); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setHomeButtonEnabled(true); } @Override diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/AppCompatPreferenceActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/AppCompatPreferenceActivity.java new file mode 100644 index 0000000..cfd5368 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/AppCompatPreferenceActivity.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ru.volgorobot.vrcatalog.additional; +import android.content.res.Configuration; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.support.annotation.LayoutRes; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatDelegate; +import android.support.v7.widget.Toolbar; +import android.view.MenuInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls + * to be used with AppCompat. + * + * This technique can be used with an {@link android.app.Activity} class, not just + * {@link android.preference.PreferenceActivity}. + */ + +public abstract class AppCompatPreferenceActivity extends PreferenceActivity { + private AppCompatDelegate mDelegate; + @Override + protected void onCreate(Bundle savedInstanceState) { + getDelegate().installViewFactory(); + getDelegate().onCreate(savedInstanceState); + super.onCreate(savedInstanceState); + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + getDelegate().onPostCreate(savedInstanceState); + } + + public ActionBar getSupportActionBar() { + return getDelegate().getSupportActionBar(); + } + + public void setSupportActionBar(@Nullable Toolbar toolbar) { + getDelegate().setSupportActionBar(toolbar); + } + + @Override + public MenuInflater getMenuInflater() { + return getDelegate().getMenuInflater(); + } + + @Override + public void setContentView(@LayoutRes int layoutResID) { + getDelegate().setContentView(layoutResID); + } + + @Override + public void setContentView(View view) { + getDelegate().setContentView(view); + } + + @Override + public void setContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().setContentView(view, params); + } + + @Override + public void addContentView(View view, ViewGroup.LayoutParams params) { + getDelegate().addContentView(view, params); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + getDelegate().onPostResume(); + } + + @Override + protected void onTitleChanged(CharSequence title, int color) { + super.onTitleChanged(title, color); + getDelegate().setTitle(title); + } + + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + getDelegate().onConfigurationChanged(newConfig); + } + + @Override + protected void onStop() { + super.onStop(); + getDelegate().onStop(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + getDelegate().onDestroy(); + } + + public void invalidateOptionsMenu() { + getDelegate().invalidateOptionsMenu(); + } + + private AppCompatDelegate getDelegate() { + if (mDelegate == null) { + mDelegate = AppCompatDelegate.create(this, null); + } + return mDelegate; + } +} \ No newline at end of file diff --git a/app/src/test/java/ru/volgorobot/vrcatalog/ExampleUnitTest.java b/app/src/test/java/ru/volgorobot/vrcatalog/ExampleUnitTest.java deleted file mode 100644 index ac47aea..0000000 --- a/app/src/test/java/ru/volgorobot/vrcatalog/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package ru.volgorobot.vrcatalog; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 077cb2f..8d3ef8e 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.4' + classpath 'com.android.tools.build:gradle:3.2.1' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4b3c7b5..2b05c88 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sat Sep 08 16:32:20 MSK 2018 +#Fri Nov 16 14:50:11 MSK 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip From cb094b62a884a0fb0a0325cd2c3fdeaedbdaea29 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 16 Nov 2018 16:50:59 +0400 Subject: [PATCH 02/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B3=D0=BE=20=D1=83=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BD=D1=8F=20=D0=B2=D0=B5=D1=82=D0=B2=D0=B8,=20=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B3=D0=BE,=20=D0=B8=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D1=8C=20=D0=B4=D0=B5=D1=82=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B8=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D0=B8=20Gson,=20Retrofit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes app/build.gradle | 7 +- .../vrcatalog/model/DetailModel.java | 237 ++++++++++++++++++ .../vrcatalog/model/FirstLevelModel.java | 69 +++++ .../vrcatalog/model/SecondLevelModel.java | 67 +++++ 5 files changed, 378 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/model/DetailModel.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 2f6f833776601a3c9893c6a53bcb25efc52fc661..4498d85cdcaf08b0f844b76263cfbfe2a21411aa 100644 GIT binary patch delta 32 qcmV+*0N?+a1epYonFEUJU9p^O0TJ$49iZjXqbkpnr6;ChSrmBfe-5Sq delta 32 ocmbQqGLvP(Oy-zL)*I*KFbdjneYTn&=@#?l|6bju;4rre0L?fJC;$Ke diff --git a/app/build.gradle b/app/build.gradle index 1cd3217..f39c78e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,9 @@ dependencies { implementation 'com.android.support:design:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'com.github.bmelnychuk:atv:1.2.+' + implementation 'com.google.code.gson:gson:2.8.2' + implementation 'com.squareup.retrofit2:retrofit:2.1.0' + implementation 'com.squareup.retrofit2:converter-gson:2.1.0' + implementation 'com.android.support:recyclerview-v7:27.1.1' } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/DetailModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/DetailModel.java new file mode 100644 index 0000000..273be0f --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/DetailModel.java @@ -0,0 +1,237 @@ +package ru.volgorobot.vrcatalog.model; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class DetailModel { + + @SerializedName("ID") + @Expose + private Integer iD; + + @SerializedName("DetalTypeId") + @Expose + private Integer detalTypeId; + + @SerializedName("DetailSubTypeId") + @Expose + private Integer detailSubTypeId; + + @SerializedName("Name") + @Expose + private String name; + + @SerializedName("Number") + @Expose + private Integer number; + + @SerializedName("Place") + @Expose + private String place; + + @SerializedName("Price") + @Expose + private String price; + + @SerializedName("ManufacturerId") + @Expose + private Integer manufacturerId; + + @SerializedName("Country") + @Expose + private String country; + + @SerializedName("IsBU") + @Expose + private Boolean isBU; + + @SerializedName("BodyTypeId") + @Expose + private Integer bodyTypeId; + + @SerializedName("analogue") + @Expose + private String analogue; + + @SerializedName("docURL") + @Expose + private String docURL; + + @SerializedName("EdIzmId") + @Expose + private Integer edIzmId; + + @SerializedName("State") + @Expose + private String state; + + @SerializedName("Notes") + @Expose + private String notes; + + @SerializedName("ID_Shop") + @Expose + private Integer iDShop; + + public DetailModel() { + // Empty constructor + } + + public DetailModel(Integer iD, Integer detalTypeId, Integer detailSubTypeId, String name, Integer number, String place, String price, Integer manufacturerId, String country, Boolean isBU, Integer bodyTypeId, String analogue, String docURL, Integer edIzmId, String state, String notes, Integer iDShop) { + super(); + this.iD = iD; + this.detalTypeId = detalTypeId; + this.detailSubTypeId = detailSubTypeId; + this.name = name; + this.number = number; + this.place = place; + this.price = price; + this.manufacturerId = manufacturerId; + this.country = country; + this.isBU = isBU; + this.bodyTypeId = bodyTypeId; + this.analogue = analogue; + this.docURL = docURL; + this.edIzmId = edIzmId; + this.state = state; + this.notes = notes; + this.iDShop = iDShop; + } + + public Integer getID() { + return iD; + } + + public void setID(Integer iD) { + this.iD = iD; + } + + public Integer getDetalTypeId() { + return detalTypeId; + } + + public void setDetalTypeId(Integer detalTypeId) { + this.detalTypeId = detalTypeId; + } + + public Integer getDetailSubTypeId() { + return detailSubTypeId; + } + + public void setDetailSubTypeId(Integer detailSubTypeId) { + this.detailSubTypeId = detailSubTypeId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + public String getPlace() { + return place; + } + + public void setPlace(String place) { + this.place = place; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + + public Integer getManufacturerId() { + return manufacturerId; + } + + public void setManufacturerId(Integer manufacturerId) { + this.manufacturerId = manufacturerId; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public Boolean getIsBU() { + return isBU; + } + + public void setIsBU(Boolean isBU) { + this.isBU = isBU; + } + + public Integer getBodyTypeId() { + return bodyTypeId; + } + + public void setBodyTypeId(Integer bodyTypeId) { + this.bodyTypeId = bodyTypeId; + } + + public String getAnalogue() { + return analogue; + } + + public void setAnalogue(String analogue) { + this.analogue = analogue; + } + + public String getDocURL() { + return docURL; + } + + public void setDocURL(String docURL) { + this.docURL = docURL; + } + + public Integer getEdIzmId() { + return edIzmId; + } + + public void setEdIzmId(Integer edIzmId) { + this.edIzmId = edIzmId; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getNotes() { + return notes; + } + + public void setNotes(String notes) { + this.notes = notes; + } + + public Integer getIDShop() { + return iDShop; + } + + public void setIDShop(Integer iDShop) { + this.iDShop = iDShop; + } + +} \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java new file mode 100644 index 0000000..32af26a --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java @@ -0,0 +1,69 @@ +package ru.volgorobot.vrcatalog.model; + + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class FirstLevelModel { + + @SerializedName("Description") + @Expose + private Object description; + + @SerializedName("Name") + @Expose + private String name; + + @SerializedName("ID") + @Expose + private Integer iD; + + @SerializedName("Enable") + @Expose + private Boolean enable; + + public FirstLevelModel() { + // Empty constructor + } + + public FirstLevelModel(Object description, String name, Integer iD, Boolean enable) { + super(); + this.description = description; + this.name = name; + this.iD = iD; + this.enable = enable; + } + + public Object getDescription() { + return description; + } + + public void setDescription(Object description) { + this.description = description; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getID() { + return iD; + } + + public void setID(Integer iD) { + this.iD = iD; + } + + public Boolean getEnable() { + return enable; + } + + public void setEnable(Boolean enable) { + this.enable = enable; + } + +} \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java new file mode 100644 index 0000000..eb42937 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java @@ -0,0 +1,67 @@ +package ru.volgorobot.vrcatalog.model; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class SecondLevelModel { + + @SerializedName("ID") + @Expose + private Integer iD; + + @SerializedName("DetailTypeId") + @Expose + private Integer detailTypeId; + + @SerializedName("Description") + @Expose + private Object description; + + @SerializedName("Name") + @Expose + private String name; + + public SecondLevelModel() { + // Empty constructor + } + + public SecondLevelModel(Integer iD, Integer detailTypeId, Object description, String name) { + this.iD = iD; + this.detailTypeId = detailTypeId; + this.description = description; + this.name = name; + } + + public Integer getID() { + return iD; + } + + public void setID(Integer iD) { + this.iD = iD; + } + + public Integer getDetailTypeId() { + return detailTypeId; + } + + public void setDetailTypeId(Integer detailTypeId) { + this.detailTypeId = detailTypeId; + } + + public Object getDescription() { + return description; + } + + public void setDescription(Object description) { + this.description = description; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file From c71c572a1244aa2c477023178b9d8732ff132291 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 16 Nov 2018 16:57:42 +0400 Subject: [PATCH 03/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D1=80=D0=B0=D0=B7=D1=80=D0=B5=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=20=D0=B4=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=83=D0=BF=20=D0=BA=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=BD?= =?UTF-8?q?=D0=B5=D1=82=D1=83.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 564beb7..5a9a658 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + Date: Fri, 16 Nov 2018 17:58:13 +0400 Subject: [PATCH 04/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=84=D1=80=D0=B0=D0=B3=D0=BC=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20TreeView.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../volgorobot/vrcatalog/TreeViewFragment.java | 18 ++++++++++++++++++ app/src/main/res/layout/fragment_default.xml | 6 ++++++ 2 files changed, 24 insertions(+) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java create mode 100644 app/src/main/res/layout/fragment_default.xml diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java b/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java new file mode 100644 index 0000000..5cd39ed --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java @@ -0,0 +1,18 @@ +package ru.volgorobot.vrcatalog; + +import android.app.Fragment; +import android.os.Bundle; +import android.support.annotation.Nullable; + +import com.unnamed.b.atv.model.TreeNode; +import com.unnamed.b.atv.view.AndroidTreeView; + +public class TreeViewFragment extends Fragment { + + private AndroidTreeView treeView; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } +} diff --git a/app/src/main/res/layout/fragment_default.xml b/app/src/main/res/layout/fragment_default.xml new file mode 100644 index 0000000..80aa108 --- /dev/null +++ b/app/src/main/res/layout/fragment_default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file From b9b3bc45ad0b37dcab7b91d300c41043fc552f65 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 16 Nov 2018 21:09:56 +0400 Subject: [PATCH 05/27] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=83=D0=BC=D0=B5=D0=B5=D1=82=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D0=B8=20=D0=BF=D0=B0=D1=80=D1=81=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D1=8B=20=D0=BE?= =?UTF-8?q?=D1=82=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0.=20=D0=9D?= =?UTF-8?q?=D0=B5=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20TreeView=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=BE=D0=B3=D0=BE=20=D1=83=D1=80=D0=BE=D0=B2=D0=BD=D1=8F?= =?UTF-8?q?=20=D0=B2=D0=B5=D1=82=D0=B2=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/volgorobot/vrcatalog/MainActivity.java | 16 +++++ .../vrcatalog/TreeViewFragment.java | 67 +++++++++++++++++++ .../volgorobot/vrcatalog/api/Controller.java | 40 +++++++++++ .../ru/volgorobot/vrcatalog/api/VRApi.java | 13 ++++ app/src/main/res/layout/content_main.xml | 5 ++ app/src/main/res/layout/fragment_default.xml | 10 +-- app/src/main/res/xml/activity_settings.xml | 3 +- 7 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index fd07e6a..8ef3670 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -1,7 +1,12 @@ package ru.volgorobot.vrcatalog; +import android.app.Fragment; +import android.app.FragmentManager; +import android.app.FragmentTransaction; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; @@ -30,6 +35,17 @@ public class MainActivity extends AppCompatActivity NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + String baseURL = sharedPreferences.getString("addressOfServer", ""); + + FragmentManager fragmentManager = getFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + TreeViewFragment treeViewFragment = new TreeViewFragment(); + + Bundle bundle = new Bundle(); + bundle.putString("baseURL", baseURL); + treeViewFragment.setArguments(bundle); + fragmentTransaction.add(R.id.fragment_container, treeViewFragment).commit(); } @Override diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java b/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java index 5cd39ed..3314b52 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java @@ -3,16 +3,83 @@ package ru.volgorobot.vrcatalog; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; import com.unnamed.b.atv.model.TreeNode; import com.unnamed.b.atv.view.AndroidTreeView; +import com.unnamed.b.atv.view.TreeNodeWrapperView; + +import java.util.ArrayList; +import java.util.List; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import ru.volgorobot.vrcatalog.api.Controller; +import ru.volgorobot.vrcatalog.api.VRApi; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; public class TreeViewFragment extends Fragment { private AndroidTreeView treeView; + private VRApi vrApi; + private List firstLevelObjects = new ArrayList<>(); + private List firstLevelNodes = new ArrayList<>(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + + + Bundle bundle = this.getArguments(); + String baseURL = bundle.getString("baseURL"); + vrApi = Controller.getApi(baseURL, getActivity()); + + vrApi.getFirstLevel().enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if(response != null) { + try { + firstLevelObjects.addAll(response.body()); + } catch(Exception e) { + Log.e("ERROR", "Answer of server is wrong!"); + Log.e("ERROR", response.message()); + } + fillFirstLevel(); + } else { + Log.e("ERROR", "Что-то пошло не так..."); + } + + } + + @Override + public void onFailure(Call> call, Throwable t) { + Log.e("ERROR", "getFirstLevel() failed!"); + } + }); + + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + View rootView = inflater.inflate(R.layout.fragment_default, null, false); + TreeNodeWrapperView containerView = rootView.findViewById(R.id.treeViewContainer); + TreeNode root = TreeNode.root(); + root.addChildren(firstLevelNodes); + treeView = new AndroidTreeView(getActivity(), root); + //containerView.insertNodeView(treeView); + return rootView; + } + + private void fillFirstLevel() { + for (int i = 0; i < firstLevelObjects.size(); i++) { + firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); + } } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java b/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java new file mode 100644 index 0000000..953cf47 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java @@ -0,0 +1,40 @@ +package ru.volgorobot.vrcatalog.api; + +import android.content.Context; +import android.widget.Toast; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +public class Controller { + private static String baseURL; + private static Context context; + private static Retrofit retrofit; + + + public static VRApi getApi(String baseURL1, Context context1) { + baseURL = baseURL1; + context = context1; + Gson gson = new GsonBuilder() + .setLenient() + .create(); + + buildRetrofitObject(gson); + + return retrofit.create(VRApi.class); + } + + private static void buildRetrofitObject(Gson gson) { + try { + retrofit = new Retrofit.Builder() + .baseUrl(baseURL) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build(); + } catch(Exception e) { + Toast.makeText(context, "Вы ввели неверный URL. Пример: http://example", Toast.LENGTH_LONG).show(); + } + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java new file mode 100644 index 0000000..8c6cafa --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java @@ -0,0 +1,13 @@ +package ru.volgorobot.vrcatalog.api; + +import java.util.List; + +import retrofit2.Call; +import retrofit2.http.GET; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; + +public interface VRApi { + + @GET("/API/Api.php?action=getFirstLevel") + Call> getFirstLevel(); +} diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index f015a0a..2f290f9 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -8,4 +8,9 @@ tools:context=".MainActivity" tools:showIn="@layout/app_bar_main"> + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_default.xml b/app/src/main/res/layout/fragment_default.xml index 80aa108..5aeaa2a 100644 --- a/app/src/main/res/layout/fragment_default.xml +++ b/app/src/main/res/layout/fragment_default.xml @@ -1,6 +1,6 @@ - - - \ No newline at end of file + + \ No newline at end of file diff --git a/app/src/main/res/xml/activity_settings.xml b/app/src/main/res/xml/activity_settings.xml index 3609123..0cdfaa6 100644 --- a/app/src/main/res/xml/activity_settings.xml +++ b/app/src/main/res/xml/activity_settings.xml @@ -3,5 +3,6 @@ + android:summary="PHP скрипт-посредник необходим для доступа к БД деталей" + android:defaultValue="http://example.com"/> \ No newline at end of file From 489dd898f2c4aaae04b58f0df75b5ed8e3c9a919 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 19 Nov 2018 15:34:56 +0400 Subject: [PATCH 06/27] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=BE?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20"addressOfServer".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/xml/activity_settings.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/xml/activity_settings.xml b/app/src/main/res/xml/activity_settings.xml index 0cdfaa6..a97929d 100644 --- a/app/src/main/res/xml/activity_settings.xml +++ b/app/src/main/res/xml/activity_settings.xml @@ -2,7 +2,7 @@ \ No newline at end of file From 8c8927489db8d9f907affecc84c52f623320df0d Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 19 Nov 2018 19:56:53 +0400 Subject: [PATCH 07/27] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=B7=D0=B0=D1=89=D0=B8=D1=82=D1=83=20=D0=BE=D1=82=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20URL,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=84=D1=80=D0=B0=D0=B3=D0=BC=D0=B5=D0=BD=D1=82=20(?= =?UTF-8?q?=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D1=8D=D1=82=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20TreeView=20=D0=BD=D0=B0=D1=85=D0=BE=D0=B4=D0=B8=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D0=BD=D0=B0=20MainActivity=20(SwipeRefreshLayou?= =?UTF-8?q?t),=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20SwipeRefresh?= =?UTF-8?q?Layout.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes .../ru/volgorobot/vrcatalog/MainActivity.java | 119 +++++++++++++++--- .../vrcatalog/TreeViewFragment.java | 85 ------------- .../volgorobot/vrcatalog/api/Controller.java | 19 +-- app/src/main/res/layout/content_main.xml | 21 +++- app/src/main/res/layout/fragment_default.xml | 6 - 6 files changed, 132 insertions(+), 118 deletions(-) delete mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java delete mode 100644 app/src/main/res/layout/fragment_default.xml diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 4498d85cdcaf08b0f844b76263cfbfe2a21411aa..d7683e43f225d67c8345a4445159f13dd1ab170e 100644 GIT binary patch delta 15 XcmbQqGLvP(3>NMG3uHIWsbvHJFRTU` delta 15 XcmbQqGLvP(3>KNL*RdPt)G`79F9QZ3 diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index 8ef3670..be58598 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -1,8 +1,5 @@ package ru.volgorobot.vrcatalog; -import android.app.Fragment; -import android.app.FragmentManager; -import android.app.FragmentTransaction; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -10,14 +7,36 @@ import android.preference.PreferenceManager; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.MenuItem; -import android.view.Menu; +import android.view.ViewGroup; +import android.widget.Toast; + +import com.unnamed.b.atv.model.TreeNode; +import com.unnamed.b.atv.view.AndroidTreeView; + +import java.util.ArrayList; +import java.util.List; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import ru.volgorobot.vrcatalog.api.Controller; +import ru.volgorobot.vrcatalog.api.VRApi; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; public class MainActivity extends AppCompatActivity - implements NavigationView.OnNavigationItemSelectedListener { + implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { + + private AndroidTreeView treeView; + private VRApi vrApi; + private List firstLevelObjects = new ArrayList<>(); + private List firstLevelNodes = new ArrayList<>(); + private TreeNode root = TreeNode.root(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -35,17 +54,22 @@ public class MainActivity extends AppCompatActivity NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - String baseURL = sharedPreferences.getString("addressOfServer", ""); - FragmentManager fragmentManager = getFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - TreeViewFragment treeViewFragment = new TreeViewFragment(); + SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); + mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); + + treeView = new AndroidTreeView(MainActivity.this, root); + treeView.setDefaultAnimation(true); + ViewGroup containerView = (ViewGroup) findViewById(R.id.treeViewContainer); + containerView.addView(treeView.getView()); + + try{ + refreshTree(); + } catch (IllegalStateException e) { + // + } - Bundle bundle = new Bundle(); - bundle.putString("baseURL", baseURL); - treeViewFragment.setArguments(bundle); - fragmentTransaction.add(R.id.fragment_container, treeViewFragment).commit(); } @Override @@ -86,4 +110,71 @@ public class MainActivity extends AppCompatActivity drawer.closeDrawer(GravityCompat.START); return true; } + + private void fillFirstLevelNodes() { + for (int i = 0; i < firstLevelObjects.size(); i++) { + firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); + } + } + + @Override + public void onRefresh() { + final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); + mSwipeRefreshLayout.setRefreshing(true); + + try { + refreshTree(); + } catch (IllegalStateException e) { + // + } + + + mSwipeRefreshLayout.post(new Runnable() { + @Override + public void run() { + mSwipeRefreshLayout.setRefreshing(false); + } + }); + } + + private void refreshTree() throws IllegalStateException { + + for (int i = 0; i < firstLevelNodes.size(); i++) { + root.deleteChild(firstLevelNodes.get(i)); + firstLevelNodes.remove(i); + } + + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + String baseURL = sharedPreferences.getString("addressOfServer", ""); + + try { + vrApi = Controller.getApi(baseURL, this); + } catch(IllegalStateException e) { + throw new IllegalStateException(); + } + + vrApi.getFirstLevel().enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if(response != null) { + try { + firstLevelObjects.addAll(response.body()); + } catch(Exception e) { + Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); + Log.e("ERROR", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); + } + fillFirstLevelNodes(); + } else { + Log.e("ERROR", "Что-то пошло не так..."); + } + + } + + @Override + public void onFailure(Call> call, Throwable t) { + Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); + Log.e("ERROR", "getFirstLevel() failed!"); + } + }); + } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java b/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java deleted file mode 100644 index 3314b52..0000000 --- a/app/src/main/java/ru/volgorobot/vrcatalog/TreeViewFragment.java +++ /dev/null @@ -1,85 +0,0 @@ -package ru.volgorobot.vrcatalog; - -import android.app.Fragment; -import android.os.Bundle; -import android.support.annotation.Nullable; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Toast; - -import com.unnamed.b.atv.model.TreeNode; -import com.unnamed.b.atv.view.AndroidTreeView; -import com.unnamed.b.atv.view.TreeNodeWrapperView; - -import java.util.ArrayList; -import java.util.List; - -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import ru.volgorobot.vrcatalog.api.Controller; -import ru.volgorobot.vrcatalog.api.VRApi; -import ru.volgorobot.vrcatalog.model.FirstLevelModel; - -public class TreeViewFragment extends Fragment { - - private AndroidTreeView treeView; - private VRApi vrApi; - private List firstLevelObjects = new ArrayList<>(); - private List firstLevelNodes = new ArrayList<>(); - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - - - Bundle bundle = this.getArguments(); - String baseURL = bundle.getString("baseURL"); - vrApi = Controller.getApi(baseURL, getActivity()); - - vrApi.getFirstLevel().enqueue(new Callback>() { - @Override - public void onResponse(Call> call, Response> response) { - if(response != null) { - try { - firstLevelObjects.addAll(response.body()); - } catch(Exception e) { - Log.e("ERROR", "Answer of server is wrong!"); - Log.e("ERROR", response.message()); - } - fillFirstLevel(); - } else { - Log.e("ERROR", "Что-то пошло не так..."); - } - - } - - @Override - public void onFailure(Call> call, Throwable t) { - Log.e("ERROR", "getFirstLevel() failed!"); - } - }); - - } - - @Nullable - @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_default, null, false); - TreeNodeWrapperView containerView = rootView.findViewById(R.id.treeViewContainer); - TreeNode root = TreeNode.root(); - root.addChildren(firstLevelNodes); - treeView = new AndroidTreeView(getActivity(), root); - //containerView.insertNodeView(treeView); - return rootView; - } - - private void fillFirstLevel() { - for (int i = 0; i < firstLevelObjects.size(); i++) { - firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); - } - } -} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java b/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java index 953cf47..317ce52 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java @@ -13,28 +13,33 @@ public class Controller { private static String baseURL; private static Context context; private static Retrofit retrofit; + private static VRApi vrApi; - public static VRApi getApi(String baseURL1, Context context1) { + public static VRApi getApi(String baseURL1, Context context1) throws IllegalStateException { baseURL = baseURL1; context = context1; Gson gson = new GsonBuilder() .setLenient() .create(); - - buildRetrofitObject(gson); - - return retrofit.create(VRApi.class); + try { + buildRetrofitObject(gson); + vrApi = retrofit.create(VRApi.class); + return vrApi; + } catch(IllegalStateException e) { + throw new IllegalStateException(); + } } - private static void buildRetrofitObject(Gson gson) { + private static void buildRetrofitObject(Gson gson) throws IllegalStateException { try { retrofit = new Retrofit.Builder() .baseUrl(baseURL) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); } catch(Exception e) { - Toast.makeText(context, "Вы ввели неверный URL. Пример: http://example", Toast.LENGTH_LONG).show(); + Toast.makeText(context, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show(); + throw new IllegalStateException(); } } } diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 2f290f9..88a331a 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -1,16 +1,25 @@ - + tools:showIn="@layout/app_bar_main" + android:orientation="horizontal" + android:id="@+id/swipeRefreshLayout"> - + android:layout_height="match_parent"> + + + - \ No newline at end of file + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_default.xml b/app/src/main/res/layout/fragment_default.xml deleted file mode 100644 index 5aeaa2a..0000000 --- a/app/src/main/res/layout/fragment_default.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - \ No newline at end of file From 2d823ac11cfd12ae71b2312e0949f02ea287409f Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 20 Nov 2018 14:23:32 +0400 Subject: [PATCH 08/27] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA=D1=83=20=D0=B4=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D0=B0=20(=D0=B8=20=D0=BC=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B8),=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B8=D0=BD=D0=BD=D0=B5=D1=80=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/volgorobot/vrcatalog/MainActivity.java | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index be58598..aeccc82 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -59,17 +59,19 @@ public class MainActivity extends AppCompatActivity mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); - treeView = new AndroidTreeView(MainActivity.this, root); - treeView.setDefaultAnimation(true); - ViewGroup containerView = (ViewGroup) findViewById(R.id.treeViewContainer); - containerView.addView(treeView.getView()); - try{ - refreshTree(); + refreshTree(false); } catch (IllegalStateException e) { // } + root.addChildren(firstLevelNodes); + treeView = new AndroidTreeView(MainActivity.this, root); + treeView.setDefaultAnimation(true); + ViewGroup containerView = (ViewGroup) findViewById(R.id.treeViewContainer); + + containerView.addView(treeView.getView()); + } @Override @@ -111,10 +113,12 @@ public class MainActivity extends AppCompatActivity return true; } - private void fillFirstLevelNodes() { + private void fillRootNode() { for (int i = 0; i < firstLevelObjects.size(); i++) { firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); } + SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); + mSwipeRefreshLayout.setRefreshing(false); } @Override @@ -123,7 +127,7 @@ public class MainActivity extends AppCompatActivity mSwipeRefreshLayout.setRefreshing(true); try { - refreshTree(); + refreshTree(true); } catch (IllegalStateException e) { // } @@ -137,11 +141,21 @@ public class MainActivity extends AppCompatActivity }); } - private void refreshTree() throws IllegalStateException { + private void refreshTree(boolean clean) throws IllegalStateException { - for (int i = 0; i < firstLevelNodes.size(); i++) { - root.deleteChild(firstLevelNodes.get(i)); - firstLevelNodes.remove(i); + SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); + mSwipeRefreshLayout.setRefreshing(true); + if(clean) { + List list = new ArrayList<>(root.getChildren()); + for (int i = 0; i < list.size(); i++) { + root.deleteChild(list.get(i)); + } + for (int i = 0; i < firstLevelNodes.size(); i++) { + firstLevelNodes.remove(i); + } + for (int i = 0; i < firstLevelObjects.size(); i++) { + firstLevelObjects.remove(i); + } } SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -163,7 +177,7 @@ public class MainActivity extends AppCompatActivity Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); Log.e("ERROR", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); } - fillFirstLevelNodes(); + fillRootNode(); } else { Log.e("ERROR", "Что-то пошло не так..."); } From 0279a95ab3e177424945536dad86a46746b93a38 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 20 Nov 2018 15:35:21 +0300 Subject: [PATCH 09/27] Fixed infinity refreshing (when error occured) for SwipeRefreshLayout. --- .idea/caches/build_file_checksums.ser | Bin 537 -> 531 bytes .../ru/volgorobot/vrcatalog/MainActivity.java | 5 ++++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index d7683e43f225d67c8345a4445159f13dd1ab170e..84fb81ffdc8f80a138ab6242b13f24919e462979 100644 GIT binary patch delta 67 zcmV-J0KEU11d{}im;^Bd>(Y^&h?8jnL?pJvDxI;&CMo)L#HqL>i?wtBA}?rfZDlWW ZF)uD>a$#g_WiOM!0UMK70TPqH0b0Sp8p8kp delta 66 zcmV-I0KNZ{1epYom;@{TpdFE%hz}?)Xm4$0FJowOZ*FgMc$0$xHj^X)L=z+q8XH1f Y#TivHn_oTyB{_5e5tEDo8 list = new ArrayList<>(root.getChildren()); @@ -176,10 +176,12 @@ public class MainActivity extends AppCompatActivity } catch(Exception e) { Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); Log.e("ERROR", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); + mSwipeRefreshLayout.setRefreshing(false); } fillRootNode(); } else { Log.e("ERROR", "Что-то пошло не так..."); + mSwipeRefreshLayout.setRefreshing(false); } } @@ -188,6 +190,7 @@ public class MainActivity extends AppCompatActivity public void onFailure(Call> call, Throwable t) { Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); Log.e("ERROR", "getFirstLevel() failed!"); + mSwipeRefreshLayout.setRefreshing(false); } }); } From f5952126927f162da93f92e403344d78079361b1 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 20 Nov 2018 15:38:10 +0300 Subject: [PATCH 10/27] Fixed tag of log, added log message for network error. --- app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index 563df58..c964d8f 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -175,12 +175,12 @@ public class MainActivity extends AppCompatActivity firstLevelObjects.addAll(response.body()); } catch(Exception e) { Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); - Log.e("ERROR", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); + Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); mSwipeRefreshLayout.setRefreshing(false); } fillRootNode(); } else { - Log.e("ERROR", "Что-то пошло не так..."); + Log.e("VRCatalog", "Что-то пошло не так..."); mSwipeRefreshLayout.setRefreshing(false); } @@ -189,7 +189,7 @@ public class MainActivity extends AppCompatActivity @Override public void onFailure(Call> call, Throwable t) { Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); - Log.e("ERROR", "getFirstLevel() failed!"); + Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings!"); mSwipeRefreshLayout.setRefreshing(false); } }); From 4833269e0ffc9e2a9391548e12769152c1166c75 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 22 Nov 2018 16:41:49 +0300 Subject: [PATCH 11/27] Made refresh tree view. --- .idea/caches/build_file_checksums.ser | Bin 531 -> 531 bytes .idea/misc.xml | 2 +- .../ru/volgorobot/vrcatalog/MainActivity.java | 24 ++++++------------ 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 84fb81ffdc8f80a138ab6242b13f24919e462979..104abea78f7d9bda2d7ee9eba7b650ed766d6b0c 100644 GIT binary patch delta 15 WcmbQtGMQz<3>NFeMyHK)${7JGB?W%~ delta 15 XcmbQtGMQz<3>HI{*OxZVDQ5%#EtUon diff --git a/.idea/misc.xml b/.idea/misc.xml index e0d5b93..b0c7b20 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index c964d8f..40e2140 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -32,11 +32,9 @@ import ru.volgorobot.vrcatalog.model.FirstLevelModel; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { - private AndroidTreeView treeView; private VRApi vrApi; private List firstLevelObjects = new ArrayList<>(); private List firstLevelNodes = new ArrayList<>(); - private TreeNode root = TreeNode.root(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -64,14 +62,6 @@ public class MainActivity extends AppCompatActivity } catch (IllegalStateException e) { // } - - root.addChildren(firstLevelNodes); - treeView = new AndroidTreeView(MainActivity.this, root); - treeView.setDefaultAnimation(true); - ViewGroup containerView = (ViewGroup) findViewById(R.id.treeViewContainer); - - containerView.addView(treeView.getView()); - } @Override @@ -117,8 +107,14 @@ public class MainActivity extends AppCompatActivity for (int i = 0; i < firstLevelObjects.size(); i++) { firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); } - SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); - mSwipeRefreshLayout.setRefreshing(false); + + TreeNode root = TreeNode.root(); + root.addChildren(firstLevelNodes); + AndroidTreeView treeView = new AndroidTreeView(MainActivity.this, root); + treeView.setDefaultAnimation(true); + treeView.setDefaultContainerStyle(R.style.TreeNodeStyle); + ViewGroup containerView = findViewById(R.id.treeViewContainer); + containerView.addView(treeView.getView()); } @Override @@ -146,10 +142,6 @@ public class MainActivity extends AppCompatActivity final SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); mSwipeRefreshLayout.setRefreshing(true); if(clean) { - List list = new ArrayList<>(root.getChildren()); - for (int i = 0; i < list.size(); i++) { - root.deleteChild(list.get(i)); - } for (int i = 0; i < firstLevelNodes.size(); i++) { firstLevelNodes.remove(i); } From 79a32d590baf6e178b4c2a201c2be1e1e991f4e5 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 12:33:25 +0400 Subject: [PATCH 12/27] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=B5=D1=80=D0=B5=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/build_file_checksums.ser | Bin 531 -> 537 bytes .idea/misc.xml | 31 +----------------- .../ru/volgorobot/vrcatalog/MainActivity.java | 17 +++++----- 3 files changed, 9 insertions(+), 39 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 104abea78f7d9bda2d7ee9eba7b650ed766d6b0c..b985abcbd5df558021ad0c25cadbc739c643f094 100644 GIT binary patch delta 67 zcmbQtGLvP(bk+=^>wLl!=XCIE=x5~Trs^kW6y@jT7gtPfW;C0u!ssHb!Y3u|6mwKE X)M$3R9kZ%s34`F|9!BZOR~hX9?*kP8 delta 68 zcmV-K0K5O01d{}imj!1#VSz%CoQRWY0YoIW#44S!$R;WJb;PN-B#X6l03t7FZ*65S ab1^S2XL4a=Y-KN#zyTYRR{;`}zX4jjUK*DG diff --git a/.idea/misc.xml b/.idea/misc.xml index b0c7b20..37a7509 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,35 +1,6 @@ - - - - - + diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index 40e2140..5213dcb 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -108,12 +108,13 @@ public class MainActivity extends AppCompatActivity firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); } + ViewGroup containerView = findViewById(R.id.treeViewContainer); + containerView.removeAllViews(); TreeNode root = TreeNode.root(); root.addChildren(firstLevelNodes); AndroidTreeView treeView = new AndroidTreeView(MainActivity.this, root); treeView.setDefaultAnimation(true); treeView.setDefaultContainerStyle(R.style.TreeNodeStyle); - ViewGroup containerView = findViewById(R.id.treeViewContainer); containerView.addView(treeView.getView()); } @@ -127,14 +128,6 @@ public class MainActivity extends AppCompatActivity } catch (IllegalStateException e) { // } - - - mSwipeRefreshLayout.post(new Runnable() { - @Override - public void run() { - mSwipeRefreshLayout.setRefreshing(false); - } - }); } private void refreshTree(boolean clean) throws IllegalStateException { @@ -165,6 +158,12 @@ public class MainActivity extends AppCompatActivity if(response != null) { try { firstLevelObjects.addAll(response.body()); + mSwipeRefreshLayout.post(new Runnable() { + @Override + public void run() { + mSwipeRefreshLayout.setRefreshing(false); + } + }); } catch(Exception e) { Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); From c09373514ba9d7ef5f4d96ffcb1fe439716e02d9 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 14:17:11 +0400 Subject: [PATCH 13/27] Changed library for TreeView. Now TreeView works!!! --- .idea/assetWizardSettings.xml | 46 ++++++++++++++++++ .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes .idea/misc.xml | 29 +++++++++++ app/build.gradle | 3 +- .../ru/volgorobot/vrcatalog/MainActivity.java | 32 ++++++------ .../vrcatalog/additional/NodeViewFactory.java | 25 ++++++++++ .../FirstLevelNodeViewBinder.java | 39 +++++++++++++++ .../SecondLevelNodeViewBinder.java | 40 +++++++++++++++ .../ThirdLevelNodeViewBinder.java | 27 ++++++++++ .../ic_keyboard_arrow_right_black_24dp.xml | 9 ++++ app/src/main/res/layout/first_level_item.xml | 30 ++++++++++++ app/src/main/res/layout/second_level_item.xml | 31 ++++++++++++ app/src/main/res/layout/third_level_item.xml | 26 ++++++++++ gradle/wrapper/gradle-wrapper.properties | 1 - 14 files changed, 320 insertions(+), 18 deletions(-) create mode 100644 .idea/assetWizardSettings.xml create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java create mode 100644 app/src/main/res/drawable/ic_keyboard_arrow_right_black_24dp.xml create mode 100644 app/src/main/res/layout/first_level_item.xml create mode 100644 app/src/main/res/layout/second_level_item.xml create mode 100644 app/src/main/res/layout/third_level_item.xml diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml new file mode 100644 index 0000000..e64e3f9 --- /dev/null +++ b/.idea/assetWizardSettings.xml @@ -0,0 +1,46 @@ + + + + + + \ No newline at end of file diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index b985abcbd5df558021ad0c25cadbc739c643f094..ef96a8505cdab6aa364ad4da6e4cda1a42b9a674 100644 GIT binary patch delta 54 zcmV-60LlNE1epYom;@6hHx;p*5djc0+iAk71Lag=XIzr=UEna2y#Y%Re$7=;BY^L} MrTb=Fv55L!ctE!o>Hq)$ delta 54 zcmV-60LlNE1epYom;@5n4imAQ5djb+4jLOmT*VnxGMisM10^|=y#Y%R?pPh5< + + + + diff --git a/app/build.gradle b/app/build.gradle index f39c78e..3d8f62f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,8 +24,7 @@ dependencies { implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' - testImplementation 'junit:junit:4.12' - implementation 'com.github.bmelnychuk:atv:1.2.+' + implementation 'me.texy.treeview:treeview_lib:1.0.4' implementation 'com.google.code.gson:gson:2.8.2' implementation 'com.squareup.retrofit2:retrofit:2.1.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index 5213dcb..1f71f27 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -13,18 +13,19 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.MenuItem; +import android.view.View; import android.view.ViewGroup; import android.widget.Toast; -import com.unnamed.b.atv.model.TreeNode; -import com.unnamed.b.atv.view.AndroidTreeView; - import java.util.ArrayList; import java.util.List; +import me.texy.treeview.TreeNode; +import me.texy.treeview.TreeView; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; +import ru.volgorobot.vrcatalog.additional.NodeViewFactory; import ru.volgorobot.vrcatalog.api.Controller; import ru.volgorobot.vrcatalog.api.VRApi; import ru.volgorobot.vrcatalog.model.FirstLevelModel; @@ -32,9 +33,11 @@ import ru.volgorobot.vrcatalog.model.FirstLevelModel; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { + private TreeView treeView; private VRApi vrApi; private List firstLevelObjects = new ArrayList<>(); private List firstLevelNodes = new ArrayList<>(); + private TreeNode rootNode = TreeNode.root(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,11 +60,15 @@ public class MainActivity extends AppCompatActivity mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); + + try{ - refreshTree(false); + getTreeData(false); } catch (IllegalStateException e) { // } + treeView = new TreeView(rootNode, MainActivity.this, new NodeViewFactory()); + ((ViewGroup) findViewById(R.id.treeViewContainer)).addView(treeView.getView()); } @Override @@ -106,16 +113,10 @@ public class MainActivity extends AppCompatActivity private void fillRootNode() { for (int i = 0; i < firstLevelObjects.size(); i++) { firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); + firstLevelNodes.get(i).setLevel(0); } - ViewGroup containerView = findViewById(R.id.treeViewContainer); - containerView.removeAllViews(); - TreeNode root = TreeNode.root(); - root.addChildren(firstLevelNodes); - AndroidTreeView treeView = new AndroidTreeView(MainActivity.this, root); - treeView.setDefaultAnimation(true); - treeView.setDefaultContainerStyle(R.style.TreeNodeStyle); - containerView.addView(treeView.getView()); + rootNode.setChildren(firstLevelNodes); } @Override @@ -124,13 +125,13 @@ public class MainActivity extends AppCompatActivity mSwipeRefreshLayout.setRefreshing(true); try { - refreshTree(true); + getTreeData(true); } catch (IllegalStateException e) { // } } - private void refreshTree(boolean clean) throws IllegalStateException { + private void getTreeData(boolean clean) throws IllegalStateException { final SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); mSwipeRefreshLayout.setRefreshing(true); @@ -170,6 +171,7 @@ public class MainActivity extends AppCompatActivity mSwipeRefreshLayout.setRefreshing(false); } fillRootNode(); + treeView.refreshTreeView(); } else { Log.e("VRCatalog", "Что-то пошло не так..."); mSwipeRefreshLayout.setRefreshing(false); @@ -180,7 +182,7 @@ public class MainActivity extends AppCompatActivity @Override public void onFailure(Call> call, Throwable t) { Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings!"); + Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical information: " + t.toString()); mSwipeRefreshLayout.setRefreshing(false); } }); diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java new file mode 100644 index 0000000..caba708 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java @@ -0,0 +1,25 @@ +package ru.volgorobot.vrcatalog.additional; + +import android.view.View; + +import me.texy.treeview.base.BaseNodeViewBinder; +import me.texy.treeview.base.BaseNodeViewFactory; +import ru.volgorobot.vrcatalog.nodeViewBinders.FirstLevelNodeViewBinder; +import ru.volgorobot.vrcatalog.nodeViewBinders.SecondLevelNodeViewBinder; +import ru.volgorobot.vrcatalog.nodeViewBinders.ThirdLevelNodeViewBinder; + +public class NodeViewFactory extends BaseNodeViewFactory { + @Override + public BaseNodeViewBinder getNodeViewBinder(View view, int level) { + switch (level) { + case 0: + return new FirstLevelNodeViewBinder(view); + case 1: + return new SecondLevelNodeViewBinder(view); + case 2: + return new ThirdLevelNodeViewBinder(view); + default: + return null; + } + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java new file mode 100644 index 0000000..edc69ec --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -0,0 +1,39 @@ +package ru.volgorobot.vrcatalog.nodeViewBinders; + +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import me.texy.treeview.TreeNode; +import me.texy.treeview.base.BaseNodeViewBinder; +import ru.volgorobot.vrcatalog.R; + +public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { + TextView textView; + ImageView imageView; + + public FirstLevelNodeViewBinder(View itemView) { + super(itemView); + textView = (TextView) itemView.findViewById(R.id.node_name_view); + imageView = (ImageView) itemView.findViewById(R.id.arrow_img); + } + + @Override + public int getLayoutId() { + return R.layout.first_level_item; + } + + @Override + public void bindView(TreeNode treeNode) { + textView.setText(treeNode.getValue().toString()); + } + + @Override + public void onNodeToggled(TreeNode treeNode, boolean expand) { + if (expand) { + imageView.animate().rotation(90).setDuration(200).start(); + } else { + imageView.animate().rotation(0).setDuration(200).start(); + } + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java new file mode 100644 index 0000000..2b55270 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -0,0 +1,40 @@ +package ru.volgorobot.vrcatalog.nodeViewBinders; + +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import me.texy.treeview.TreeNode; +import me.texy.treeview.base.BaseNodeViewBinder; +import ru.volgorobot.vrcatalog.R; + +public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { + TextView textView; + ImageView imageView; + + public SecondLevelNodeViewBinder(View itemView) { + super(itemView); + textView = (TextView) itemView.findViewById(R.id.node_name_view); + imageView = (ImageView) itemView.findViewById(R.id.arrow_img); + } + + @Override + public int getLayoutId() { + return R.layout.second_level_item; + } + + @Override + public void bindView(final TreeNode treeNode) { + textView.setText(treeNode.getValue().toString()); + imageView.setRotation(treeNode.isExpanded() ? 90 : 0); + } + + @Override + public void onNodeToggled(TreeNode treeNode, boolean expand) { + if (expand) { + imageView.animate().rotation(90).setDuration(200).start(); + } else { + imageView.animate().rotation(0).setDuration(200).start(); + } + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java new file mode 100644 index 0000000..238cabe --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java @@ -0,0 +1,27 @@ +package ru.volgorobot.vrcatalog.nodeViewBinders; + +import android.view.View; +import android.widget.TextView; + +import me.texy.treeview.TreeNode; +import me.texy.treeview.base.BaseNodeViewBinder; +import ru.volgorobot.vrcatalog.R; + +public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder { + TextView textView; + + public ThirdLevelNodeViewBinder(View itemView) { + super(itemView); + textView = (TextView) itemView.findViewById(R.id.node_name_view); + } + + @Override + public int getLayoutId() { + return R.layout.third_level_item; + } + + @Override + public void bindView(final TreeNode treeNode) { + textView.setText(treeNode.getValue().toString()); + } +} diff --git a/app/src/main/res/drawable/ic_keyboard_arrow_right_black_24dp.xml b/app/src/main/res/drawable/ic_keyboard_arrow_right_black_24dp.xml new file mode 100644 index 0000000..a3d1622 --- /dev/null +++ b/app/src/main/res/drawable/ic_keyboard_arrow_right_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/first_level_item.xml b/app/src/main/res/layout/first_level_item.xml new file mode 100644 index 0000000..f53dc6d --- /dev/null +++ b/app/src/main/res/layout/first_level_item.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/second_level_item.xml b/app/src/main/res/layout/second_level_item.xml new file mode 100644 index 0000000..f2d1388 --- /dev/null +++ b/app/src/main/res/layout/second_level_item.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/third_level_item.xml b/app/src/main/res/layout/third_level_item.xml new file mode 100644 index 0000000..879bd0e --- /dev/null +++ b/app/src/main/res/layout/third_level_item.xml @@ -0,0 +1,26 @@ + + + + + + + + + + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2b05c88..3f0f9be 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,3 @@ -#Fri Nov 16 14:50:11 MSK 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME From 3b46a96b0d5d15156b167390074a878e3ad1f99e Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 16:27:45 +0400 Subject: [PATCH 14/27] Made getting subcategories. --- .../ru/volgorobot/vrcatalog/MainActivity.java | 103 ++++++++++++------ .../ru/volgorobot/vrcatalog/api/VRApi.java | 5 + 2 files changed, 75 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index 1f71f27..c43d822 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -1,7 +1,9 @@ package ru.volgorobot.vrcatalog; +import android.content.AsyncQueryHandler; import android.content.Intent; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.design.widget.NavigationView; @@ -17,6 +19,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Toast; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -29,6 +32,7 @@ import ru.volgorobot.vrcatalog.additional.NodeViewFactory; import ru.volgorobot.vrcatalog.api.Controller; import ru.volgorobot.vrcatalog.api.VRApi; import ru.volgorobot.vrcatalog.model.FirstLevelModel; +import ru.volgorobot.vrcatalog.model.SecondLevelModel; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { @@ -37,7 +41,9 @@ public class MainActivity extends AppCompatActivity private VRApi vrApi; private List firstLevelObjects = new ArrayList<>(); private List firstLevelNodes = new ArrayList<>(); + private List secondLevelObjects = new ArrayList<>(); private TreeNode rootNode = TreeNode.root(); + private SwipeRefreshLayout mSwipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,13 +62,11 @@ public class MainActivity extends AppCompatActivity NavigationView navigationView = findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); - SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); + mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); - - - try{ + try { getTreeData(false); } catch (IllegalStateException e) { // @@ -114,14 +118,29 @@ public class MainActivity extends AppCompatActivity for (int i = 0; i < firstLevelObjects.size(); i++) { firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); firstLevelNodes.get(i).setLevel(0); + firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID())); } rootNode.setChildren(firstLevelNodes); } + private ArrayList getSecondLevelNodesByParentID(int parentTypeID) { + ArrayList objects = new ArrayList<>(); + for (int i = 0; i < secondLevelObjects.size(); i++) { + if(secondLevelObjects.get(i).getDetailTypeId() == parentTypeID) { + objects.add(secondLevelObjects.get(i)); + } + } + ArrayList nodes = new ArrayList<>(); + for (int i = 0; i < objects.size(); i++) { + nodes.add(new TreeNode(objects.get(i).getName())); + nodes.get(i).setLevel(1); + } + return nodes; + } + @Override public void onRefresh() { - final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); mSwipeRefreshLayout.setRefreshing(true); try { @@ -131,9 +150,7 @@ public class MainActivity extends AppCompatActivity } } - private void getTreeData(boolean clean) throws IllegalStateException { - - final SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); + private void getTreeData(final boolean clean) throws IllegalStateException { mSwipeRefreshLayout.setRefreshing(true); if(clean) { for (int i = 0; i < firstLevelNodes.size(); i++) { @@ -142,6 +159,9 @@ public class MainActivity extends AppCompatActivity for (int i = 0; i < firstLevelObjects.size(); i++) { firstLevelObjects.remove(i); } + for (int i = 0; i < secondLevelObjects.size(); i++) { + secondLevelObjects.remove(i); + } } SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -152,39 +172,56 @@ public class MainActivity extends AppCompatActivity } catch(IllegalStateException e) { throw new IllegalStateException(); } - - vrApi.getFirstLevel().enqueue(new Callback>() { + class GetTreeData extends AsyncTask { @Override - public void onResponse(Call> call, Response> response) { - if(response != null) { - try { - firstLevelObjects.addAll(response.body()); - mSwipeRefreshLayout.post(new Runnable() { - @Override - public void run() { - mSwipeRefreshLayout.setRefreshing(false); + protected Void doInBackground(Void... voids) { + try { + Response> firstLevelResponse = vrApi.getFirstLevel().execute(); + if(firstLevelResponse.isSuccessful()) { + firstLevelObjects.addAll(firstLevelResponse.body()); + for (int i = 0; i < firstLevelObjects.size(); i++) { + Response> secondLevelResponse = vrApi.getSecondLevel(firstLevelObjects.get(i).getID()).execute(); + if(secondLevelResponse.isSuccessful()) { + secondLevelObjects.addAll(secondLevelResponse.body()); + } else { + onFailureAnswer(1); } - }); - } catch(Exception e) { - Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); - mSwipeRefreshLayout.setRefreshing(false); + } } - fillRootNode(); - treeView.refreshTreeView(); - } else { - Log.e("VRCatalog", "Что-то пошло не так..."); - mSwipeRefreshLayout.setRefreshing(false); + } catch (Exception e) { + onFailureAnswer(0); } - + return null; } @Override - public void onFailure(Call> call, Throwable t) { - Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical information: " + t.toString()); + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + fillRootNode(); + treeView.refreshTreeView(); mSwipeRefreshLayout.setRefreshing(false); } - }); + } + new GetTreeData().execute(); } + + private void onFailureAnswer(int errorCode) { + switch (errorCode) { + case 0: { + Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); + Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings!"); + mSwipeRefreshLayout.setRefreshing(false); + break; + } + case 1: { + Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); + Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials!"); + mSwipeRefreshLayout.setRefreshing(false); + break; + } + } + } + } + + 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 8c6cafa..5a6543a 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java @@ -4,10 +4,15 @@ import java.util.List; import retrofit2.Call; import retrofit2.http.GET; +import retrofit2.http.Query; import ru.volgorobot.vrcatalog.model.FirstLevelModel; +import ru.volgorobot.vrcatalog.model.SecondLevelModel; public interface VRApi { @GET("/API/Api.php?action=getFirstLevel") Call> getFirstLevel(); + + @GET("/API/Api.php?action=getSecondLevel") + Call> getSecondLevel(@Query("parentTypeID") int parentTypeID); } From d1cc2cc3dcedef9a4ce4ff480d580fa715c1288e Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 16:36:58 +0400 Subject: [PATCH 15/27] Removed extra LinearView in ScrollView --- app/src/main/res/layout/content_main.xml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 88a331a..556a29d 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -11,14 +11,10 @@ android:id="@+id/swipeRefreshLayout"> - - + android:layout_height="match_parent" + android:scrollbarAlwaysDrawVerticalTrack="true"> From dc01b7960b8bdc9e3fd2bba5ad0618ddb70314c6 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 21:25:34 +0400 Subject: [PATCH 16/27] Added third level loading. --- .../ru/volgorobot/vrcatalog/MainActivity.java | 70 +++++++++++++++---- .../ru/volgorobot/vrcatalog/api/VRApi.java | 4 ++ .../vrcatalog/model/FirstLevelModel.java | 28 +------- .../vrcatalog/model/SecondLevelModel.java | 15 +--- .../vrcatalog/model/ThirdLevelModel.java | 62 ++++++++++++++++ 5 files changed, 123 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index c43d822..a287510 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -33,6 +33,7 @@ import ru.volgorobot.vrcatalog.api.Controller; import ru.volgorobot.vrcatalog.api.VRApi; import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { @@ -42,6 +43,8 @@ public class MainActivity extends AppCompatActivity private List firstLevelObjects = new ArrayList<>(); private List firstLevelNodes = new ArrayList<>(); private List secondLevelObjects = new ArrayList<>(); + private List thirdLevelObjects = new ArrayList<>(); + private TreeNode rootNode = TreeNode.root(); private SwipeRefreshLayout mSwipeRefreshLayout; @@ -119,6 +122,9 @@ public class MainActivity extends AppCompatActivity firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); firstLevelNodes.get(i).setLevel(0); firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID())); + for (int j = 0; j < firstLevelNodes.get(i).getChildren().size(); j++) { + firstLevelNodes.get(i).getChildren().get(j).setChildren(getThirdLevelNodesByParentName((String) firstLevelNodes.get(i).getChildren().get(j).getValue())); + } } rootNode.setChildren(firstLevelNodes); @@ -139,6 +145,21 @@ public class MainActivity extends AppCompatActivity return nodes; } + private ArrayList getThirdLevelNodesByParentName(String parentName) { + ArrayList objects = new ArrayList<>(); + for (int i = 0; i < thirdLevelObjects.size(); i++) { + if(thirdLevelObjects.get(i).getName().equals(parentName)) { + objects.add(thirdLevelObjects.get(i)); + } + } + ArrayList nodes = new ArrayList<>(); + for (int i = 0; i < objects.size(); i++) { + nodes.add(new TreeNode(objects.get(i).getName())); + nodes.get(i).setLevel(2); + } + return nodes; + } + @Override public void onRefresh() { mSwipeRefreshLayout.setRefreshing(true); @@ -162,8 +183,10 @@ public class MainActivity extends AppCompatActivity for (int i = 0; i < secondLevelObjects.size(); i++) { secondLevelObjects.remove(i); } + for (int i = 0; i < thirdLevelObjects.size(); i++) { + thirdLevelObjects.remove(i); + } } - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String baseURL = sharedPreferences.getString("addressOfServer", ""); @@ -172,9 +195,9 @@ public class MainActivity extends AppCompatActivity } catch(IllegalStateException e) { throw new IllegalStateException(); } - class GetTreeData extends AsyncTask { + class GetTreeData extends AsyncTask { @Override - protected Void doInBackground(Void... voids) { + protected Integer doInBackground(Void... voids) { try { Response> firstLevelResponse = vrApi.getFirstLevel().execute(); if(firstLevelResponse.isSuccessful()) { @@ -184,22 +207,39 @@ public class MainActivity extends AppCompatActivity if(secondLevelResponse.isSuccessful()) { secondLevelObjects.addAll(secondLevelResponse.body()); } else { - onFailureAnswer(1); + return 2; } } + for (int i = 0; i < secondLevelObjects.size(); i++) { + Response> thirdLevelResponse = vrApi.getDetails(secondLevelObjects.get(i).getID()).execute(); + if(thirdLevelResponse.isSuccessful()) { + thirdLevelObjects.addAll(thirdLevelResponse.body()); + } else { + return 2; + } + } + } else { + return 2; } } catch (Exception e) { - onFailureAnswer(0); + return 1; } - return null; + return 0; } @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - fillRootNode(); - treeView.refreshTreeView(); - mSwipeRefreshLayout.setRefreshing(false); + protected void onPostExecute(Integer integer) { + super.onPostExecute(integer); + if(integer == 0) { + fillRootNode(); + treeView.refreshTreeView(); + mSwipeRefreshLayout.setRefreshing(false); + } else if(integer == 1) { + onFailureAnswer(1); + } else if(integer == 2) { + onFailureAnswer(2); + } + } } new GetTreeData().execute(); @@ -207,15 +247,15 @@ public class MainActivity extends AppCompatActivity private void onFailureAnswer(int errorCode) { switch (errorCode) { - case 0: { + case 1: { Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings!"); + Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical info: null"); mSwipeRefreshLayout.setRefreshing(false); break; } - case 1: { + case 2: { Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials!"); + Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Technical info: null"); mSwipeRefreshLayout.setRefreshing(false); break; } 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 5a6543a..8d6f164 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java @@ -7,6 +7,7 @@ import retrofit2.http.GET; import retrofit2.http.Query; import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; public interface VRApi { @@ -15,4 +16,7 @@ public interface VRApi { @GET("/API/Api.php?action=getSecondLevel") Call> getSecondLevel(@Query("parentTypeID") int parentTypeID); + + @GET("/API/Api.php?action=getDetailsByParentID") + Call> getDetails(@Query("parentID") int parentID); } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java index 32af26a..b5579b4 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java @@ -6,10 +6,6 @@ import com.google.gson.annotations.SerializedName; public class FirstLevelModel { - @SerializedName("Description") - @Expose - private Object description; - @SerializedName("Name") @Expose private String name; @@ -18,28 +14,14 @@ public class FirstLevelModel { @Expose private Integer iD; - @SerializedName("Enable") - @Expose - private Boolean enable; public FirstLevelModel() { // Empty constructor } - public FirstLevelModel(Object description, String name, Integer iD, Boolean enable) { - super(); - this.description = description; + public FirstLevelModel(String name, Integer iD) { this.name = name; this.iD = iD; - this.enable = enable; - } - - public Object getDescription() { - return description; - } - - public void setDescription(Object description) { - this.description = description; } public String getName() { @@ -58,12 +40,4 @@ public class FirstLevelModel { this.iD = iD; } - public Boolean getEnable() { - return enable; - } - - public void setEnable(Boolean enable) { - this.enable = enable; - } - } \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java index eb42937..c002540 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java @@ -13,10 +13,6 @@ public class SecondLevelModel { @Expose private Integer detailTypeId; - @SerializedName("Description") - @Expose - private Object description; - @SerializedName("Name") @Expose private String name; @@ -25,10 +21,9 @@ public class SecondLevelModel { // Empty constructor } - public SecondLevelModel(Integer iD, Integer detailTypeId, Object description, String name) { + public SecondLevelModel(Integer iD, Integer detailTypeId, String name) { this.iD = iD; this.detailTypeId = detailTypeId; - this.description = description; this.name = name; } @@ -48,14 +43,6 @@ public class SecondLevelModel { this.detailTypeId = detailTypeId; } - public Object getDescription() { - return description; - } - - public void setDescription(Object description) { - this.description = description; - } - public String getName() { return name; } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java new file mode 100644 index 0000000..11c2c7d --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java @@ -0,0 +1,62 @@ +package ru.volgorobot.vrcatalog.model; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class ThirdLevelModel { + + @SerializedName("ID") + @Expose + private Integer iD; + @SerializedName("DetailSubTypeId") + @Expose + private Integer detailSubTypeId; + @SerializedName("Name") + @Expose + private String name; + + /** + * No args constructor for use in serialization + * + */ + public ThirdLevelModel() { + } + + /** + * + * @param detailSubTypeId Parent category + * @param name Name of detail + * @param iD ID of detail + */ + public ThirdLevelModel(Integer iD, Integer detailSubTypeId, String name) { + super(); + this.iD = iD; + this.detailSubTypeId = detailSubTypeId; + this.name = name; + } + + public Integer getID() { + return iD; + } + + public void setID(Integer iD) { + this.iD = iD; + } + + public Integer getDetailSubTypeId() { + return detailSubTypeId; + } + + public void setDetailSubTypeId(Integer detailSubTypeId) { + this.detailSubTypeId = detailSubTypeId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file From bb349e6b83f27e5a3e560668c72b71b8ce26c309 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 22:01:22 +0400 Subject: [PATCH 17/27] Removed loading third level (in future it's will be implemented) and fixed duplication of tree nodes. --- .../ru/volgorobot/vrcatalog/MainActivity.java | 26 +++---------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index a287510..7ff9c58 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -122,9 +122,6 @@ public class MainActivity extends AppCompatActivity firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); firstLevelNodes.get(i).setLevel(0); firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID())); - for (int j = 0; j < firstLevelNodes.get(i).getChildren().size(); j++) { - firstLevelNodes.get(i).getChildren().get(j).setChildren(getThirdLevelNodesByParentName((String) firstLevelNodes.get(i).getChildren().get(j).getValue())); - } } rootNode.setChildren(firstLevelNodes); @@ -174,18 +171,9 @@ public class MainActivity extends AppCompatActivity private void getTreeData(final boolean clean) throws IllegalStateException { mSwipeRefreshLayout.setRefreshing(true); if(clean) { - for (int i = 0; i < firstLevelNodes.size(); i++) { - firstLevelNodes.remove(i); - } - for (int i = 0; i < firstLevelObjects.size(); i++) { - firstLevelObjects.remove(i); - } - for (int i = 0; i < secondLevelObjects.size(); i++) { - secondLevelObjects.remove(i); - } - for (int i = 0; i < thirdLevelObjects.size(); i++) { - thirdLevelObjects.remove(i); - } + firstLevelNodes.removeAll(firstLevelNodes); + firstLevelObjects.removeAll(firstLevelObjects); + secondLevelObjects.removeAll(secondLevelObjects); } SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String baseURL = sharedPreferences.getString("addressOfServer", ""); @@ -210,14 +198,6 @@ public class MainActivity extends AppCompatActivity return 2; } } - for (int i = 0; i < secondLevelObjects.size(); i++) { - Response> thirdLevelResponse = vrApi.getDetails(secondLevelObjects.get(i).getID()).execute(); - if(thirdLevelResponse.isSuccessful()) { - thirdLevelObjects.addAll(thirdLevelResponse.body()); - } else { - return 2; - } - } } else { return 2; } From 001889aa1481f15dd0d80feaef89d9aa54d362cc Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 4 Jan 2019 21:19:13 +0400 Subject: [PATCH 18/27] The application architecture was refactored (the application now uses the MVP architectural pattern), and dynamic loading of all levels of the tree is implemented. --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes .idea/misc.xml | 2 +- app/build.gradle | 5 + app/src/main/AndroidManifest.xml | 6 +- .../ru/volgorobot/vrcatalog/MainActivity.java | 247 ------------------ .../ru/volgorobot/vrcatalog/MainContract.java | 32 +++ .../volgorobot/vrcatalog/MainPresenter.java | 59 +++++ .../vrcatalog/additional/NodeViewFactory.java | 14 +- .../volgorobot/vrcatalog/api/Controller.java | 5 +- .../volgorobot/vrcatalog/model/CoreModel.java | 80 ++++++ .../vrcatalog/model/FirstLevelModel.java | 3 + .../vrcatalog/model/SecondLevelModel.java | 3 + .../vrcatalog/model/ThirdLevelModel.java | 3 + .../FirstLevelNodeViewBinder.java | 47 +++- .../SecondLevelNodeViewBinder.java | 48 +++- .../vrcatalog/{ => view}/AboutActivity.java | 4 +- .../vrcatalog/view/MainActivity.java | 156 +++++++++++ .../{ => view}/SettingsActivity.java | 4 +- app/src/main/res/layout/activity_about.xml | 2 +- app/src/main/res/layout/app_bar_main.xml | 2 +- app/src/main/res/layout/content_main.xml | 2 +- 21 files changed, 456 insertions(+), 268 deletions(-) delete mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java rename app/src/main/java/ru/volgorobot/vrcatalog/{ => view}/AboutActivity.java (92%) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java rename app/src/main/java/ru/volgorobot/vrcatalog/{ => view}/SettingsActivity.java (93%) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index ef96a8505cdab6aa364ad4da6e4cda1a42b9a674..33a95c05574a131d0f93a7b0ee0ed30a2f7bd9cf 100644 GIT binary patch delta 33 rcmV++0N($Z1epYom;@PTL9Vf!YylC4l68)huDINEbJ2}+au@~c&V~jmH@x4!^mkg!#*SZc6#&nI4rBlT diff --git a/.idea/misc.xml b/.idea/misc.xml index e0d5b93..b0c7b20 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 3d8f62f..70affef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,6 +10,10 @@ android { versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } buildTypes { release { minifyEnabled false @@ -24,6 +28,7 @@ dependencies { implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' + implementation 'com.github.msteinbeck:sig4j:1.0.1' implementation 'me.texy.treeview:treeview_lib:1.0.4' implementation 'com.google.code.gson:gson:2.8.2' implementation 'com.squareup.retrofit2:retrofit:2.1.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5a9a658..6b73d4a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> @@ -21,8 +21,8 @@ - - + + \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java deleted file mode 100644 index 7ff9c58..0000000 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ /dev/null @@ -1,247 +0,0 @@ -package ru.volgorobot.vrcatalog; - -import android.content.AsyncQueryHandler; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.AsyncTask; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.support.design.widget.NavigationView; -import android.support.v4.view.GravityCompat; -import android.support.v4.widget.DrawerLayout; -import android.support.v4.widget.SwipeRefreshLayout; -import android.support.v7.app.ActionBarDrawerToggle; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.util.Log; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Toast; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import me.texy.treeview.TreeNode; -import me.texy.treeview.TreeView; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import ru.volgorobot.vrcatalog.additional.NodeViewFactory; -import ru.volgorobot.vrcatalog.api.Controller; -import ru.volgorobot.vrcatalog.api.VRApi; -import ru.volgorobot.vrcatalog.model.FirstLevelModel; -import ru.volgorobot.vrcatalog.model.SecondLevelModel; -import ru.volgorobot.vrcatalog.model.ThirdLevelModel; - -public class MainActivity extends AppCompatActivity - implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { - - private TreeView treeView; - private VRApi vrApi; - private List firstLevelObjects = new ArrayList<>(); - private List firstLevelNodes = new ArrayList<>(); - private List secondLevelObjects = new ArrayList<>(); - private List thirdLevelObjects = new ArrayList<>(); - - private TreeNode rootNode = TreeNode.root(); - private SwipeRefreshLayout mSwipeRefreshLayout; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - - - DrawerLayout drawer = findViewById(R.id.drawer_layout); - ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( - this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); - drawer.addDrawerListener(toggle); - toggle.syncState(); - - NavigationView navigationView = findViewById(R.id.nav_view); - navigationView.setNavigationItemSelectedListener(this); - - mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); - mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); - mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); - - try { - getTreeData(false); - } catch (IllegalStateException e) { - // - } - treeView = new TreeView(rootNode, MainActivity.this, new NodeViewFactory()); - ((ViewGroup) findViewById(R.id.treeViewContainer)).addView(treeView.getView()); - } - - @Override - public void onBackPressed() { - DrawerLayout drawer = findViewById(R.id.drawer_layout); - if (drawer.isDrawerOpen(GravityCompat.START)) { - drawer.closeDrawer(GravityCompat.START); - } else { - super.onBackPressed(); - } - } - - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - - return super.onOptionsItemSelected(item); - } - - @SuppressWarnings("StatementWithEmptyBody") - @Override - public boolean onNavigationItemSelected(MenuItem item) { - // Handle navigation view item clicks here. - int id = item.getItemId(); - NavigationView navView = findViewById(R.id.nav_view); - - if (id == R.id.nav_settings) { - startActivity(new Intent(this, SettingsActivity.class)); - - } else if (id == R.id.nav_about) { - startActivity(new Intent(this, AboutActivity.class)); - } - - DrawerLayout drawer = findViewById(R.id.drawer_layout); - drawer.closeDrawer(GravityCompat.START); - return true; - } - - private void fillRootNode() { - for (int i = 0; i < firstLevelObjects.size(); i++) { - firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); - firstLevelNodes.get(i).setLevel(0); - firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID())); - } - - rootNode.setChildren(firstLevelNodes); - } - - private ArrayList getSecondLevelNodesByParentID(int parentTypeID) { - ArrayList objects = new ArrayList<>(); - for (int i = 0; i < secondLevelObjects.size(); i++) { - if(secondLevelObjects.get(i).getDetailTypeId() == parentTypeID) { - objects.add(secondLevelObjects.get(i)); - } - } - ArrayList nodes = new ArrayList<>(); - for (int i = 0; i < objects.size(); i++) { - nodes.add(new TreeNode(objects.get(i).getName())); - nodes.get(i).setLevel(1); - } - return nodes; - } - - private ArrayList getThirdLevelNodesByParentName(String parentName) { - ArrayList objects = new ArrayList<>(); - for (int i = 0; i < thirdLevelObjects.size(); i++) { - if(thirdLevelObjects.get(i).getName().equals(parentName)) { - objects.add(thirdLevelObjects.get(i)); - } - } - ArrayList nodes = new ArrayList<>(); - for (int i = 0; i < objects.size(); i++) { - nodes.add(new TreeNode(objects.get(i).getName())); - nodes.get(i).setLevel(2); - } - return nodes; - } - - @Override - public void onRefresh() { - mSwipeRefreshLayout.setRefreshing(true); - - try { - getTreeData(true); - } catch (IllegalStateException e) { - // - } - } - - private void getTreeData(final boolean clean) throws IllegalStateException { - mSwipeRefreshLayout.setRefreshing(true); - if(clean) { - firstLevelNodes.removeAll(firstLevelNodes); - firstLevelObjects.removeAll(firstLevelObjects); - secondLevelObjects.removeAll(secondLevelObjects); - } - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - String baseURL = sharedPreferences.getString("addressOfServer", ""); - - try { - vrApi = Controller.getApi(baseURL, this); - } catch(IllegalStateException e) { - throw new IllegalStateException(); - } - class GetTreeData extends AsyncTask { - @Override - protected Integer doInBackground(Void... voids) { - try { - Response> firstLevelResponse = vrApi.getFirstLevel().execute(); - if(firstLevelResponse.isSuccessful()) { - firstLevelObjects.addAll(firstLevelResponse.body()); - for (int i = 0; i < firstLevelObjects.size(); i++) { - Response> secondLevelResponse = vrApi.getSecondLevel(firstLevelObjects.get(i).getID()).execute(); - if(secondLevelResponse.isSuccessful()) { - secondLevelObjects.addAll(secondLevelResponse.body()); - } else { - return 2; - } - } - } else { - return 2; - } - } catch (Exception e) { - return 1; - } - return 0; - } - - @Override - protected void onPostExecute(Integer integer) { - super.onPostExecute(integer); - if(integer == 0) { - fillRootNode(); - treeView.refreshTreeView(); - mSwipeRefreshLayout.setRefreshing(false); - } else if(integer == 1) { - onFailureAnswer(1); - } else if(integer == 2) { - onFailureAnswer(2); - } - - } - } - new GetTreeData().execute(); - } - - private void onFailureAnswer(int errorCode) { - switch (errorCode) { - case 1: { - Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical info: null"); - mSwipeRefreshLayout.setRefreshing(false); - break; - } - case 2: { - Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Technical info: null"); - mSwipeRefreshLayout.setRefreshing(false); - break; - } - } - } - -} - - diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java new file mode 100644 index 0000000..cfabe87 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java @@ -0,0 +1,32 @@ +package ru.volgorobot.vrcatalog; + +import android.widget.ImageView; + +import com.github.msteinbeck.sig4j.signal.Signal1; + +import java.util.ArrayList; + +import ru.volgorobot.vrcatalog.model.FirstLevelModel; +import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; + +public interface MainContract { + interface MainActivityView { + void swipeLayoutSetRefreshing(boolean state); + void buildFirstLevelTree(ArrayList firstLevelModels); + void onFailureAnswer(int errorCode); + void refreshTree(); + } + + interface Presenter { + void getFirstLevel(); + void failureAnswerSlot(int errorCode); + } + + interface MainModel { + Signal1 errorSignal = new Signal1<>(); + ArrayList getFirstLevel(); + ArrayList getSecondLevelByParentID(int parentID); + ArrayList getThirdLevelByParentID(int parentID); + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java new file mode 100644 index 0000000..eba079a --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -0,0 +1,59 @@ +package ru.volgorobot.vrcatalog; + +import android.content.Context; +import android.os.AsyncTask; + +import java.util.ArrayList; + +import ru.volgorobot.vrcatalog.model.CoreModel; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; + +public class MainPresenter implements MainContract.Presenter { + private MainContract.MainActivityView mView; + private MainContract.MainModel coreModel; + + public MainPresenter(MainContract.MainActivityView mainActivityView, Context context) { + this.mView = mainActivityView; + try { + this.coreModel = new CoreModel(context); + } catch (IllegalStateException e) { + mView.onFailureAnswer(3); + } + coreModel.errorSignal.connect(this::failureAnswerSlot); + } + + @Override + public void getFirstLevel() { + new AsyncTask>() { + @Override + protected ArrayList doInBackground(Void... voids) { + ArrayList firstLevelModels = coreModel.getFirstLevel(); + return firstLevelModels; + } + + @Override + protected void onPostExecute(ArrayList firstLevelModels) { + mView.buildFirstLevelTree(firstLevelModels); + } + }.execute(); + mView.swipeLayoutSetRefreshing(false); + } + + @Override + public void failureAnswerSlot(int errorCode) { + switch (errorCode) { + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + } +} 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 caba708..0537306 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NodeViewFactory.java @@ -1,21 +1,31 @@ package ru.volgorobot.vrcatalog.additional; +import android.content.Context; 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.nodeViewBinders.FirstLevelNodeViewBinder; import ru.volgorobot.vrcatalog.nodeViewBinders.SecondLevelNodeViewBinder; import ru.volgorobot.vrcatalog.nodeViewBinders.ThirdLevelNodeViewBinder; public class NodeViewFactory extends BaseNodeViewFactory { + MainContract.MainActivityView mView; + Context context; + + public NodeViewFactory(Context context, MainContract.MainActivityView mView) { + this.mView = mView; + this.context = context; + } + @Override public BaseNodeViewBinder getNodeViewBinder(View view, int level) { switch (level) { case 0: - return new FirstLevelNodeViewBinder(view); + return new FirstLevelNodeViewBinder(view, context, mView); case 1: - return new SecondLevelNodeViewBinder(view); + return new SecondLevelNodeViewBinder(view, context, mView); case 2: return new ThirdLevelNodeViewBinder(view); default: diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java b/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java index 317ce52..3a1654a 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/Controller.java @@ -11,14 +11,12 @@ import retrofit2.converter.gson.GsonConverterFactory; public class Controller { private static String baseURL; - private static Context context; private static Retrofit retrofit; private static VRApi vrApi; - public static VRApi getApi(String baseURL1, Context context1) throws IllegalStateException { + public static VRApi getApi(String baseURL1) throws IllegalStateException { baseURL = baseURL1; - context = context1; Gson gson = new GsonBuilder() .setLenient() .create(); @@ -38,7 +36,6 @@ public class Controller { .addConverterFactory(GsonConverterFactory.create(gson)) .build(); } catch(Exception e) { - Toast.makeText(context, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show(); throw new IllegalStateException(); } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java new file mode 100644 index 0000000..8f24604 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java @@ -0,0 +1,80 @@ +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; +import java.util.List; + +import retrofit2.Response; +import ru.volgorobot.vrcatalog.MainContract; +import ru.volgorobot.vrcatalog.api.Controller; +import ru.volgorobot.vrcatalog.api.VRApi; + +public class CoreModel implements MainContract.MainModel { + private Context context; + private VRApi vrApi; + public final Signal1 errorSignal = new Signal1<>(); + + public CoreModel(Context context) throws IllegalStateException { + this.context = context; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + String baseURL = sharedPreferences.getString("addressOfServer", ""); + try { + vrApi = Controller.getApi(baseURL); + } catch(IllegalStateException e) { + throw new IllegalStateException(); + } + } + + @Override + public ArrayList getFirstLevel() { + final ArrayList firstLevelModels = new ArrayList<>(); + try { + Response> response = vrApi.getFirstLevel().execute(); + if(response.isSuccessful()) { + firstLevelModels.addAll(response.body()); + } else { + errorSignal.emit(2); + } + } catch (IOException e) { + errorSignal.emit(1); + } + return firstLevelModels; + } + + @Override + public ArrayList getSecondLevelByParentID(int parentID) { + final ArrayList secondLevelModels = new ArrayList<>(); + try { + Response> response = vrApi.getSecondLevel(parentID).execute(); + if(response.isSuccessful()) { + secondLevelModels.addAll(response.body()); + } else { + errorSignal.emit(2); + } + } catch (IOException e) { + errorSignal.emit(1); + } + return secondLevelModels; + } + + @Override + public ArrayList getThirdLevelByParentID(int parentID) { + final ArrayList thirdLevelModels = new ArrayList<>(); + try { + Response> response = vrApi.getDetails(parentID).execute(); + if(response.isSuccessful()) { + thirdLevelModels.addAll(response.body()); + } else { + errorSignal.emit(2); + } + } catch (IOException e) { + errorSignal.emit(1); + } + return thirdLevelModels; + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java index b5579b4..3b467f7 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java @@ -40,4 +40,7 @@ public class FirstLevelModel { this.iD = iD; } + public String toString() { + return name; + } } \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java index c002540..51cfb71 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java @@ -51,4 +51,7 @@ public class SecondLevelModel { this.name = name; } + public String toString() { + return name; + } } \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java index 11c2c7d..c8898de 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java @@ -59,4 +59,7 @@ public class ThirdLevelModel { this.name = name; } + public String toString() { + return name; + } } \ No newline at end of file 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 edc69ec..c08768f 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -1,21 +1,34 @@ package ru.volgorobot.vrcatalog.nodeViewBinders; +import android.content.Context; +import android.os.AsyncTask; import android.view.View; import android.widget.ImageView; 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.model.CoreModel; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; +import ru.volgorobot.vrcatalog.model.SecondLevelModel; public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { TextView textView; ImageView imageView; + CoreModel coreModel; + MainContract.MainActivityView mView; - public FirstLevelNodeViewBinder(View itemView) { + public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) { 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.mView = mView; + coreModel.errorSignal.connect(mView::onFailureAnswer); } @Override @@ -30,8 +43,36 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { @Override public void onNodeToggled(TreeNode treeNode, boolean expand) { - if (expand) { - imageView.animate().rotation(90).setDuration(200).start(); + if(expand) { + FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue(); + new AsyncTask>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ArrayList doInBackground(Void... voids) { + ArrayList secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID()); + return secondLevelModels; + } + + @Override + protected void onPostExecute(ArrayList secondLevelModels) { + super.onPostExecute(secondLevelModels); + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < secondLevelModels.size(); i++) { + TreeNode treeNode1 = new TreeNode(secondLevelModels.get(i)); + treeNode1.setLevel(1); + treeNodes.add(treeNode1); + } + treeNode.setChildren(treeNodes); + mView.refreshTree(); + mView.swipeLayoutSetRefreshing(false); + imageView.animate().rotation(90).setDuration(200).start(); + } + }.execute(); } else { imageView.animate().rotation(0).setDuration(200).start(); } 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 2b55270..cfd07c7 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -1,21 +1,35 @@ package ru.volgorobot.vrcatalog.nodeViewBinders; +import android.content.Context; +import android.os.AsyncTask; import android.view.View; import android.widget.ImageView; 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.model.CoreModel; +import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { TextView textView; ImageView imageView; + MainContract.MainModel coreModel; + MainContract.MainActivityView mView; - public SecondLevelNodeViewBinder(View itemView) { + + public SecondLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView) { 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.mView = mView; + coreModel.errorSignal.connect(mView::onFailureAnswer); } @Override @@ -31,8 +45,36 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { @Override public void onNodeToggled(TreeNode treeNode, boolean expand) { - if (expand) { - imageView.animate().rotation(90).setDuration(200).start(); + if(expand) { + SecondLevelModel secondLevelModel = (SecondLevelModel) treeNode.getValue(); + new AsyncTask>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ArrayList doInBackground(Void... voids) { + ArrayList thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID()); + return thirdLevelModels; + } + + @Override + protected void onPostExecute(ArrayList thirdLevelModels) { + super.onPostExecute(thirdLevelModels); + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < thirdLevelModels.size(); i++) { + TreeNode treeNode1 = new TreeNode(thirdLevelModels.get(i)); + treeNode1.setLevel(2); + treeNodes.add(treeNode1); + } + treeNode.setChildren(treeNodes); + mView.refreshTree(); + mView.swipeLayoutSetRefreshing(false); + imageView.animate().rotation(90).setDuration(200).start(); + } + }.execute(); } else { imageView.animate().rotation(0).setDuration(200).start(); } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/AboutActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/AboutActivity.java similarity index 92% rename from app/src/main/java/ru/volgorobot/vrcatalog/AboutActivity.java rename to app/src/main/java/ru/volgorobot/vrcatalog/view/AboutActivity.java index 817410f..a1496df 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/AboutActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/AboutActivity.java @@ -1,10 +1,12 @@ -package ru.volgorobot.vrcatalog; +package ru.volgorobot.vrcatalog.view; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; +import ru.volgorobot.vrcatalog.R; + public class AboutActivity extends AppCompatActivity { @Override diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java new file mode 100644 index 0000000..b0a21d1 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java @@ -0,0 +1,156 @@ +package ru.volgorobot.vrcatalog.view; + +import android.content.Intent; +import android.os.Bundle; +import android.support.design.widget.NavigationView; +import android.support.v4.view.GravityCompat; +import android.support.v4.widget.DrawerLayout; +import android.support.v4.widget.SwipeRefreshLayout; +import android.support.v7.app.ActionBarDrawerToggle; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.DefaultItemAnimator; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.MenuItem; +import android.view.ViewGroup; +import android.widget.Toast; + +import java.util.ArrayList; + +import me.texy.treeview.TreeNode; +import me.texy.treeview.TreeView; +import ru.volgorobot.vrcatalog.MainContract; +import ru.volgorobot.vrcatalog.MainPresenter; +import ru.volgorobot.vrcatalog.R; +import ru.volgorobot.vrcatalog.additional.NodeViewFactory; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; + +public class MainActivity extends AppCompatActivity + implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener, MainContract.MainActivityView { + + private MainContract.Presenter mPresenter; + private TreeView treeView; + private TreeNode rootNode = TreeNode.root(); + private SwipeRefreshLayout mSwipeRefreshLayout; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + DrawerLayout drawer = findViewById(R.id.drawer_layout); + ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( + this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); + drawer.addDrawerListener(toggle); + toggle.syncState(); + + NavigationView navigationView = findViewById(R.id.nav_view); + navigationView.setNavigationItemSelectedListener(this); + + mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); + mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); + mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); + + mPresenter = new MainPresenter(this, MainActivity.this); + + treeView = new TreeView(rootNode, MainActivity.this, new NodeViewFactory(MainActivity.this, this)); + treeView.setItemAnimator(new DefaultItemAnimator()); + ((ViewGroup) findViewById(R.id.treeViewContainer)).addView(treeView.getView()); + mSwipeRefreshLayout.setRefreshing(true); + mPresenter.getFirstLevel(); + } + + @Override + public void onBackPressed() { + DrawerLayout drawer = findViewById(R.id.drawer_layout); + if (drawer.isDrawerOpen(GravityCompat.START)) { + drawer.closeDrawer(GravityCompat.START); + } else { + super.onBackPressed(); + } + } + + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + + return super.onOptionsItemSelected(item); + } + + @SuppressWarnings("StatementWithEmptyBody") + @Override + public boolean onNavigationItemSelected(MenuItem item) { + // Handle navigation view item clicks here. + int id = item.getItemId(); + NavigationView navView = findViewById(R.id.nav_view); + + if (id == R.id.nav_settings) { + startActivity(new Intent(this, SettingsActivity.class)); + + } else if (id == R.id.nav_about) { + startActivity(new Intent(this, AboutActivity.class)); + } + + DrawerLayout drawer = findViewById(R.id.drawer_layout); + drawer.closeDrawer(GravityCompat.START); + return true; + } + + + @Override + public void onRefresh() { + mSwipeRefreshLayout.setRefreshing(true); + mPresenter.getFirstLevel(); + } + + public void onFailureAnswer(int errorCode) { + switch (errorCode) { + case 1: { + Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); + Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical info: null"); + mSwipeRefreshLayout.setRefreshing(false); + break; + } + case 2: { + Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); + Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Technical info: null"); + mSwipeRefreshLayout.setRefreshing(false); + break; + } + case 3: { + Toast.makeText(this, "Вы ввели неверный URL. Пример: http://example.ru", Toast.LENGTH_LONG).show(); + mSwipeRefreshLayout.setRefreshing(false); + break; + } + } + } + + @Override + public void swipeLayoutSetRefreshing(boolean state) { + mSwipeRefreshLayout.setRefreshing(state); + } + + @Override + public void buildFirstLevelTree(ArrayList firstLevelModels) { + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < firstLevelModels.size(); i++) { + TreeNode treeNode = new TreeNode(firstLevelModels.get(i)); + treeNode.setLevel(0); + treeNodes.add(treeNode); + } + rootNode.setChildren(treeNodes); + treeView.refreshTreeView(); + } + + @Override + public void refreshTree() { + treeView.refreshTreeView(); + } +} + + diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java similarity index 93% rename from app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java rename to app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java index d74d8a9..e4d5335 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/SettingsActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java @@ -1,8 +1,10 @@ -package ru.volgorobot.vrcatalog; +package ru.volgorobot.vrcatalog.view; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; + +import ru.volgorobot.vrcatalog.R; import ru.volgorobot.vrcatalog.additional.AppCompatPreferenceActivity; public class SettingsActivity extends AppCompatPreferenceActivity { diff --git a/app/src/main/res/layout/activity_about.xml b/app/src/main/res/layout/activity_about.xml index 4bc0a9c..f89d895 100644 --- a/app/src/main/res/layout/activity_about.xml +++ b/app/src/main/res/layout/activity_about.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" - tools:context=".AboutActivity"> + tools:context=".view.AboutActivity"> + tools:context=".view.MainActivity"> From 259006d354f7412c66ac96d16e456d147eba981b Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 4 Jan 2019 21:36:10 +0400 Subject: [PATCH 19/27] Changed the style of text on the tree. --- app/src/main/res/layout/first_level_item.xml | 6 +++--- app/src/main/res/layout/second_level_item.xml | 3 ++- app/src/main/res/layout/third_level_item.xml | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/src/main/res/layout/first_level_item.xml b/app/src/main/res/layout/first_level_item.xml index f53dc6d..68a0797 100644 --- a/app/src/main/res/layout/first_level_item.xml +++ b/app/src/main/res/layout/first_level_item.xml @@ -22,9 +22,9 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" - android:fontFamily="monospace" - android:textSize="15dp" - android:text="Tree Node" /> + android:text="Tree Node" + android:textAppearance="@style/TextAppearance.AppCompat" + android:textSize="18sp" /> \ No newline at end of file diff --git a/app/src/main/res/layout/second_level_item.xml b/app/src/main/res/layout/second_level_item.xml index f2d1388..b1ede8e 100644 --- a/app/src/main/res/layout/second_level_item.xml +++ b/app/src/main/res/layout/second_level_item.xml @@ -25,7 +25,8 @@ android:layout_gravity="center" android:fontFamily="monospace" android:text="Tree Node" - android:textSize="15dp" /> + android:textAppearance="@style/TextAppearance.AppCompat" + android:textSize="18sp" /> \ No newline at end of file diff --git a/app/src/main/res/layout/third_level_item.xml b/app/src/main/res/layout/third_level_item.xml index 879bd0e..2679be5 100644 --- a/app/src/main/res/layout/third_level_item.xml +++ b/app/src/main/res/layout/third_level_item.xml @@ -20,7 +20,8 @@ android:layout_gravity="center" android:fontFamily="monospace" android:text="Tree Node" - android:textSize="15dp" /> + android:textAppearance="@style/TextAppearance.AppCompat" + android:textSize="18sp" /> \ No newline at end of file From 9fc86b219be38f62684ac359a81f66341fe57d15 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 15:08:40 +0400 Subject: [PATCH 20/27] Added an advanced error response system. --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes .../ru/volgorobot/vrcatalog/MainContract.java | 15 +-- .../volgorobot/vrcatalog/MainPresenter.java | 71 +++++----- .../additional/NetworkErrorException.java | 13 ++ .../additional/ResultWithErrorCode.java | 28 ++++ .../volgorobot/vrcatalog/model/CoreModel.java | 123 +++++++++++------- .../FirstLevelNodeViewBinder.java | 55 ++++++-- .../SecondLevelNodeViewBinder.java | 55 +++++--- build.gradle | 2 + 9 files changed, 247 insertions(+), 115 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 33a95c05574a131d0f93a7b0ee0ed30a2f7bd9cf..1b56c5d76906aca8b9bd67fc9759fefd1ac50436 100644 GIT binary patch delta 35 tcmV+;0NnqX1epYom;@bxE>)47SM( diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java index cfabe87..2e7232f 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java @@ -1,11 +1,8 @@ package ru.volgorobot.vrcatalog; -import android.widget.ImageView; - -import com.github.msteinbeck.sig4j.signal.Signal1; - import java.util.ArrayList; +import ru.volgorobot.vrcatalog.additional.NetworkErrorException; import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; import ru.volgorobot.vrcatalog.model.ThirdLevelModel; @@ -20,13 +17,13 @@ public interface MainContract { interface Presenter { void getFirstLevel(); - void failureAnswerSlot(int errorCode); } interface MainModel { - Signal1 errorSignal = new Signal1<>(); - ArrayList getFirstLevel(); - ArrayList getSecondLevelByParentID(int parentID); - ArrayList getThirdLevelByParentID(int parentID); + ArrayList getFirstLevel() throws NetworkErrorException, NullPointerException; + ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList getThirdLevelByParentID(int parentID) 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 eba079a..c707bef 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -5,6 +5,8 @@ import android.os.AsyncTask; import java.util.ArrayList; +import ru.volgorobot.vrcatalog.additional.NetworkErrorException; +import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; import ru.volgorobot.vrcatalog.model.CoreModel; import ru.volgorobot.vrcatalog.model.FirstLevelModel; @@ -14,46 +16,53 @@ public class MainPresenter implements MainContract.Presenter { public MainPresenter(MainContract.MainActivityView mainActivityView, Context context) { this.mView = mainActivityView; - try { - this.coreModel = new CoreModel(context); - } catch (IllegalStateException e) { - mView.onFailureAnswer(3); - } - coreModel.errorSignal.connect(this::failureAnswerSlot); + this.coreModel = new CoreModel(context); } @Override public void getFirstLevel() { - new AsyncTask>() { + try { + coreModel.reinitializeApi(); + } catch (IllegalStateException e) { + mView.onFailureAnswer(3); + return; + } + new AsyncTask>>() { @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList firstLevelModels = coreModel.getFirstLevel(); - return firstLevelModels; + protected ResultWithErrorCode> doInBackground(Void... voids) { + ArrayList firstLevelModels = null; + try { + firstLevelModels = coreModel.getFirstLevel(); + } catch (NetworkErrorException networkErrorException) { + return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode()); + } catch (NullPointerException nullPointerException) { + return new ResultWithErrorCode<>(firstLevelModels, 3); + } + return new ResultWithErrorCode<>(firstLevelModels, 0); } @Override - protected void onPostExecute(ArrayList firstLevelModels) { - mView.buildFirstLevelTree(firstLevelModels); + protected void onPostExecute(ResultWithErrorCode> result) { + switch (result.getErrorCode()) { + case 0: { + mView.buildFirstLevelTree(result.getData()); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); } }.execute(); - mView.swipeLayoutSetRefreshing(false); - } - - @Override - public void failureAnswerSlot(int errorCode) { - switch (errorCode) { - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java new file mode 100644 index 0000000..ea9bf16 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java @@ -0,0 +1,13 @@ +package ru.volgorobot.vrcatalog.additional; + +public class NetworkErrorException extends Exception { + private int errorCode; + + public NetworkErrorException(int errorCode) { + this.errorCode = errorCode; + } + + public int getErrorCode() { + return errorCode; + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java new file mode 100644 index 0000000..6d20600 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java @@ -0,0 +1,28 @@ +package ru.volgorobot.vrcatalog.additional; + + +public class ResultWithErrorCode { + private T data; + private int errorCode; + + public ResultWithErrorCode(T data, int errorCode) { + this.data = data; + this.errorCode = errorCode; + } + + public T getData() { + return data; + } + + public int getErrorCode() { + return errorCode; + } + + public void setData(T data) { + this.data = data; + } + + public void setErrorCode(int errorCode) { + this.errorCode = errorCode; + } +} 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 8f24604..90e2498 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java @@ -11,18 +11,88 @@ import java.util.List; import retrofit2.Response; import ru.volgorobot.vrcatalog.MainContract; +import ru.volgorobot.vrcatalog.additional.NetworkErrorException; import ru.volgorobot.vrcatalog.api.Controller; import ru.volgorobot.vrcatalog.api.VRApi; 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) throws IllegalStateException { + public CoreModel(Context context) { this.context = context; SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); - String baseURL = sharedPreferences.getString("addressOfServer", ""); + this.baseURL = sharedPreferences.getString("addressOfServer", ""); + try { + vrApi = Controller.getApi(baseURL); + } catch(IllegalStateException e) { + // Nothing + } + } + + @Override + public ArrayList getFirstLevel() throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + final ArrayList firstLevelModels = new ArrayList<>(); + try { + Response> response = vrApi.getFirstLevel().execute(); + if(response.isSuccessful()) { + firstLevelModels.addAll(response.body()); + } else { + throw new NetworkErrorException(2); + } + } catch (IOException e) { + throw new NetworkErrorException(1); + } + return firstLevelModels; + } + + @Override + public ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + final ArrayList secondLevelModels = new ArrayList<>(); + try { + Response> response = vrApi.getSecondLevel(parentID).execute(); + if(response.isSuccessful()) { + secondLevelModels.addAll(response.body()); + } else { + throw new NetworkErrorException(2); + } + } catch (IOException e) { + throw new NetworkErrorException(1); + } + return secondLevelModels; + } + + @Override + public ArrayList getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + final ArrayList thirdLevelModels = new ArrayList<>(); + try { + Response> response = vrApi.getDetails(parentID).execute(); + if(response.isSuccessful()) { + thirdLevelModels.addAll(response.body()); + } else { + throw new NetworkErrorException(2); + } + } catch (IOException e) { + throw new NetworkErrorException(1); + } + return thirdLevelModels; + } + + @Override + public void reinitializeApi() throws IllegalStateException { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + this.baseURL = sharedPreferences.getString("addressOfServer", ""); try { vrApi = Controller.getApi(baseURL); } catch(IllegalStateException e) { @@ -31,50 +101,11 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getFirstLevel() { - final ArrayList firstLevelModels = new ArrayList<>(); - try { - Response> response = vrApi.getFirstLevel().execute(); - if(response.isSuccessful()) { - firstLevelModels.addAll(response.body()); - } else { - errorSignal.emit(2); - } - } catch (IOException e) { - errorSignal.emit(1); + public boolean getApiState() { + if(vrApi != null) { + return true; + } else { + return false; } - return firstLevelModels; - } - - @Override - public ArrayList getSecondLevelByParentID(int parentID) { - final ArrayList secondLevelModels = new ArrayList<>(); - try { - Response> response = vrApi.getSecondLevel(parentID).execute(); - if(response.isSuccessful()) { - secondLevelModels.addAll(response.body()); - } else { - errorSignal.emit(2); - } - } catch (IOException e) { - errorSignal.emit(1); - } - return secondLevelModels; - } - - @Override - public ArrayList getThirdLevelByParentID(int parentID) { - final ArrayList thirdLevelModels = new ArrayList<>(); - try { - Response> response = vrApi.getDetails(parentID).execute(); - if(response.isSuccessful()) { - thirdLevelModels.addAll(response.body()); - } else { - errorSignal.emit(2); - } - } catch (IOException e) { - errorSignal.emit(1); - } - return thirdLevelModels; } } 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 c08768f..52865a2 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -12,6 +12,8 @@ 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.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; @@ -45,7 +47,7 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { public void onNodeToggled(TreeNode treeNode, boolean expand) { if(expand) { FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue(); - new AsyncTask>() { + new AsyncTask>>() { @Override protected void onPreExecute() { super.onPreExecute(); @@ -53,24 +55,49 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { } @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID()); - return secondLevelModels; + protected ResultWithErrorCode> doInBackground(Void... voids) { + ArrayList secondLevelModels = null; + try { + secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID()); + } catch (NetworkErrorException networkErrorException) { + return new ResultWithErrorCode<>(secondLevelModels, networkErrorException.getErrorCode()); + } catch (NullPointerException nullPointerException) { + return new ResultWithErrorCode<>(secondLevelModels, 3); + } + + return new ResultWithErrorCode<>(secondLevelModels, 0); } @Override - protected void onPostExecute(ArrayList secondLevelModels) { - super.onPostExecute(secondLevelModels); - ArrayList treeNodes = new ArrayList<>(); - for (int i = 0; i < secondLevelModels.size(); i++) { - TreeNode treeNode1 = new TreeNode(secondLevelModels.get(i)); - treeNode1.setLevel(1); - treeNodes.add(treeNode1); + protected void onPostExecute(ResultWithErrorCode> result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < result.getData().size(); i++) { + TreeNode treeNode1 = new TreeNode(result.getData().get(i)); + treeNode1.setLevel(1); + treeNodes.add(treeNode1); + } + treeNode.setChildren(treeNodes); + mView.refreshTree(); + imageView.animate().rotation(90).setDuration(200).start(); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } } - treeNode.setChildren(treeNodes); - mView.refreshTree(); mView.swipeLayoutSetRefreshing(false); - imageView.animate().rotation(90).setDuration(200).start(); } }.execute(); } else { 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 cfd07c7..670fcb1 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -12,6 +12,8 @@ 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.SecondLevelModel; import ru.volgorobot.vrcatalog.model.ThirdLevelModel; @@ -29,7 +31,6 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { imageView = (ImageView) itemView.findViewById(R.id.arrow_img); this.coreModel = new CoreModel(context); this.mView = mView; - coreModel.errorSignal.connect(mView::onFailureAnswer); } @Override @@ -47,7 +48,7 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { public void onNodeToggled(TreeNode treeNode, boolean expand) { if(expand) { SecondLevelModel secondLevelModel = (SecondLevelModel) treeNode.getValue(); - new AsyncTask>() { + new AsyncTask>>() { @Override protected void onPreExecute() { super.onPreExecute(); @@ -55,24 +56,48 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { } @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID()); - return thirdLevelModels; + protected ResultWithErrorCode> doInBackground(Void... voids) { + ArrayList thirdLevelModels = null; + try { + thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID()); + } catch (NetworkErrorException networkErrorException) { + return new ResultWithErrorCode<>(thirdLevelModels, networkErrorException.getErrorCode()); + } catch (NullPointerException nullPointerException) { + return new ResultWithErrorCode<>(thirdLevelModels, 3); + } + return new ResultWithErrorCode<>(thirdLevelModels, 0); } @Override - protected void onPostExecute(ArrayList thirdLevelModels) { - super.onPostExecute(thirdLevelModels); - ArrayList treeNodes = new ArrayList<>(); - for (int i = 0; i < thirdLevelModels.size(); i++) { - TreeNode treeNode1 = new TreeNode(thirdLevelModels.get(i)); - treeNode1.setLevel(2); - treeNodes.add(treeNode1); + protected void onPostExecute(ResultWithErrorCode> result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < result.getData().size(); i++) { + TreeNode treeNode1 = new TreeNode(result.getData().get(i)); + treeNode1.setLevel(2); + treeNodes.add(treeNode1); + } + treeNode.setChildren(treeNodes); + mView.refreshTree(); + imageView.animate().rotation(90).setDuration(200).start(); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } } - treeNode.setChildren(treeNodes); - mView.refreshTree(); mView.swipeLayoutSetRefreshing(false); - imageView.animate().rotation(90).setDuration(200).start(); } }.execute(); } else { diff --git a/build.gradle b/build.gradle index 8d3ef8e..03de3f4 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,7 @@ buildscript { repositories { google() jcenter() + mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:3.2.1' @@ -19,6 +20,7 @@ allprojects { repositories { google() jcenter() + mavenCentral() } } From d53165fe487189960ed3fce7d71d5460e8264733 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 15:34:02 +0400 Subject: [PATCH 21/27] Moved refreshing call from onRefresh() (MainActivity) to MainPresenter (getFirstLevel()) --- app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java | 1 + app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java index c707bef..3fa59d7 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -21,6 +21,7 @@ public class MainPresenter implements MainContract.Presenter { @Override public void getFirstLevel() { + mView.swipeLayoutSetRefreshing(true); try { coreModel.reinitializeApi(); } catch (IllegalStateException e) { 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 b0a21d1..dffbd16 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/MainActivity.java @@ -104,7 +104,6 @@ public class MainActivity extends AppCompatActivity @Override public void onRefresh() { - mSwipeRefreshLayout.setRefreshing(true); mPresenter.getFirstLevel(); } From 90dae086e4861fb13c0e57258c0bbcbc3053c19f Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 15:42:09 +0400 Subject: [PATCH 22/27] Fixed a bug (activation of SwipeRefreshLayout) when scrolling up. --- app/src/main/res/layout/content_main.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 86b2141..88dce18 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -10,12 +10,12 @@ android:orientation="horizontal" android:id="@+id/swipeRefreshLayout"> - - + \ No newline at end of file From e7795ca4c5dad9ad1ed714cd48dfbf8ac9ebd1db Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 17:37:29 +0400 Subject: [PATCH 23/27] 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 From 37fdf104d537e2798b6f169f745a1c5075d25f39 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 18:21:20 +0400 Subject: [PATCH 24/27] Added others details --- .../ThirdLevelNodeViewBinder.java | 3 + .../vrcatalog/view/DetailActivity.java | 15 ++- app/src/main/res/layout/activity_detail.xml | 94 ++++++++++++++++--- 3 files changed, 97 insertions(+), 15 deletions(-) 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 d7c55ae..c3365b9 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java @@ -75,6 +75,9 @@ public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder { intent.putExtra("detailName", result.getData().getName()); intent.putExtra("detailQuantity", result.getData().getNumber().toString()); intent.putExtra("detailPrice", result.getData().getPrice()); + intent.putExtra("detailContry", 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; diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java index 44e90fc..5e95a3c 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java @@ -3,6 +3,7 @@ package ru.volgorobot.vrcatalog.view; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.text.method.LinkMovementMethod; import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; @@ -13,6 +14,9 @@ public class DetailActivity extends AppCompatActivity { TextView nameView; TextView quantityView; TextView priceView; + TextView countryView; + TextView analogueView; + TextView datasheetView; EditText notesView; @Override @@ -26,9 +30,12 @@ public class DetailActivity extends AppCompatActivity { Intent intent = getIntent(); nameView.setText(intent.getStringExtra("detailName")); - quantityView.setText(intent.getStringExtra("detailQuantity")); + quantityView.setText(intent.getStringExtra("detailQuantity").equals("0") ? "Нет на складе" : intent.getStringExtra("detailQuantity")); priceView.setText(intent.getStringExtra("detailPrice")); - notesView.setText(intent.getStringExtra("detailNotes")); + countryView.setText(intent.getStringExtra("detailCountry")); + analogueView.setText(intent.getStringExtra("detailAnalogue").equals("") ? "Нет аналогов" : intent.getStringExtra("detailAnalogue")); + datasheetView.setText(intent.getStringExtra("detailDatasheet").equals(" ") ? "Нет" : intent.getStringExtra("detailDatasheet")); + notesView.setText(intent.getStringExtra("detailNotes").equals("") ? "Нет описания" : intent.getStringExtra("detailNotes")); } void initializeViews() { @@ -36,6 +43,10 @@ public class DetailActivity extends AppCompatActivity { quantityView = findViewById(R.id.quantity); priceView = findViewById(R.id.price); notesView = findViewById(R.id.notes); + countryView = findViewById(R.id.country); + analogueView = findViewById(R.id.analogue); + datasheetView = findViewById(R.id.datasheet); + datasheetView.setMovementMethod(LinkMovementMethod.getInstance()); } @Override diff --git a/app/src/main/res/layout/activity_detail.xml b/app/src/main/res/layout/activity_detail.xml index 1d1daf9..5727204 100644 --- a/app/src/main/res/layout/activity_detail.xml +++ b/app/src/main/res/layout/activity_detail.xml @@ -29,11 +29,11 @@ android:id="@+id/notes_text" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginTop="7dp" + android:layout_marginTop="172dp" android:text="Описание: " android:textAppearance="@style/TextAppearance.AppCompat.Body1" - app:layout_constraintStart_toStartOf="@+id/price_text" - app:layout_constraintTop_toBottomOf="@+id/price_text" /> + app:layout_constraintStart_toStartOf="@+id/notes" + app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toTopOf="@+id/name" /> + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="@+id/notes_text"> + @@ -80,8 +86,70 @@ android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginTop="7dp" + android:layout_marginBottom="11dp" android:text="TextView" - app:layout_constraintStart_toEndOf="@+id/price_text" - app:layout_constraintTop_toBottomOf="@+id/quantity_text" /> + app:layout_constraintBottom_toTopOf="@+id/country_text" + app:layout_constraintStart_toEndOf="@+id/price_text" /> + + + + + + + + + + + + \ No newline at end of file From 312598bd70750a6c080ce834eea80a465c2afd52 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 9 Jan 2019 18:49:49 +0400 Subject: [PATCH 25/27] Refactoring of nodeViewBinders --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes app/build.gradle | 1 - .../ru/volgorobot/vrcatalog/MainContract.java | 19 +- .../volgorobot/vrcatalog/MainPresenter.java | 2 +- .../vrcatalog/ViewBinderPresenter.java | 205 ++++++++++++++++++ .../volgorobot/vrcatalog/model/CoreModel.java | 8 +- .../FirstLevelNodeViewBinder.java | 70 +----- .../SecondLevelNodeViewBinder.java | 67 +----- .../ThirdLevelNodeViewBinder.java | 68 +----- 9 files changed, 256 insertions(+), 184 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 4d8407c9fc8e10ed9d50bc92fd154b877dc483e6..9c9d4877679efb0f01b207b4ae0d52e78ccc685f 100644 GIT binary patch delta 33 rcmV++0N($Z1epYom;^HoV7#%MYylBD=;r#8h(4nhX0jA_t<>pw<{b|u delta 33 rcmV++0N($Z1epYom;@dEww1A*YylC4l68)huD 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; + ArrayList fetchFirstLevel() throws NetworkErrorException, NullPointerException; + ArrayList fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList fetchDetailByID(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 8bac518..81a6f77 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -33,7 +33,7 @@ public class MainPresenter implements MainContract.Presenter { protected ResultWithErrorCode> doInBackground(Void... voids) { ArrayList firstLevelModels = null; try { - firstLevelModels = coreModel.getFirstLevel(); + firstLevelModels = coreModel.fetchFirstLevel(); } catch (NetworkErrorException networkErrorException) { return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode()); } catch (NullPointerException nullPointerException) { diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java new file mode 100644 index 0000000..dc1bdf0 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java @@ -0,0 +1,205 @@ +package ru.volgorobot.vrcatalog; + +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; + +import java.util.ArrayList; + +import me.texy.treeview.TreeNode; +import ru.volgorobot.vrcatalog.additional.NetworkErrorException; +import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; +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; + +public class ViewBinderPresenter implements MainContract.ViewBinderPresenter { + private MainContract.MainActivityView mView; + private MainContract.MainModel coreModel; + private MainContract.ViewBinder viewBinder; + private Context context; + + public ViewBinderPresenter(MainContract.MainActivityView mView, MainContract.MainModel coreModel, MainContract.ViewBinder viewBinder) { + this.mView = mView; + this.coreModel = coreModel; + this.viewBinder = viewBinder; + } + + public ViewBinderPresenter(MainContract.MainActivityView mView, MainContract.MainModel coreModel, MainContract.ViewBinder viewBinder, Context context) { + this.mView = mView; + this.coreModel = coreModel; + this.viewBinder = viewBinder; + this.context = context; + } + + @Override + public void fetchSecondLevel(FirstLevelModel firstLevelModel, TreeNode treeNode) { + new AsyncTask>>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode> doInBackground(FirstLevelModel... firstLevelModel) { + ArrayList secondLevelModels = null; + try { + secondLevelModels = coreModel.fetchSecondLevelByParentID(firstLevelModel[0].getID()); + } catch (NetworkErrorException networkErrorException) { + return new ResultWithErrorCode<>(secondLevelModels, networkErrorException.getErrorCode()); + } catch (NullPointerException nullPointerException) { + return new ResultWithErrorCode<>(secondLevelModels, 3); + } + + return new ResultWithErrorCode<>(secondLevelModels, 0); + } + + @Override + protected void onPostExecute(ResultWithErrorCode> result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < result.getData().size(); i++) { + TreeNode treeNode1 = new TreeNode(result.getData().get(i)); + treeNode1.setLevel(1); + treeNodes.add(treeNode1); + } + treeNode.setChildren(treeNodes); + mView.refreshTree(); + viewBinder.rotateImageView(); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); + } + }.execute(firstLevelModel); + } + + @Override + public void fetchThirdLevel(SecondLevelModel secondLevelModel, TreeNode treeNode) { + new AsyncTask>>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode> doInBackground(SecondLevelModel... secondLevelModel) { + ArrayList thirdLevelModels = null; + try { + thirdLevelModels = coreModel.fetchThirdLevelByParentID(secondLevelModel[0].getID()); + } catch (NetworkErrorException networkErrorException) { + return new ResultWithErrorCode<>(thirdLevelModels, networkErrorException.getErrorCode()); + } catch (NullPointerException nullPointerException) { + return new ResultWithErrorCode<>(thirdLevelModels, 3); + } + return new ResultWithErrorCode<>(thirdLevelModels, 0); + } + + @Override + protected void onPostExecute(ResultWithErrorCode> result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + ArrayList treeNodes = new ArrayList<>(); + for (int i = 0; i < result.getData().size(); i++) { + TreeNode treeNode1 = new TreeNode(result.getData().get(i)); + treeNode1.setLevel(2); + treeNodes.add(treeNode1); + } + treeNode.setChildren(treeNodes); + mView.refreshTree(); + viewBinder.rotateImageView(); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); + } + }.execute(secondLevelModel); + } + + @Override + public void fetchDetail(ThirdLevelModel thirdLevelModel) { + new AsyncTask>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode doInBackground(ThirdLevelModel... thirdLevelModel) { + ArrayList detail = null; + try { + detail = coreModel.fetchDetailByID(thirdLevelModel[0].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("detailContry", 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.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); + } + }.execute(thirdLevelModel); + } +} 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 349dded..ccbb53a 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java @@ -31,7 +31,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getFirstLevel() throws NetworkErrorException, NullPointerException { + public ArrayList fetchFirstLevel() throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } @@ -50,7 +50,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + public ArrayList fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } @@ -69,7 +69,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + public ArrayList fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } @@ -88,7 +88,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getDetailByID(int detailID) throws NetworkErrorException, NullPointerException { + public ArrayList fetchDetailByID(int detailID) throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } 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 72a2a32..78b6495 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -12,24 +12,23 @@ 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.ViewBinderPresenter; import ru.volgorobot.vrcatalog.additional.NetworkErrorException; import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; import ru.volgorobot.vrcatalog.model.CoreModel; import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; -public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { +public class FirstLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { TextView textView; ImageView imageView; - CoreModel coreModel; - MainContract.MainActivityView mView; + ViewBinderPresenter viewBinderPresenter; - public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) { + public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, MainContract.MainModel coreModel) { super(itemView); textView = (TextView) itemView.findViewById(R.id.node_name_view); imageView = (ImageView) itemView.findViewById(R.id.arrow_img); - this.coreModel = coreModel; - this.mView = mView; + viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this); } @Override @@ -46,61 +45,14 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { public void onNodeToggled(TreeNode treeNode, boolean expand) { if(expand) { FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue(); - new AsyncTask>>() { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } - - @Override - protected ResultWithErrorCode> doInBackground(Void... voids) { - ArrayList secondLevelModels = null; - try { - secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID()); - } catch (NetworkErrorException networkErrorException) { - return new ResultWithErrorCode<>(secondLevelModels, networkErrorException.getErrorCode()); - } catch (NullPointerException nullPointerException) { - return new ResultWithErrorCode<>(secondLevelModels, 3); - } - - return new ResultWithErrorCode<>(secondLevelModels, 0); - } - - @Override - protected void onPostExecute(ResultWithErrorCode> result) { - super.onPostExecute(result); - switch (result.getErrorCode()) { - case 0: { - ArrayList treeNodes = new ArrayList<>(); - for (int i = 0; i < result.getData().size(); i++) { - TreeNode treeNode1 = new TreeNode(result.getData().get(i)); - treeNode1.setLevel(1); - treeNodes.add(treeNode1); - } - treeNode.setChildren(treeNodes); - mView.refreshTree(); - imageView.animate().rotation(90).setDuration(200).start(); - break; - } - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } - mView.swipeLayoutSetRefreshing(false); - } - }.execute(); + viewBinderPresenter.fetchSecondLevel(firstLevelModel, treeNode); } else { imageView.animate().rotation(0).setDuration(200).start(); } } + + @Override + public void rotateImageView() { + imageView.animate().rotation(90).setDuration(200).start(); + } } 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 1494cb2..fb561bb 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -12,25 +12,24 @@ 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.ViewBinderPresenter; import ru.volgorobot.vrcatalog.additional.NetworkErrorException; import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; import ru.volgorobot.vrcatalog.model.CoreModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; import ru.volgorobot.vrcatalog.model.ThirdLevelModel; -public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { +public class SecondLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { TextView textView; ImageView imageView; - MainContract.MainModel coreModel; - MainContract.MainActivityView mView; + MainContract.ViewBinderPresenter viewBinderPresenter; 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 = coreModel; - this.mView = mView; + viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this); } @Override @@ -48,60 +47,14 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { public void onNodeToggled(TreeNode treeNode, boolean expand) { if(expand) { SecondLevelModel secondLevelModel = (SecondLevelModel) treeNode.getValue(); - new AsyncTask>>() { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } - - @Override - protected ResultWithErrorCode> doInBackground(Void... voids) { - ArrayList thirdLevelModels = null; - try { - thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID()); - } catch (NetworkErrorException networkErrorException) { - return new ResultWithErrorCode<>(thirdLevelModels, networkErrorException.getErrorCode()); - } catch (NullPointerException nullPointerException) { - return new ResultWithErrorCode<>(thirdLevelModels, 3); - } - return new ResultWithErrorCode<>(thirdLevelModels, 0); - } - - @Override - protected void onPostExecute(ResultWithErrorCode> result) { - super.onPostExecute(result); - switch (result.getErrorCode()) { - case 0: { - ArrayList treeNodes = new ArrayList<>(); - for (int i = 0; i < result.getData().size(); i++) { - TreeNode treeNode1 = new TreeNode(result.getData().get(i)); - treeNode1.setLevel(2); - treeNodes.add(treeNode1); - } - treeNode.setChildren(treeNodes); - mView.refreshTree(); - imageView.animate().rotation(90).setDuration(200).start(); - break; - } - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } - mView.swipeLayoutSetRefreshing(false); - } - }.execute(); + viewBinderPresenter.fetchThirdLevel(secondLevelModel, treeNode); } else { imageView.animate().rotation(0).setDuration(200).start(); } } + + @Override + public void rotateImageView() { + imageView.animate().rotation(90).setDuration(200).start(); + } } 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 c3365b9..1545d46 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java @@ -12,6 +12,7 @@ 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.ViewBinderPresenter; import ru.volgorobot.vrcatalog.additional.NetworkErrorException; import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; import ru.volgorobot.vrcatalog.model.CoreModel; @@ -19,17 +20,13 @@ import ru.volgorobot.vrcatalog.model.DetailModel; import ru.volgorobot.vrcatalog.model.ThirdLevelModel; import ru.volgorobot.vrcatalog.view.DetailActivity; -public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder { +public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { TextView textView; - Context context; - MainContract.MainModel coreModel; - MainContract.MainActivityView mView; + MainContract.ViewBinderPresenter viewBinderPresenter; public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) { super(itemView); - this.context = context; - this.coreModel = coreModel; - this.mView = mView; + viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this, context); textView = (TextView) itemView.findViewById(R.id.node_name_view); } @@ -46,57 +43,12 @@ public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder { @Override public void onNodeToggled(TreeNode treeNode, boolean expand) { super.onNodeToggled(treeNode, expand); - new AsyncTask>() { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } + ThirdLevelModel thirdLevelModel = (ThirdLevelModel) treeNode.getValue(); + viewBinderPresenter.fetchDetail(thirdLevelModel); + } - @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("detailContry", 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.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } - mView.swipeLayoutSetRefreshing(false); - } - }.execute(); + @Override + public void rotateImageView() { + // This is unnecessary. } } From cc1c55c30bc226bde2bde1d01a9b0d65367d728b Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 9 Jan 2019 18:51:41 +0400 Subject: [PATCH 26/27] Fixed bug which related with detailCountry --- .../main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java index dc1bdf0..d633d53 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java @@ -178,7 +178,7 @@ public class ViewBinderPresenter implements MainContract.ViewBinderPresenter { intent.putExtra("detailName", result.getData().getName()); intent.putExtra("detailQuantity", result.getData().getNumber().toString()); intent.putExtra("detailPrice", result.getData().getPrice()); - intent.putExtra("detailContry", result.getData().getCountry()); + intent.putExtra("detailCountry", result.getData().getCountry()); intent.putExtra("detailAnalogue", result.getData().getAnalogue()); intent.putExtra("detailDatasheet", result.getData().getDocURL()); intent.putExtra("detailNotes", result.getData().getNotes()); From bafcb5dc14dfee5042e9b9412298cdd001985b23 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 9 Jan 2019 19:30:42 +0400 Subject: [PATCH 27/27] Obtaining intent extras is wrapped in a function that returns an associative array. --- .../vrcatalog/view/DetailActivity.java | 67 +++++++++++++++++-- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java index 5e95a3c..b478d3b 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/DetailActivity.java @@ -8,6 +8,9 @@ import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; +import java.util.HashMap; +import java.util.Map; + import ru.volgorobot.vrcatalog.R; public class DetailActivity extends AppCompatActivity { @@ -29,13 +32,15 @@ public class DetailActivity extends AppCompatActivity { Intent intent = getIntent(); - nameView.setText(intent.getStringExtra("detailName")); - quantityView.setText(intent.getStringExtra("detailQuantity").equals("0") ? "Нет на складе" : intent.getStringExtra("detailQuantity")); - priceView.setText(intent.getStringExtra("detailPrice")); - countryView.setText(intent.getStringExtra("detailCountry")); - analogueView.setText(intent.getStringExtra("detailAnalogue").equals("") ? "Нет аналогов" : intent.getStringExtra("detailAnalogue")); - datasheetView.setText(intent.getStringExtra("detailDatasheet").equals(" ") ? "Нет" : intent.getStringExtra("detailDatasheet")); - notesView.setText(intent.getStringExtra("detailNotes").equals("") ? "Нет описания" : intent.getStringExtra("detailNotes")); + HashMap extrasMap = getIntentExtras(intent); + + nameView.setText(extrasMap.get("detailName")); + quantityView.setText(extrasMap.get("detailQuantity")); + priceView.setText(extrasMap.get("detailPrice")); + countryView.setText(extrasMap.get("detailCountry")); + analogueView.setText(extrasMap.get("detailAnalogue")); + datasheetView.setText(extrasMap.get("detailDatasheet")); + notesView.setText(extrasMap.get("detailNotes")); } void initializeViews() { @@ -49,6 +54,54 @@ public class DetailActivity extends AppCompatActivity { datasheetView.setMovementMethod(LinkMovementMethod.getInstance()); } + HashMap getIntentExtras(Intent intent) { + HashMap hashMap = new HashMap<>(); + String tmp; + + tmp = intent.getStringExtra("detailName"); + hashMap.put("detailName", tmp); + + tmp = intent.getStringExtra("detailQuantity"); + if(tmp.equals("0")) { + tmp = "Под заказ"; + } + hashMap.put("detailQuantity", tmp); + + tmp = intent.getStringExtra("detailPrice"); + if(tmp.equals("0")) { + tmp = "Уточните у продавца"; + } + hashMap.put("detailPrice", tmp); + + tmp = intent.getStringExtra("detailCountry"); + if(tmp.equals("")) { + tmp = "Не указано"; + } + hashMap.put("detailCountry", tmp); + + tmp = intent.getStringExtra("detailAnalogue"); + if(tmp.equals("")) { + tmp = "Нет аналогов"; + } + hashMap.put("detailAnalogue", tmp); + + tmp = intent.getStringExtra("detailDatasheet"); + if(!tmp.matches("\\S")) { + tmp = "Не указано"; + } + hashMap.put("detailDatasheet", tmp); + + tmp = intent.getStringExtra("detailNotes"); + if(tmp != null && tmp.equals("")) { + tmp = "Нет описания"; + } else if (tmp == null){ + tmp = "Нет описания"; + } + hashMap.put("detailNotes", tmp); + + return hashMap; + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) {