From b9b3bc45ad0b37dcab7b91d300c41043fc552f65 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 16 Nov 2018 21:09:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=83?= =?UTF-8?q?=D0=BC=D0=B5=D0=B5=D1=82=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B8=20=D0=BF=D0=B0=D1=80=D1=81=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D1=8B=20=D0=BE=D1=82=20?= =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0.=20=D0=9D=D0=B5?= =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?TreeView=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=83=D1=80=D0=BE=D0=B2=D0=BD=D1=8F=20=D0=B2?= =?UTF-8?q?=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