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.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;
@ -110,7 +109,7 @@ 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()];
@ -175,7 +174,9 @@ public class InstancePickerActivity extends AppCompatActivity {
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()];
@ -238,6 +239,8 @@ public class InstancePickerActivity extends AppCompatActivity {
alertDialog.setTitle(getString(R.string.pickup_categories));
alertDialog.show();
});
}
mainLoader.setVisibility(View.VISIBLE);

View File

@ -80,6 +80,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = "";
if (peertubeInformation != null && peertubeInformation.getCategories() != null) {
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();
@ -97,10 +98,12 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
instance.setSpannableStringBuilder(stringBuilder);
}
}
if (instance.getSpannableStringBuilder() != null) {
holder.tags.setText(instance.getSpannableStringBuilder());
}
if (peertubeInformation != null && peertubeInformation.getLanguages() != null) {
LinkedHashMap<String, String> info_lang = new LinkedHashMap<>(peertubeInformation.getLanguages());
StringBuilder languages = new StringBuilder();
if (instance.getLanguages() != null && instance.getLanguages().size() > 0) {
@ -111,14 +114,13 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
it.remove();
}
}
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) {
holder.sensitive_content.setText(context.getString(R.string.sensitive_content, instance.getDefaultNSFWPolicy()));
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.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.IBinder;
@ -29,13 +30,25 @@ import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
import app.fedilab.fedilabtube.helper.NetworkStateReceiver;
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";
private NetworkStateReceiver networkStateReceiver;
public void onCreate() {
super.onCreate();
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.addListener(this);
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@ -92,4 +105,44 @@ public class RetrieveInfoService extends Service {
public IBinder onBind(Intent intent) {
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() {
}
}