From 8c8927489db8d9f907affecc84c52f623320df0d Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Mon, 19 Nov 2018 19:56:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=D1=89=D0=B8=D1=82=D1=83=20=D0=BE=D1=82=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20URL,=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=84=D1=80=D0=B0=D0=B3=D0=BC=D0=B5=D0=BD=D1=82=20(=D0=B2?= =?UTF-8?q?=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D1=8D=D1=82=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20TreeView=20=D0=BD=D0=B0=D1=85=D0=BE=D0=B4=D0=B8=D1=82=D1=81?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B0=20MainActivity=20(SwipeRefreshLayout),=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20SwipeRefreshLayout?= =?UTF-8?q?.?= 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