Storing peerID and peerDHT in static class, showing peerID in settings

This commit is contained in:
ChronosX88 2019-03-15 22:09:57 +04:00
parent 127bccd272
commit 64a527df78
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 30 additions and 4 deletions

View File

@ -3,6 +3,8 @@ package io.github.chronosx88.influence.helpers;
import android.app.Application;
import android.content.Context;
import net.tomp2p.dht.PeerDHT;
import io.github.chronosx88.influence.observable.MainObservable;
/**
@ -12,6 +14,8 @@ import io.github.chronosx88.influence.observable.MainObservable;
public class AppHelper extends Application {
private static Application instance;
private static MainObservable observable;
private static String peerID;
private static PeerDHT peerDHT;
@Override
public void onCreate() {
@ -20,9 +24,17 @@ public class AppHelper extends Application {
observable = new MainObservable();
}
public static void storePeerID(String peerID1) { peerID = peerID1; }
public static void storePeerDHT(PeerDHT peerDHT1) { peerDHT = peerDHT1; }
public static Context getContext() {
return instance.getApplicationContext();
}
public static MainObservable getObservable() { return observable; }
public static String getPeerID() { return peerID; }
public static PeerDHT getPeerDHT() { return peerDHT; }
}

View File

@ -8,7 +8,6 @@ import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.dht.PeerDHT;
import net.tomp2p.futures.FutureBootstrap;
import net.tomp2p.futures.FutureDiscover;
import net.tomp2p.nat.FutureNAT;
import net.tomp2p.nat.FutureRelayNAT;
import net.tomp2p.nat.PeerBuilderNAT;
import net.tomp2p.nat.PeerNAT;
@ -24,8 +23,6 @@ import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import io.github.chronosx88.influence.contracts.MainModelContract;
@ -131,6 +128,8 @@ public class MainModel implements MainModelContract {
} catch (JSONException ex) {
ex.printStackTrace();
}
AppHelper.storePeerID(preferences.getString("peerID", null));
AppHelper.storePeerDHT(peerDHT);
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -1,13 +1,28 @@
package io.github.chronosx88.influence.views.fragments;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import androidx.preference.PreferenceFragmentCompat;
import io.github.chronosx88.influence.R;
import io.github.chronosx88.influence.helpers.AppHelper;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
setPreferencesFromResource(R.xml.main_settings, s);
// Load the Preferences from the XML file
addPreferencesFromResource(R.xml.main_settings);
getPreferenceScreen().getPreference(0).setSummary(AppHelper.getPeerID());
getPreferenceScreen().getPreference(0).setOnPreferenceClickListener((preference -> {
ClipboardManager clipboard = (ClipboardManager) AppHelper.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", AppHelper.getPeerID());
clipboard.setPrimaryClip(clip);
Toast.makeText(AppHelper.getContext(), "Скопировано в буфер обмена!", Toast.LENGTH_SHORT).show();
return false;
}));
}
}