From 440ce569aa6ec8205647285c476cfa5dd9d62485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Schu=CC=88ller?= Date: Sun, 3 Mar 2019 00:48:33 +0100 Subject: [PATCH] server selection wip --- .../activity/SelectServerActivity.java | 25 +++++++++++++ .../peertube/activity/SettingsActivity.java | 36 +++++++++++++------ .../peertube/adapter/ServerAdapter.java | 8 ++++- .../peertube/helper/APIUrlHelper.java | 15 ++++++++ .../res/layout/activity_server_selection.xml | 4 +-- app/src/main/res/values/strings.xml | 4 +++ app/src/main/res/xml/pref_general.xml | 8 +---- 7 files changed, 80 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/activity/SelectServerActivity.java b/app/src/main/java/net/schueller/peertube/activity/SelectServerActivity.java index 8715b15..d29a8fa 100644 --- a/app/src/main/java/net/schueller/peertube/activity/SelectServerActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/SelectServerActivity.java @@ -9,9 +9,12 @@ import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.util.Log; import android.view.View; +import android.widget.Button; import android.widget.TextView; import android.widget.Toast; @@ -27,6 +30,9 @@ import net.schueller.peertube.network.RetrofitInstance; import java.util.ArrayList; +import static net.schueller.peertube.helper.Constants.DEFAULT_THEME; +import static net.schueller.peertube.helper.Constants.THEME_PREF_KEY; + public class SelectServerActivity extends AppCompatActivity { private ServerAdapter serverAdapter; @@ -46,6 +52,25 @@ public class SelectServerActivity extends AppCompatActivity { setContentView(R.layout.activity_server_selection); loadList(); + + // set url + TextView selectedUrl = findViewById(R.id.serverSelectedUrl); + selectedUrl.setText(APIUrlHelper.getUrl(SelectServerActivity.this)); + + Button setServerButton = findViewById(R.id.server_selection_set); + setServerButton.setOnClickListener(v -> { + + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences.Editor editor = sharedPref.edit(); + + String serverUrl = APIUrlHelper.cleanServerUrl(selectedUrl.getText().toString()); + + editor.putString("pref_api_base", serverUrl); + editor.apply(); + + this.finish(); + }); + } diff --git a/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java b/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java index 3d7352d..0c8f507 100644 --- a/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/SettingsActivity.java @@ -26,14 +26,18 @@ import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.preference.Preference; + import androidx.appcompat.app.ActionBar; + import android.preference.PreferenceFragment; import android.preference.PreferenceManager; +import android.preference.PreferenceScreen; import android.util.Patterns; import android.view.MenuItem; import android.widget.Toast; import net.schueller.peertube.R; + import java.util.List; import static net.schueller.peertube.helper.Constants.DEFAULT_THEME; @@ -53,14 +57,14 @@ public class SettingsActivity extends AppCompatPreferenceActivity { return super.onOptionsItemSelected(item); } - private static String getSelectedColor(Context context, String colorId){ + private static String getSelectedColor(Context context, String colorId) { String res = "Color not found"; - String [ ] themeArray = context.getResources().getStringArray(R.array.themeValues); - String [ ] colorArray = context.getResources().getStringArray(R.array.themeArray); + String[] themeArray = context.getResources().getStringArray(R.array.themeValues); + String[] colorArray = context.getResources().getStringArray(R.array.themeArray); - for (int i = 0 ; i < themeArray.length ; i++){ - if (themeArray[i].equals(colorId)){ + for (int i = 0; i < themeArray.length; i++) { + if (themeArray[i].equals(colorId)) { res = colorArray[i]; break; } @@ -76,12 +80,12 @@ public class SettingsActivity extends AppCompatPreferenceActivity { String stringValue = value.toString(); // check URL is valid - if (preference.getKey().equals("pref_api_base") && !Patterns.WEB_URL.matcher(stringValue).matches()) { - Toast.makeText(preference.getContext(), R.string.invalid_url, Toast.LENGTH_LONG).show(); - return false; - } +// if (preference.getKey().equals("pref_api_base") && !Patterns.WEB_URL.matcher(stringValue).matches()) { +// Toast.makeText(preference.getContext(), R.string.invalid_url, Toast.LENGTH_LONG).show(); +// return false; +// } // Check if Theme color has change & Provide selected color - else if (preference.getKey().equals("pref_theme")) { + if (preference.getKey().equals("pref_theme")) { stringValue = getSelectedColor(preference.getContext(), stringValue); @@ -212,6 +216,18 @@ public class SettingsActivity extends AppCompatPreferenceActivity { } return super.onOptionsItemSelected(item); } + + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, + Preference preference) { + String key = preference.getKey(); + if (key.equals("pref_api_base")) { + Intent intentServer = new Intent(preference.getContext(), SelectServerActivity.class); + startActivity(intentServer); + return true; + } + return false; + } } } \ No newline at end of file diff --git a/app/src/main/java/net/schueller/peertube/adapter/ServerAdapter.java b/app/src/main/java/net/schueller/peertube/adapter/ServerAdapter.java index 2a4dcb5..0e3de08 100644 --- a/app/src/main/java/net/schueller/peertube/adapter/ServerAdapter.java +++ b/app/src/main/java/net/schueller/peertube/adapter/ServerAdapter.java @@ -24,6 +24,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import android.widget.Toast; import net.schueller.peertube.R; @@ -77,10 +78,15 @@ public class ServerAdapter extends RecyclerView.Adapter + android:text="@string/server_selection_select_a_server" /> Signup Allowed: %s Yes No + Server set to: %s + Select a Server from the list below or enter it directly. + PeerTube Server URL + Account Report diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 56eaa7a..3b1e6a1 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -1,13 +1,7 @@ -