Добавил защиту от неправильного URL, удалил фрагмент (вместо этого TreeView находится на MainActivity (SwipeRefreshLayout), добавил SwipeRefreshLayout.

This commit is contained in:
ChronosX88 2018-11-19 19:56:53 +04:00
parent 489dd898f2
commit 8c8927489d
6 changed files with 132 additions and 118 deletions

View File

@ -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<FirstLevelModel> firstLevelObjects = new ArrayList<>();
private List<TreeNode> 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<List<FirstLevelModel>>() {
@Override
public void onResponse(Call<List<FirstLevelModel>> call, Response<List<FirstLevelModel>> 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<List<FirstLevelModel>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
Log.e("ERROR", "getFirstLevel() failed!");
}
});
}
}

View File

@ -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<FirstLevelModel> firstLevelObjects = new ArrayList<>();
private List<TreeNode> 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<List<FirstLevelModel>>() {
@Override
public void onResponse(Call<List<FirstLevelModel>> call, Response<List<FirstLevelModel>> 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<List<FirstLevelModel>> 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()));
}
}
}

View File

@ -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();
}
}
}

View File

@ -1,16 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
tools:showIn="@layout/app_bar_main"
android:orientation="horizontal"
android:id="@+id/swipeRefreshLayout">
<RelativeLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"></RelativeLayout>
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/treeViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.SwipeRefreshLayout>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/treeViewContainer">
</FrameLayout>