Small fixes

This commit is contained in:
Thomas 2020-09-17 18:37:44 +02:00
parent 11209a5334
commit e503a5ec95
5 changed files with 39 additions and 48 deletions

View File

@ -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<Instance> 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<Integer, String> categories = new LinkedHashMap<>(peertubeInformation.getCategories());
checkedItems = new boolean[categories.size()];
itemsLabel = new String[categories.size()];
pickup_categories.setOnClickListener(v -> {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this);
LinkedHashMap<Integer, String> categories = peertubeInformation.getCategories();
int i = 0;
if (categories != null && categories.size() > 0) {
if (categories.size() > 0) {
Iterator<Map.Entry<Integer, String>> it = categories.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> 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<Instance> instances = apiResponse.getInstances();
if ((instances == null || instances.size() == 0)) {
textviewNoAction.setVisibility(View.VISIBLE);
return;
}
this.instances.addAll(instances);
instanceAdapter.notifyItemRangeInserted(0, instances.size());
} 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));
}
}
}

View File

@ -72,6 +72,7 @@ public class MainActivity extends AppCompatActivity {
navView.inflateMenu(R.menu.bottom_nav_menu);
}
if (peertubeInformation == null) {
peertubeInformation = new PeertubeInformation();
peertubeInformation.setCategories(new LinkedHashMap<>());
peertubeInformation.setLanguages(new LinkedHashMap<>());
@ -86,6 +87,7 @@ public class MainActivity extends AppCompatActivity {
e.printStackTrace();
}
}).start();
}
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.

View File

@ -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<Instance> instances = parseInstances(jsonArray);
apiResponse.setInstances(instances);
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();

View File

@ -76,11 +76,11 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
LinkedHashMap<Integer, String> info_cat = new LinkedHashMap<>(peertubeInformation.getCategories());
if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) {
Iterator<Map.Entry<Integer, Integer>> it = instance.getCategories().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> pair = it.next();
LinkedHashMap<Integer, String> 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<RecyclerView.ViewHolde
holder.tags.setText(instance.getSpannableStringBuilder());
}
LinkedHashMap<String, String> info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages());
StringBuilder languages = new StringBuilder();
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {
Iterator<Map.Entry<Integer, String>> it = instance.getLanguages().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
LinkedHashMap<String, String> info_lang = peertubeInformation.getLanguages();
languages.append(info_lang.get(pair.getValue())).append(" ");
it.remove();
}

View File

@ -52,7 +52,7 @@
<TextView
android:layout_marginTop="10dp"
android:layout_marginTop="14dp"
android:id="@+id/text_spinner"
app:layout_constraintTop_toBottomOf="@+id/categories_view"
app:layout_constraintStart_toStartOf="parent"
@ -60,13 +60,14 @@
android:layout_height="wrap_content"
android:text="@string/sensitive_video"
android:labelFor="@+id/sensitive"/>
<Spinner
app:layout_constraintTop_toBottomOf="@+id/categories_view"
app:layout_constraintStart_toEndOf="@+id/text_spinner"
android:id="@+id/sensitive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp" />
android:layout_marginStart="10dp" />
<LinearLayout