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