Some offline fixes

This commit is contained in:
Thomas 2020-09-18 16:08:52 +02:00
parent 7aab6fbba9
commit b2f1bf2817
4 changed files with 292 additions and 150 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.Instance;
import app.fedilab.fedilabtube.client.entities.InstanceParams; import app.fedilab.fedilabtube.client.entities.InstanceParams;
import app.fedilab.fedilabtube.drawer.InstanceAdapter; import app.fedilab.fedilabtube.drawer.InstanceAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan; import app.fedilab.fedilabtube.helper.RoundedBackgroundSpan;
import app.fedilab.fedilabtube.viewmodel.InstancesVM; import app.fedilab.fedilabtube.viewmodel.InstancesVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
@ -110,134 +109,138 @@ public class InstancePickerActivity extends AppCompatActivity {
} }
}); });
if (peertubeInformation != null && peertubeInformation.getLanguages() != null) {
LinkedHashMap<String, String> languages = new LinkedHashMap<>(peertubeInformation.getLanguages());
checkedItemsLanguage = new boolean[languages.size()];
itemsLabelLanguage = new String[languages.size()];
itemsKeyLanguage = new String[languages.size()];
LinkedHashMap<String, String> languages = new LinkedHashMap<>(peertubeInformation.getLanguages()); pickup_languages.setOnClickListener(v -> {
checkedItemsLanguage = new boolean[languages.size()]; AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this);
itemsLabelLanguage = new String[languages.size()];
itemsKeyLanguage = new String[languages.size()];
pickup_languages.setOnClickListener(v -> {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this);
int i = 0; int i = 0;
if (languages.size() > 0) { if (languages.size() > 0) {
Iterator<Map.Entry<String, String>> it = languages.entrySet().iterator(); Iterator<Map.Entry<String, String>> it = languages.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry<String, String> pair = it.next(); Map.Entry<String, String> pair = it.next();
itemsLabelLanguage[i] = pair.getValue(); itemsLabelLanguage[i] = pair.getValue();
checkedItemsLanguage[i] = false; checkedItemsLanguage[i] = false;
itemsKeyLanguage[i] = pair.getKey(); itemsKeyLanguage[i] = pair.getKey();
it.remove(); it.remove();
i++; i++;
}
}
dialogBuilder.setMultiChoiceItems(itemsLabelLanguage, checkedItemsLanguage, (dialog, which, isChecked) -> {
// The user checked or unchecked a box
checkedItemsLanguage[which] = isChecked;
});
dialogBuilder.setOnDismissListener(dialogInterface -> {
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
stringBuilder.append(between);
List<String> langs = new ArrayList<>();
int j = 0;
for(boolean itemcheked: checkedItemsLanguage){
if( itemcheked) {
langs.add(itemsKeyLanguage[j]);
String lang = itemsLabelLanguage[j];
if (lang != null && lang.trim().toLowerCase().compareTo("null") != 0) {
if (between.length() == 0) between = " ";
String tag = " " + lang + " ";
stringBuilder.append(tag);
stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
stringBuilder.append(" ");
}
} }
j++;
} }
instanceParams.setLanguagesOr(langs);
languages_view.setText(stringBuilder, TextView.BufferType.SPANNABLE);
if (languages_view.getText().toString().trim().length() > 0) {
languages_view.setVisibility(View.VISIBLE);
} else {
languages_view.setVisibility(View.GONE);
}
mainLoader.setVisibility(View.VISIBLE);
viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance);
});
dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialog = dialogBuilder.create(); dialogBuilder.setMultiChoiceItems(itemsLabelLanguage, checkedItemsLanguage, (dialog, which, isChecked) -> {
alertDialog.setTitle(getString(R.string.pickup_languages)); // The user checked or unchecked a box
alertDialog.show(); checkedItemsLanguage[which] = isChecked;
}); });
LinkedHashMap<Integer, String> categories = new LinkedHashMap<>(peertubeInformation.getCategories()); dialogBuilder.setOnDismissListener(dialogInterface -> {
checkedItemsCategory = new boolean[categories.size()];
itemsLabelCategory = new String[categories.size()];
itemsKeyCategory = new int[categories.size()];
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
pickup_categories.setOnClickListener(v -> { String between = "";
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this); stringBuilder.append(between);
int i = 0; List<String> langs = new ArrayList<>();
if (categories.size() > 0) { int j = 0;
Iterator<Map.Entry<Integer, String>> it = categories.entrySet().iterator(); for (boolean itemcheked : checkedItemsLanguage) {
while (it.hasNext()) { if (itemcheked) {
Map.Entry<Integer, String> pair = it.next(); langs.add(itemsKeyLanguage[j]);
itemsLabelCategory[i] = pair.getValue(); String lang = itemsLabelLanguage[j];
itemsKeyCategory[i] = pair.getKey(); if (lang != null && lang.trim().toLowerCase().compareTo("null") != 0) {
checkedItemsCategory[i] = false; if (between.length() == 0) between = " ";
it.remove(); String tag = " " + lang + " ";
i++; stringBuilder.append(tag);
} stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
} stringBuilder.append(" ");
}
dialogBuilder.setMultiChoiceItems(itemsLabelCategory, checkedItemsCategory, (dialog, which, isChecked) -> {
// The user checked or unchecked a box
checkedItemsCategory[which] = isChecked;
});
dialogBuilder.setOnDismissListener(dialogInterface -> {
int j = 0;
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
stringBuilder.append(between);
List<Integer> cats = new ArrayList<>();
for(boolean itemcheked: checkedItemsCategory){
if( itemcheked) {
cats.add(itemsKeyCategory[j]);
String cat = itemsLabelCategory[j];
if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) {
if (between.length() == 0) between = " ";
String tag = " " + cat + " ";
stringBuilder.append(tag);
stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
stringBuilder.append(" ");
} }
j++;
} }
j++; instanceParams.setLanguagesOr(langs);
} languages_view.setText(stringBuilder, TextView.BufferType.SPANNABLE);
instanceParams.setCategoriesOr(cats); if (languages_view.getText().toString().trim().length() > 0) {
categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE); languages_view.setVisibility(View.VISIBLE);
if (categories_view.getText().toString().trim().length() > 0) { } else {
categories_view.setVisibility(View.VISIBLE); languages_view.setVisibility(View.GONE);
} else { }
categories_view.setVisibility(View.GONE); mainLoader.setVisibility(View.VISIBLE);
} viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance);
mainLoader.setVisibility(View.VISIBLE); });
viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss());
});
dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.pickup_languages));
alertDialog.show();
});
}
if (peertubeInformation != null && peertubeInformation.getLanguages() != null) {
LinkedHashMap<Integer, String> categories = new LinkedHashMap<>(peertubeInformation.getCategories());
checkedItemsCategory = new boolean[categories.size()];
itemsLabelCategory = new String[categories.size()];
itemsKeyCategory = new int[categories.size()];
pickup_categories.setOnClickListener(v -> {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(InstancePickerActivity.this);
int i = 0;
if (categories.size() > 0) {
Iterator<Map.Entry<Integer, String>> it = categories.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
itemsLabelCategory[i] = pair.getValue();
itemsKeyCategory[i] = pair.getKey();
checkedItemsCategory[i] = false;
it.remove();
i++;
}
}
dialogBuilder.setMultiChoiceItems(itemsLabelCategory, checkedItemsCategory, (dialog, which, isChecked) -> {
// The user checked or unchecked a box
checkedItemsCategory[which] = isChecked;
});
dialogBuilder.setOnDismissListener(dialogInterface -> {
int j = 0;
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
stringBuilder.append(between);
List<Integer> cats = new ArrayList<>();
for (boolean itemcheked : checkedItemsCategory) {
if (itemcheked) {
cats.add(itemsKeyCategory[j]);
String cat = itemsLabelCategory[j];
if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) {
if (between.length() == 0) between = " ";
String tag = " " + cat + " ";
stringBuilder.append(tag);
stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
stringBuilder.append(" ");
}
}
j++;
}
instanceParams.setCategoriesOr(cats);
categories_view.setText(stringBuilder, TextView.BufferType.SPANNABLE);
if (categories_view.getText().toString().trim().length() > 0) {
categories_view.setVisibility(View.VISIBLE);
} else {
categories_view.setVisibility(View.GONE);
}
mainLoader.setVisibility(View.VISIBLE);
viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance);
});
dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.pickup_categories));
alertDialog.show();
});
}
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setTitle(getString(R.string.pickup_categories));
alertDialog.show();
});
mainLoader.setVisibility(View.VISIBLE); mainLoader.setVisibility(View.VISIBLE);

View File

@ -80,45 +80,47 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(); SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = ""; String between = "";
LinkedHashMap<Integer, String> info_cat = new LinkedHashMap<>(peertubeInformation.getCategories()); if (peertubeInformation != null && peertubeInformation.getCategories() != null) {
if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) { LinkedHashMap<Integer, String> info_cat = new LinkedHashMap<>(peertubeInformation.getCategories());
Iterator<Map.Entry<Integer, Integer>> it = instance.getCategories().entrySet().iterator(); if (instance.getCategories() != null && instance.getCategories().size() > 0 && instance.getSpannableStringBuilder() == null) {
while (it.hasNext()) { Iterator<Map.Entry<Integer, Integer>> it = instance.getCategories().entrySet().iterator();
Map.Entry<Integer, Integer> pair = it.next(); while (it.hasNext()) {
String cat = info_cat.get(pair.getKey()); Map.Entry<Integer, Integer> pair = it.next();
stringBuilder.append(between); String cat = info_cat.get(pair.getKey());
if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) { stringBuilder.append(between);
if (between.length() == 0) between = " "; if (cat != null && cat.trim().toLowerCase().compareTo("null") != 0) {
String tag = " " + cat + " "; if (between.length() == 0) between = " ";
stringBuilder.append(tag); String tag = " " + cat + " ";
stringBuilder.setSpan(new RoundedBackgroundSpan(context), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); stringBuilder.append(tag);
stringBuilder.setSpan(new RoundedBackgroundSpan(context), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
it.remove();
} }
it.remove(); instance.setSpannableStringBuilder(stringBuilder);
} }
instance.setSpannableStringBuilder(stringBuilder);
} }
if( instance.getSpannableStringBuilder() != null) { if (instance.getSpannableStringBuilder() != null) {
holder.tags.setText(instance.getSpannableStringBuilder()); holder.tags.setText(instance.getSpannableStringBuilder());
} }
LinkedHashMap<String, String> info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages()); if (peertubeInformation != null && peertubeInformation.getLanguages() != null) {
StringBuilder languages = new StringBuilder(); LinkedHashMap<String, String> info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages());
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) { StringBuilder languages = new StringBuilder();
Iterator<Map.Entry<Integer, String>> it = instance.getLanguages().entrySet().iterator(); if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {
while (it.hasNext()) { Iterator<Map.Entry<Integer, String>> it = instance.getLanguages().entrySet().iterator();
Map.Entry<Integer, String> pair = it.next(); while (it.hasNext()) {
languages.append(info_lang.get(pair.getValue())).append(" "); Map.Entry<Integer, String> pair = it.next();
it.remove(); languages.append(info_lang.get(pair.getValue())).append(" ");
it.remove();
}
}
if (languages.toString().trim().length() == 0) {
holder.languages.setVisibility(View.GONE);
} else {
holder.languages.setText(languages);
holder.languages.setVisibility(View.VISIBLE);
} }
} }
if (languages.toString().trim().length() == 0) {
holder.languages.setVisibility(View.GONE);
} else {
holder.languages.setText(languages);
holder.languages.setVisibility(View.VISIBLE);
}
if (instance.getDefaultNSFWPolicy().compareTo("do_not_list") != 0) { if (instance.getDefaultNSFWPolicy().compareTo("do_not_list") != 0) {
holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy())); holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy()));
holder.sensitive_content.setVisibility(View.VISIBLE); holder.sensitive_content.setVisibility(View.VISIBLE);

View File

@ -0,0 +1,84 @@
package app.fedilab.fedilabtube.helper;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.HashSet;
import java.util.Set;
/**
* Original work from https://stackoverflow.com/a/25873554
*/
public class NetworkStateReceiver extends BroadcastReceiver {
protected Set<NetworkStateReceiverListener> listeners;
protected Boolean connected;
public NetworkStateReceiver() {
listeners = new HashSet<>();
connected = null;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || intent.getExtras() == null)
return;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
assert manager != null;
NetworkInfo ni = manager.getActiveNetworkInfo();
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
connected = true;
} else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
connected = false;
}
notifyStateToAll();
}
private void notifyStateToAll() {
for (NetworkStateReceiverListener listener : listeners)
notifyState(listener);
}
private void notifyState(NetworkStateReceiverListener listener) {
if (connected == null || listener == null)
return;
if (connected)
listener.networkAvailable();
else
listener.networkUnavailable();
}
public void addListener(NetworkStateReceiverListener l) {
listeners.add(l);
notifyState(l);
}
public void removeListener(NetworkStateReceiverListener l) {
listeners.remove(l);
}
public interface NetworkStateReceiverListener {
void networkAvailable();
void networkUnavailable();
}
}

View File

@ -17,6 +17,7 @@ package app.fedilab.fedilabtube.services;
import android.app.Notification; import android.app.Notification;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
@ -29,13 +30,25 @@ import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.PeertubeInformation; import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
import app.fedilab.fedilabtube.helper.NetworkStateReceiver;
import static app.fedilab.fedilabtube.MainActivity.peertubeInformation; import static app.fedilab.fedilabtube.MainActivity.peertubeInformation;
public class RetrieveInfoService extends Service { public class RetrieveInfoService extends Service implements NetworkStateReceiver.NetworkStateReceiverListener {
static String NOTIFICATION_CHANNEL_ID = "update_info"; static String NOTIFICATION_CHANNEL_ID = "update_info";
private NetworkStateReceiver networkStateReceiver;
public void onCreate() {
super.onCreate();
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.addListener(this);
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
@ -92,4 +105,44 @@ public class RetrieveInfoService extends Service {
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
return null; return null;
} }
@Override
public void onDestroy() {
super.onDestroy();
if (networkStateReceiver != null) {
networkStateReceiver.removeListener(this);
unregisterReceiver(networkStateReceiver);
}
}
@Override
public void networkAvailable() {
Thread thread = new Thread() {
@Override
public void run() {
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<>());
try {
peertubeInformation = new PeertubeAPI(RetrieveInfoService.this).getPeertubeInformation();
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
}
stopForeground(true);
}
};
thread.start();
}
@Override
public void networkUnavailable() {
}
} }