From e503a5ec95ee9906c2c1c1bb84a0a1371e428934 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 17 Sep 2020 18:37:44 +0200 Subject: [PATCH] Small fixes --- .../fedilabtube/InstancePickerActivity.java | 42 +++++++------------ .../app/fedilab/fedilabtube/MainActivity.java | 30 ++++++------- .../fedilabtube/client/PeertubeAPI.java | 5 ++- .../fedilabtube/drawer/InstanceAdapter.java | 5 +-- .../res/layout/activity_instance_picker.xml | 5 ++- 5 files changed, 39 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java b/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java index 0b59382..c050997 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/InstancePickerActivity.java @@ -44,7 +44,6 @@ import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.entities.Instance; import app.fedilab.fedilabtube.client.entities.InstanceParams; import app.fedilab.fedilabtube.drawer.InstanceAdapter; -import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan; import app.fedilab.fedilabtube.viewmodel.InstancesVM; import es.dmoral.toasty.Toasty; @@ -56,14 +55,12 @@ import static app.fedilab.fedilabtube.MainActivity.peertubeInformation; public class InstancePickerActivity extends AppCompatActivity { - LinearLayoutManager mLayoutManager; private RelativeLayout mainLoader, textviewNoAction; - private List instances; - private InstanceAdapter instanceAdapter; boolean[] checkedItems; String[] itemsLabel; InstanceParams instanceParams; private TextView categories_view; + private InstancesVM viewModel; @Override protected void onCreate(Bundle savedInstanceState) { @@ -74,9 +71,7 @@ public class InstancePickerActivity extends AppCompatActivity { setContentView(R.layout.activity_instance_picker); - instances = new ArrayList<>(); - RecyclerView lv_instances = findViewById(R.id.lv_instances); mainLoader = findViewById(R.id.loader); textviewNoAction = findViewById(R.id.no_action); mainLoader.setVisibility(View.VISIBLE); @@ -91,13 +86,14 @@ public class InstancePickerActivity extends AppCompatActivity { sensitive.setAdapter(adapterChannel); - InstancesVM viewModel = new ViewModelProvider(InstancePickerActivity.this).get(InstancesVM.class); + viewModel = new ViewModelProvider(InstancePickerActivity.this).get(InstancesVM.class); sensitive.setSelection(1, false); sensitive.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { instanceParams.setNsfwPolicy(channelSensitive[position]); + mainLoader.setVisibility(View.VISIBLE); viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, apiResponse -> manageVIewInstance(apiResponse)); } @@ -107,17 +103,16 @@ public class InstancePickerActivity extends AppCompatActivity { } }); - checkedItems = new boolean[peertubeInformation.getCategories().size()]; - itemsLabel= new String[peertubeInformation.getCategories().size()]; + LinkedHashMap categories = new LinkedHashMap<>(peertubeInformation.getCategories()); + checkedItems = new boolean[categories.size()]; + itemsLabel = new String[categories.size()]; - - pickup_categories.setOnClickListener(v->{ + pickup_categories.setOnClickListener(v -> { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this); - LinkedHashMap categories = peertubeInformation.getCategories(); int i = 0; - if (categories != null && categories.size() > 0) { + if (categories.size() > 0) { Iterator> it = categories.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); @@ -155,6 +150,7 @@ public class InstancePickerActivity extends AppCompatActivity { } instanceParams.setCategoriesOr(cats); categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); + mainLoader.setVisibility(View.VISIBLE); viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); }); dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); @@ -164,21 +160,10 @@ public class InstancePickerActivity extends AppCompatActivity { alertDialog.show(); }); - - mainLoader.setVisibility(View.VISIBLE); - instanceAdapter = new InstanceAdapter(this.instances); - - lv_instances.setAdapter(instanceAdapter); - mLayoutManager = new LinearLayoutManager(InstancePickerActivity.this); - lv_instances.setLayoutManager(mLayoutManager); - - - setTitle(R.string.instances_picker); - instanceParams = new InstanceParams(); instanceParams.setNsfwPolicy(channelSensitive[1]); viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); @@ -206,9 +191,12 @@ public class InstancePickerActivity extends AppCompatActivity { List instances = apiResponse.getInstances(); if ((instances == null || instances.size() == 0)) { textviewNoAction.setVisibility(View.VISIBLE); - return; + } else { + textviewNoAction.setVisibility(View.GONE); + RecyclerView lv_instances = findViewById(R.id.lv_instances); + InstanceAdapter instanceAdapter = new InstanceAdapter(instances); + lv_instances.setAdapter(instanceAdapter); + lv_instances.setLayoutManager(new LinearLayoutManager(InstancePickerActivity.this)); } - this.instances.addAll(instances); - instanceAdapter.notifyItemRangeInserted(0, instances.size()); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 80d5231..c98380d 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -72,20 +72,22 @@ public class MainActivity extends AppCompatActivity { navView.inflateMenu(R.menu.bottom_nav_menu); } - peertubeInformation = new PeertubeInformation(); - peertubeInformation.setCategories(new LinkedHashMap<>()); - peertubeInformation.setLanguages(new LinkedHashMap<>()); - peertubeInformation.setLicences(new LinkedHashMap<>()); - peertubeInformation.setPrivacies(new LinkedHashMap<>()); - peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>()); - peertubeInformation.setTranslations(new LinkedHashMap<>()); - new Thread(() -> { - try { - peertubeInformation = new PeertubeAPI(MainActivity.this).getPeertubeInformation(); - } catch (HttpsConnection.HttpsConnectionException e) { - e.printStackTrace(); - } - }).start(); + if (peertubeInformation == null) { + peertubeInformation = new PeertubeInformation(); + peertubeInformation.setCategories(new LinkedHashMap<>()); + peertubeInformation.setLanguages(new LinkedHashMap<>()); + peertubeInformation.setLicences(new LinkedHashMap<>()); + peertubeInformation.setPrivacies(new LinkedHashMap<>()); + peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>()); + peertubeInformation.setTranslations(new LinkedHashMap<>()); + new Thread(() -> { + try { + peertubeInformation = new PeertubeAPI(MainActivity.this).getPeertubeInformation(); + } catch (HttpsConnection.HttpsConnectionException e) { + e.printStackTrace(); + } + }).start(); + } // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index 53b976a..1e60914 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -599,8 +599,8 @@ public class PeertubeAPI { if (instanceParams != null) { if (instanceParams.getCategoriesOr() != null && instanceParams.getCategoriesOr().size() > 0) { StringBuilder parameters = new StringBuilder(); - for (String lang : instanceParams.getLanguagesOr()) - parameters.append("categoriesOr[]=").append(lang).append("&"); + for (int category : instanceParams.getCategoriesOr()) + parameters.append("categoriesOr[]=").append(category).append("&"); parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(15)); params.put("categoriesOr[]", parameters.toString()); } @@ -622,6 +622,7 @@ public class PeertubeAPI { List instances = parseInstances(jsonArray); apiResponse.setInstances(instances); } catch (HttpsConnection.HttpsConnectionException e) { + e.printStackTrace(); setError(e.getStatusCode(), e); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java index 1db2644..5250ce3 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/InstanceAdapter.java @@ -76,11 +76,11 @@ public class InstanceAdapter extends RecyclerView.Adapter info_cat = new LinkedHashMap<>(peertubeInformation.getCategories()); if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) { Iterator> it = instance.getCategories().entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); - LinkedHashMap info_cat = peertubeInformation.getCategories(); String cat = info_cat.get(pair.getKey()); stringBuilder.append(between); if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) { @@ -97,13 +97,12 @@ public class InstanceAdapter extends RecyclerView.Adapter info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages()); StringBuilder languages = new StringBuilder(); if (instance.getLanguages() != null && instance.getLanguages().size() > 0) { Iterator> it = instance.getLanguages().entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); - LinkedHashMap info_lang = peertubeInformation.getLanguages(); languages.append(info_lang.get(pair.getValue())).append(" "); it.remove(); } diff --git a/app/src/main/res/layout/activity_instance_picker.xml b/app/src/main/res/layout/activity_instance_picker.xml index a39a229..2c0bd6e 100644 --- a/app/src/main/res/layout/activity_instance_picker.xml +++ b/app/src/main/res/layout/activity_instance_picker.xml @@ -52,7 +52,7 @@ + + android:layout_marginStart="10dp" />