server selection wip

This commit is contained in:
Stefan Schüller 2019-03-03 00:48:33 +01:00
parent 6274e964e4
commit 440ce569aa
7 changed files with 80 additions and 20 deletions

View File

@ -9,9 +9,12 @@ import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -27,6 +30,9 @@ import net.schueller.peertube.network.RetrofitInstance;
import java.util.ArrayList; 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 { public class SelectServerActivity extends AppCompatActivity {
private ServerAdapter serverAdapter; private ServerAdapter serverAdapter;
@ -46,6 +52,25 @@ public class SelectServerActivity extends AppCompatActivity {
setContentView(R.layout.activity_server_selection); setContentView(R.layout.activity_server_selection);
loadList(); 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();
});
} }

View File

@ -26,14 +26,18 @@ import android.content.res.Configuration;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Patterns; import android.util.Patterns;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import net.schueller.peertube.R; import net.schueller.peertube.R;
import java.util.List; import java.util.List;
import static net.schueller.peertube.helper.Constants.DEFAULT_THEME; import static net.schueller.peertube.helper.Constants.DEFAULT_THEME;
@ -53,14 +57,14 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
return super.onOptionsItemSelected(item); 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 res = "Color not found";
String [ ] themeArray = context.getResources().getStringArray(R.array.themeValues); String[] themeArray = context.getResources().getStringArray(R.array.themeValues);
String [ ] colorArray = context.getResources().getStringArray(R.array.themeArray); String[] colorArray = context.getResources().getStringArray(R.array.themeArray);
for (int i = 0 ; i < themeArray.length ; i++){ for (int i = 0; i < themeArray.length; i++) {
if (themeArray[i].equals(colorId)){ if (themeArray[i].equals(colorId)) {
res = colorArray[i]; res = colorArray[i];
break; break;
} }
@ -76,12 +80,12 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
String stringValue = value.toString(); String stringValue = value.toString();
// check URL is valid // check URL is valid
if (preference.getKey().equals("pref_api_base") && !Patterns.WEB_URL.matcher(stringValue).matches()) { // 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(); // Toast.makeText(preference.getContext(), R.string.invalid_url, Toast.LENGTH_LONG).show();
return false; // return false;
} // }
// Check if Theme color has change & Provide selected color // 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); stringValue = getSelectedColor(preference.getContext(), stringValue);
@ -212,6 +216,18 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
} }
return super.onOptionsItemSelected(item); 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;
}
} }
} }

View File

@ -24,6 +24,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import net.schueller.peertube.R; import net.schueller.peertube.R;
@ -77,10 +78,15 @@ public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.AccountVie
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = sharedPref.edit(); SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("pref_api_base", "https://" + serverList.get(position).getHost()); String serverUrl = APIUrlHelper.cleanServerUrl(serverList.get(position).getHost());
editor.putString("pref_api_base", serverUrl);
editor.apply(); editor.apply();
activity.finish(); activity.finish();
Toast.makeText(activity, activity.getString(R.string.server_selection_set_server, serverUrl), Toast.LENGTH_LONG).show();
}); });
// //

View File

@ -48,4 +48,19 @@ public class APIUrlHelper{
public static String getServerIndexUrl(Context context) { public static String getServerIndexUrl(Context context) {
return "https://instances.joinpeertube.org/api/v1/"; return "https://instances.joinpeertube.org/api/v1/";
} }
public static String cleanServerUrl(String url) {
String cleanUrl = url.toLowerCase();
if (!cleanUrl.startsWith("http")) {
cleanUrl = "https://" + cleanUrl;
}
if (cleanUrl.endsWith("/")) {
cleanUrl = cleanUrl.substring(0, cleanUrl.length() - 1);
}
return cleanUrl;
}
} }

View File

@ -24,14 +24,14 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textSize="16sp" android:textSize="16sp"
android:text="Select a Server from the list below or enter it directly." /> android:text="@string/server_selection_select_a_server" />
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<AutoCompleteTextView <AutoCompleteTextView
android:hint="PeerTube Server URL" android:hint="@string/server_selection_peertube_server_url"
android:id="@+id/serverSelectedUrl" android:id="@+id/serverSelectedUrl"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -306,6 +306,10 @@
<string name="server_selection_signup_allowed">Signup Allowed: %s</string> <string name="server_selection_signup_allowed">Signup Allowed: %s</string>
<string name="server_selection_signup_allowed_yes">Yes</string> <string name="server_selection_signup_allowed_yes">Yes</string>
<string name="server_selection_signup_allowed_no">No</string> <string name="server_selection_signup_allowed_no">No</string>
<string name="server_selection_set_server">Server set to: %s</string>
<string name="server_selection_select_a_server">Select a Server from the list below or enter it directly.</string>
<string name="server_selection_peertube_server_url">PeerTube Server URL</string>
<string name="title_activity_account">Account</string> <string name="title_activity_account">Account</string>
<string name="menu_video_more_report">Report</string> <string name="menu_video_more_report">Report</string>

View File

@ -1,13 +1,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference <Preference
android:capitalize="words"
android:defaultValue="@string/pref_default_api_base_url"
android:inputType="textUri"
android:key="pref_api_base" android:key="pref_api_base"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_peertube_server" /> android:title="@string/pref_title_peertube_server" />
<!--<SwitchPreference--> <!--<SwitchPreference-->