1
0
mirror of https://framagit.org/tom79/nitterizeme synced 2025-03-13 01:40:04 +01:00

Some improvements

This commit is contained in:
Thomas 2021-10-25 18:56:09 +02:00
parent bb2f0e6b82
commit 98ce97656b
4 changed files with 21 additions and 57 deletions

View File

@ -159,6 +159,7 @@ public class InstanceActivity extends AppCompatActivity {
invidiousAdapter.evalLatency();
nitterAdapter.evalLatency();
bibliogramAdapter.evalLatency();
tedditAdapter.evalLatency();
}
);

View File

@ -14,6 +14,7 @@ package app.fedilab.nitterizeme.adapters;
* You should have received a copy of the GNU General Public License along with UntrackMe; if not,
* see <http://www.gnu.org/licenses>. */
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
@ -73,7 +74,6 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
SharedPreferences sharedpreferences = context.getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
//Reset checked instances by type when tipping
holder.binding.checkboxInstance.setText(instance.getDomain());
if (instance.getLatency() == -1) {
holder.binding.latency.setVisibility(View.GONE);
@ -150,6 +150,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
@SuppressLint("NotifyDataSetChanged")
public void evalLatency() {
for (Instance instance : instances) {
instance.setLatency(0);
@ -158,7 +159,7 @@ public class InstanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public void run() {
long ping = Utils.ping(instance.getDomain());
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
@SuppressLint("NotifyDataSetChanged") Runnable myRunnable = () -> {
instance.setLatency(ping);
notifyDataSetChanged();
};

View File

@ -69,8 +69,6 @@ public class SearchInstanceVM extends AndroidViewModel {
@Override
public void run() {
List<Instance> instances = getInstancesFromFedilabApp();
List<Instance> bibliogramInstances = getInstancesFromBibliogramArt();
instances.addAll(bibliogramInstances);
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> instancesMLD.setValue(instances);
mainHandler.post(myRunnable);
@ -103,6 +101,7 @@ public class SearchInstanceVM extends AndroidViewModel {
String defaultInvidious = sharedpreferences.getString(SET_INVIDIOUS_HOST, DEFAULT_INVIDIOUS_HOST);
String defaultNitter = sharedpreferences.getString(SET_NITTER_HOST, DEFAULT_NITTER_HOST);
String defaultTeddit = sharedpreferences.getString(SET_TEDDIT_HOST, DEFAULT_TEDDIT_HOST);
String defaultBibliogram = sharedpreferences.getString(SET_BIBLIOGRAM_HOST, DEFAULT_BIBLIOGRAM_HOST);
if (response != null) {
try {
@ -110,6 +109,7 @@ public class SearchInstanceVM extends AndroidViewModel {
JSONArray jsonArrayInvidious = jsonObject.getJSONArray("invidious");
JSONArray jsonArrayNitter = jsonObject.getJSONArray("nitter");
JSONArray jsonArrayTeddit = jsonObject.getJSONArray("teddit");
JSONArray jsonArrayBibliogram = jsonObject.getJSONArray("bibliogram");
for (int i = 0; i < jsonArrayInvidious.length(); i++) {
Instance instance = new Instance();
String domain = jsonArrayInvidious.getJSONObject(i).getString("domain");
@ -138,6 +138,20 @@ public class SearchInstanceVM extends AndroidViewModel {
}
instances.add(instance);
}
for (int i = 0; i < jsonArrayBibliogram.length(); i++) {
Instance instance = new Instance();
String domain = jsonArrayBibliogram.getJSONObject(i).getString("domain");
boolean cloudFlare = jsonArrayBibliogram.getJSONObject(i).getBoolean("cloudflare");
String locale = jsonArrayBibliogram.getJSONObject(i).getString("locale");
instance.setDomain(domain);
instance.setCloudflare(cloudFlare);
instance.setLocale(locale);
instance.setType(Instance.instanceType.BIBLIOGRAM);
if (defaultBibliogram != null && domain.compareTo(defaultBibliogram) == 0) {
instance.setChecked(true);
}
instances.add(instance);
}
for (int i = 0; i < jsonArrayTeddit.length(); i++) {
Instance instance = new Instance();
String domain = jsonArrayTeddit.getJSONObject(i).getString("domain");
@ -163,56 +177,4 @@ public class SearchInstanceVM extends AndroidViewModel {
}
private List<Instance> getInstancesFromBibliogramArt() {
HttpsURLConnection httpsURLConnection;
ArrayList<Instance> instances = new ArrayList<>();
try {
String instances_url = "https://bibliogram.art/api/instances";
URL url = new URL(instances_url);
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(10 * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
httpsURLConnection.setRequestProperty("Accept", "application/json");
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setDefaultUseCaches(true);
httpsURLConnection.setUseCaches(true);
String response = null;
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
java.util.Scanner s = new java.util.Scanner(httpsURLConnection.getInputStream()).useDelimiter("\\A");
response = s.hasNext() ? s.next() : "";
}
httpsURLConnection.getInputStream().close();
SharedPreferences sharedpreferences = getApplication().getApplicationContext().getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
String defaultBibliogram = sharedpreferences.getString(SET_BIBLIOGRAM_HOST, DEFAULT_BIBLIOGRAM_HOST);
if (response != null) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArrayBibliogram = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArrayBibliogram.length(); i++) {
Instance instance = new Instance();
String url_bibliogram = jsonArrayBibliogram.getJSONObject(i).getString("address");
URL urlBibliogram = new URL(url_bibliogram);
String domain = urlBibliogram.getHost();
boolean cloudFlare = jsonArrayBibliogram.getJSONObject(i).getBoolean("using_cloudflare");
String locale = jsonArrayBibliogram.getJSONObject(i).getString("country");
instance.setDomain(domain);
instance.setCloudflare(cloudFlare);
instance.setLocale(locale);
instance.setType(Instance.instanceType.BIBLIOGRAM);
if (defaultBibliogram != null && domain.compareTo(defaultBibliogram) == 0) {
instance.setChecked(true);
}
instances.add(instance);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return instances;
}
}

View File

@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/fab_margin"
android:padding="@dimen/fab_margin"
android:orientation="vertical">
<RelativeLayout