diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ecdc571e2..704ea4a43 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -355,9 +355,6 @@ - @@ -402,11 +399,6 @@ android:name=".peertube.activities.AllLocalPlaylistsActivity" android:configChanges="orientation|screenSize" android:windowSoftInputMode="stateAlwaysHidden" /> - - - . */ -import static app.fedilab.android.peertube.activities.PeertubeMainActivity.PICK_INSTANCE_SURF; -import static app.fedilab.android.peertube.activities.PeertubeMainActivity.showRadioButtonDialogFullInstances; +import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE; +import static app.fedilab.android.mastodon.helper.Helper.addFragment; +import static app.fedilab.android.peertube.activities.PeertubeMainActivity.INSTANCE_ADDRESS; +import static app.fedilab.android.peertube.activities.PeertubeMainActivity.PICK_INSTANCE; import android.annotation.SuppressLint; import android.app.Activity; -import android.content.Intent; +import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Handler; import android.view.MenuItem; import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.Toast; +import androidx.appcompat.app.AlertDialog; import androidx.lifecycle.ViewModelProvider; +import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.LinearLayoutManager; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -37,11 +47,15 @@ import app.fedilab.android.databinding.ActivityManageInstancesPeertubeBinding; import app.fedilab.android.mastodon.activities.BaseBarActivity; import app.fedilab.android.peertube.client.RetrofitPeertubeAPI; import app.fedilab.android.peertube.client.data.InstanceData; +import app.fedilab.android.peertube.client.entities.WellKnownNodeinfo; import app.fedilab.android.peertube.drawer.AboutInstanceAdapter; +import app.fedilab.android.peertube.fragment.FragmentLoginPickInstancePeertube; import app.fedilab.android.peertube.helper.Helper; +import app.fedilab.android.peertube.helper.HelperInstance; import app.fedilab.android.peertube.sqlite.StoredInstanceDAO; import app.fedilab.android.peertube.viewmodel.InfoInstanceVM; import app.fedilab.android.sqlite.Sqlite; +import es.dmoral.toasty.Toasty; public class ManageInstancesActivity extends BaseBarActivity implements AboutInstanceAdapter.AllInstancesRemoved { @@ -64,7 +78,7 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns binding.loader.setVisibility(View.VISIBLE); binding.noAction.setVisibility(View.GONE); binding.lvInstances.setVisibility(View.GONE); - binding.actionButton.setOnClickListener(v -> showRadioButtonDialogFullInstances(ManageInstancesActivity.this, true)); + binding.actionButton.setOnClickListener(v -> showRadioButtonDialogFullInstances(ManageInstancesActivity.this)); aboutInstances = new ArrayList<>(); aboutInstanceAdapter = new AboutInstanceAdapter(this.aboutInstances); aboutInstanceAdapter.allInstancesRemoved = this; @@ -76,6 +90,70 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns viewModelInfoInstance.getInstances().observe(ManageInstancesActivity.this, this::manageVIewInfoInstance); } + @SuppressLint("ApplySharedPref") + private void showRadioButtonDialogFullInstances(Activity activity) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); + AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(activity, app.fedilab.android.mastodon.helper.Helper.dialogStyle()); + alt_bld.setTitle(R.string.instance_choice); + String instance = HelperInstance.getLiveInstance(activity); + final EditText input = new EditText(activity); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT); + input.setLayoutParams(lp); + alt_bld.setView(input); + input.setText(instance); + alt_bld.setPositiveButton(R.string.validate, + (dialog, which) -> new Thread(() -> { + try { + String newInstance = input.getText().toString().trim(); + if (!newInstance.startsWith("http")) { + newInstance = "http://" + newInstance; + } + URL url = new URL(newInstance); + newInstance = url.getHost(); + + WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitPeertubeAPI(activity, newInstance, null).getNodeInfo(); + if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("peertube") == 0) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_INSTANCE, newInstance); + editor.commit(); + newInstance = newInstance.trim().toLowerCase(); + InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance(); + SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + new StoredInstanceDAO(activity, db).insertInstance(aboutInstance, newInstance); + activity.runOnUiThread(() -> { + dialog.dismiss(); + Helper.logoutNoRemoval(activity); + }); + } else { + activity.runOnUiThread(() -> Toasty.error(activity, activity.getString(R.string.not_valide_instance), Toast.LENGTH_LONG).show()); + } + } catch (Exception e) { + e.printStackTrace(); + } + + }).start()); + alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); + alt_bld.setNeutralButton(R.string.help, (dialog, which) -> { + getSupportFragmentManager().setFragmentResultListener(PICK_INSTANCE, this, (requestKey, result) -> { + new Thread(() -> { + String newInstance = result.getString(INSTANCE_ADDRESS); + InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(ManageInstancesActivity.this, newInstance, null).getAboutInstance(); + SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + new StoredInstanceDAO(ManageInstancesActivity.this, db).insertInstance(aboutInstance, newInstance); + runOnUiThread(() -> new Handler().post(() -> Helper.logoutNoRemoval(ManageInstancesActivity.this))); + }).start(); + getSupportFragmentManager().clearFragmentResultListener(PICK_INSTANCE); + }); + addFragment( + getSupportFragmentManager(), android.R.id.content, new FragmentLoginPickInstancePeertube(), + null, null, FragmentLoginPickInstancePeertube.class.getName()); + }); + AlertDialog alert = alt_bld.create(); + alert.show(); + } + private void manageVIewInfoInstance(List aboutInstances) { binding.loader.setVisibility(View.GONE); if (aboutInstances == null || aboutInstances.size() == 0) { @@ -106,24 +184,6 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns return super.onOptionsItemSelected(item); } - - @SuppressLint("ApplySharedPref") - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == PICK_INSTANCE_SURF && resultCode == Activity.RESULT_OK) { - if (data != null && data.getData() != null) { - new Thread(() -> { - String newInstance = data.getData().toString().trim().toLowerCase(); - InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(ManageInstancesActivity.this, newInstance, null).getAboutInstance(); - SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - new StoredInstanceDAO(ManageInstancesActivity.this, db).insertInstance(aboutInstance, newInstance); - runOnUiThread(() -> new Handler().post(() -> Helper.logoutNoRemoval(ManageInstancesActivity.this))); - }).start(); - } - } - } - @Override public void onAllInstancesRemoved() { binding.noAction.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java index b96315390..3a15b2d7c 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java @@ -25,14 +25,13 @@ import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_SOFTWARE; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN; +import static app.fedilab.android.mastodon.helper.Helper.addFragment; import static app.fedilab.android.peertube.helper.Helper.peertubeInformation; import static app.fedilab.android.peertube.helper.SwitchAccountHelper.switchDialog; import android.annotation.SuppressLint; -import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; -import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -42,11 +41,8 @@ import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.EditText; import android.widget.FrameLayout; -import android.widget.LinearLayout; import android.widget.TextView; -import android.widget.Toast; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.SearchView; @@ -66,7 +62,6 @@ import com.kobakei.ratethisapp.RateThisApp; import org.jetbrains.annotations.NotNull; -import java.net.URL; import java.util.LinkedHashMap; import java.util.List; import java.util.Set; @@ -92,24 +87,18 @@ import app.fedilab.android.peertube.client.entities.PeertubeInformation; import app.fedilab.android.peertube.client.entities.Token; import app.fedilab.android.peertube.client.entities.UserMe; import app.fedilab.android.peertube.client.entities.UserSettings; -import app.fedilab.android.peertube.client.entities.WellKnownNodeinfo; import app.fedilab.android.peertube.fragment.DisplayOverviewFragment; import app.fedilab.android.peertube.fragment.DisplayVideosFragment; +import app.fedilab.android.peertube.fragment.FragmentLoginPickInstancePeertube; import app.fedilab.android.peertube.helper.Helper; import app.fedilab.android.peertube.helper.HelperInstance; import app.fedilab.android.peertube.services.RetrieveInfoService; -import app.fedilab.android.peertube.sqlite.StoredInstanceDAO; import app.fedilab.android.peertube.viewmodel.TimelineVM; -import app.fedilab.android.sqlite.Sqlite; -import es.dmoral.toasty.Toasty; - public class PeertubeMainActivity extends PeertubeBaseMainActivity { - - - public static int PICK_INSTANCE = 5641; - public static int PICK_INSTANCE_SURF = 5642; + public static String PICK_INSTANCE = "pick_instance"; + public static String INSTANCE_ADDRESS = "instance_address"; public static UserMe userMe; public static InstanceData.InstanceConfig instanceConfig; public static TypeOfConnection typeOfConnection = TypeOfConnection.NORMAL; @@ -143,71 +132,6 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { return true; }; - @SuppressLint("ApplySharedPref") - public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) { - SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); - AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(activity, app.fedilab.android.mastodon.helper.Helper.dialogStyle()); - alt_bld.setTitle(R.string.instance_choice); - String instance = HelperInstance.getLiveInstance(activity); - final EditText input = new EditText(activity); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT); - input.setLayoutParams(lp); - alt_bld.setView(input); - input.setText(instance); - alt_bld.setPositiveButton(R.string.validate, - (dialog, which) -> new Thread(() -> { - try { - String newInstance = input.getText().toString().trim(); - if (!newInstance.startsWith("http")) { - newInstance = "http://" + newInstance; - } - URL url = new URL(newInstance); - newInstance = url.getHost(); - - WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitPeertubeAPI(activity, newInstance, null).getNodeInfo(); - if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("peertube") == 0) { - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_INSTANCE, newInstance); - editor.commit(); - if (storeInDb) { - newInstance = newInstance.trim().toLowerCase(); - InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance(); - SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - new StoredInstanceDAO(activity, db).insertInstance(aboutInstance, newInstance); - activity.runOnUiThread(() -> { - dialog.dismiss(); - Helper.logoutNoRemoval(activity); - }); - } else { - activity.runOnUiThread(() -> { - dialog.dismiss(); - Intent intent = new Intent(activity, PeertubeMainActivity.class); - activity.startActivity(intent); - }); - } - } else { - activity.runOnUiThread(() -> Toasty.error(activity, activity.getString(R.string.not_valide_instance), Toast.LENGTH_LONG).show()); - } - } catch (Exception e) { - e.printStackTrace(); - } - - }).start()); - alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); - alt_bld.setNeutralButton(R.string.help, (dialog, which) -> { - Intent intent = new Intent(activity, InstancePickerActivity.class); - if (storeInDb) { - activity.startActivityForResult(intent, PICK_INSTANCE_SURF); - } else { - activity.startActivityForResult(intent, PICK_INSTANCE); - } - }); - AlertDialog alert = alt_bld.create(); - alert.show(); - } - private void setTitleCustom(int titleRId) { Toolbar toolbar = findViewById(R.id.toolbar); @@ -224,6 +148,7 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { binding = null; } + @SuppressLint("ApplySharedPref") @Override protected void onCreate(Bundle savedInstanceState) { @@ -448,8 +373,19 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { //Instance if (HelperInstance.getLiveInstance(PeertubeMainActivity.this) == null) { - Intent intent = new Intent(PeertubeMainActivity.this, InstancePickerActivity.class); - startActivityForResult(intent, PICK_INSTANCE); + getSupportFragmentManager().setFragmentResultListener(PICK_INSTANCE, this, (requestKey, result) -> { + String instance = result.getString(INSTANCE_ADDRESS, null); + if (instance != null) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_INSTANCE, instance); + editor.commit(); + PeertubeMainActivity.this.recreate(); + } + getSupportFragmentManager().clearFragmentResultListener(requestKey); + }); + addFragment( + getSupportFragmentManager(), android.R.id.content, new FragmentLoginPickInstancePeertube(), + null, null, FragmentLoginPickInstancePeertube.class.getName()); } fetchRecentAccounts(PeertubeMainActivity.this, headerMainBinding); @@ -758,22 +694,6 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { } } - - @SuppressLint("ApplySharedPref") - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == PICK_INSTANCE && resultCode == Activity.RESULT_OK) { - if (data != null && data.getData() != null) { - final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this); - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_INSTANCE, String.valueOf(data.getData())); - editor.commit(); - recreate(); - } - } - } - public enum TypeOfConnection { NORMAL, SURFING diff --git a/app/src/main/java/app/fedilab/android/peertube/drawer/InstanceAdapter.java b/app/src/main/java/app/fedilab/android/peertube/drawer/InstanceAdapter.java index 1a776b769..f13e258be 100644 --- a/app/src/main/java/app/fedilab/android/peertube/drawer/InstanceAdapter.java +++ b/app/src/main/java/app/fedilab/android/peertube/drawer/InstanceAdapter.java @@ -15,14 +15,10 @@ package app.fedilab.android.peertube.drawer; * see . */ -import static android.app.Activity.RESULT_OK; import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY; import static app.fedilab.android.peertube.helper.Helper.peertubeInformation; -import android.app.Activity; import android.content.Context; -import android.content.Intent; -import android.net.Uri; import android.os.Build; import android.text.Html; import android.text.SpannableString; @@ -52,6 +48,8 @@ public class InstanceAdapter extends RecyclerView.Adapter instances) { this.instances = instances; } @@ -146,11 +144,7 @@ public class InstanceAdapter extends RecyclerView.Adapter { - Intent data = new Intent(); - String instanceHost = instance.getHost(); - data.setData(Uri.parse(instanceHost)); - ((Activity) context).setResult(RESULT_OK, data); - ((Activity) context).finish(); + actionClick.instance(instance.getHost()); }); } @@ -174,4 +168,7 @@ public class InstanceAdapter extends RecyclerView.Adapter. */ +import static app.fedilab.android.peertube.activities.PeertubeMainActivity.INSTANCE_ADDRESS; import static app.fedilab.android.peertube.helper.Helper.peertubeInformation; import android.os.Bundle; import android.text.SpannableStringBuilder; import android.text.Spanned; -import android.view.MenuItem; +import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -41,8 +45,8 @@ import java.util.List; import java.util.Map; import app.fedilab.android.R; -import app.fedilab.android.databinding.ActivityInstancePickerPeertubeBinding; -import app.fedilab.android.mastodon.activities.BaseBarActivity; +import app.fedilab.android.databinding.FragmentLoginPickInstancePeertubeBinding; +import app.fedilab.android.mastodon.helper.Helper; import app.fedilab.android.peertube.client.APIResponse; import app.fedilab.android.peertube.client.data.InstanceData; import app.fedilab.android.peertube.client.entities.InstanceParams; @@ -52,7 +56,7 @@ import app.fedilab.android.peertube.viewmodel.InstancesVM; import es.dmoral.toasty.Toasty; -public class InstancePickerActivity extends BaseBarActivity { +public class FragmentLoginPickInstancePeertube extends Fragment implements InstanceAdapter.ActionClick { boolean[] checkedItemsCategory; @@ -63,28 +67,24 @@ public class InstancePickerActivity extends BaseBarActivity { String[] itemsLabelLanguage; InstanceParams instanceParams; private InstancesVM viewModel; - private ActivityInstancePickerPeertubeBinding binding; + private FragmentLoginPickInstancePeertubeBinding binding; + @Nullable @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (getSupportActionBar() != null) - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - binding = ActivityInstancePickerPeertubeBinding.inflate(getLayoutInflater()); - setContentView(binding.getRoot()); + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + binding = FragmentLoginPickInstancePeertubeBinding.inflate(inflater, container, false); binding.loader.setVisibility(View.VISIBLE); String[] channelSensitive = new String[]{"do_not_list", "blur", "display", "no_opinion"}; String[] channelSensitivesLabel = new String[]{getString(R.string.do_not_list), getString(R.string.blur), getString(R.string.display), getString(R.string.no_opinion)}; - ArrayAdapter adapterChannel = new ArrayAdapter<>(InstancePickerActivity.this, + ArrayAdapter adapterChannel = new ArrayAdapter<>(requireActivity(), android.R.layout.simple_spinner_dropdown_item, channelSensitivesLabel); binding.sensitive.setAdapter(adapterChannel); - viewModel = new ViewModelProvider(InstancePickerActivity.this).get(InstancesVM.class); + viewModel = new ViewModelProvider(this).get(InstancesVM.class); binding.sensitive.setSelection(1, false); binding.sensitive.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @@ -92,7 +92,7 @@ public class InstancePickerActivity extends BaseBarActivity { public void onItemSelected(AdapterView parent, View view, int position, long id) { instanceParams.setNsfwPolicy(channelSensitive[position]); binding.loader.setVisibility(View.VISIBLE); - viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, apiResponse -> manageVIewInstance(apiResponse)); + viewModel.getInstances(instanceParams).observe(getViewLifecycleOwner(), apiResponse -> manageVIewInstance(apiResponse)); } @Override @@ -108,7 +108,7 @@ public class InstancePickerActivity extends BaseBarActivity { itemsKeyLanguage = new String[languages.size()]; binding.pickupLanguages.setOnClickListener(v -> { - AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(InstancePickerActivity.this, app.fedilab.android.mastodon.helper.Helper.dialogStyle()); + AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(requireContext(), app.fedilab.android.mastodon.helper.Helper.dialogStyle()); int i = 0; if (languages.size() > 0) { @@ -143,7 +143,7 @@ public class InstancePickerActivity extends BaseBarActivity { if (between.length() == 0) between = " "; String tag = " " + lang + " "; stringBuilder.append(tag); - stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + stringBuilder.setSpan(new RoundedBackgroundSpan(requireContext()), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); stringBuilder.append(" "); } } @@ -157,7 +157,7 @@ public class InstancePickerActivity extends BaseBarActivity { binding.languagesView.setVisibility(View.GONE); } binding.loader.setVisibility(View.VISIBLE); - viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); + viewModel.getInstances(instanceParams).observe(getViewLifecycleOwner(), this::manageVIewInstance); }); dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); @@ -175,7 +175,7 @@ public class InstancePickerActivity extends BaseBarActivity { binding.pickupCategories.setOnClickListener(v -> { - AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(InstancePickerActivity.this, app.fedilab.android.mastodon.helper.Helper.dialogStyle()); + AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(requireContext(), app.fedilab.android.mastodon.helper.Helper.dialogStyle()); int i = 0; if (categories.size() > 0) { Iterator> it = categories.entrySet().iterator(); @@ -208,7 +208,7 @@ public class InstancePickerActivity extends BaseBarActivity { if (between.length() == 0) between = " "; String tag = " " + cat + " "; stringBuilder.append(tag); - stringBuilder.setSpan(new RoundedBackgroundSpan(InstancePickerActivity.this), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + stringBuilder.setSpan(new RoundedBackgroundSpan(requireContext()), stringBuilder.length() - tag.length(), stringBuilder.length() - tag.length() + tag.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); stringBuilder.append(" "); } } @@ -222,7 +222,7 @@ public class InstancePickerActivity extends BaseBarActivity { binding.categoriesView.setVisibility(View.GONE); } binding.loader.setVisibility(View.VISIBLE); - viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); + viewModel.getInstances(instanceParams).observe(getViewLifecycleOwner(), this::manageVIewInstance); }); dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> dialog.dismiss()); @@ -235,41 +235,40 @@ public class InstancePickerActivity extends BaseBarActivity { binding.loader.setVisibility(View.VISIBLE); - setTitle(R.string.instances_picker); - instanceParams = new InstanceParams(); instanceParams.setNsfwPolicy(channelSensitive[1]); - viewModel.getInstances(instanceParams).observe(InstancePickerActivity.this, this::manageVIewInstance); + viewModel.getInstances(instanceParams).observe(getViewLifecycleOwner(), this::manageVIewInstance); + + + return binding.getRoot(); } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - finish(); - return true; - } - return super.onOptionsItemSelected(item); - } - - public void manageVIewInstance(APIResponse apiResponse) { binding.loader.setVisibility(View.GONE); if (apiResponse.getError() != null) { - Toasty.error(InstancePickerActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); + Toasty.error(requireContext(), apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); return; } List instances = apiResponse.getInstances(); - RecyclerView lv_instances = findViewById(R.id.lv_instances); if ((instances == null || instances.size() == 0)) { binding.noAction.setVisibility(View.VISIBLE); - lv_instances.setVisibility(View.GONE); + binding.lvInstances.setVisibility(View.GONE); } else { binding.noAction.setVisibility(View.GONE); - lv_instances.setVisibility(View.VISIBLE); + binding.lvInstances.setVisibility(View.VISIBLE); InstanceAdapter instanceAdapter = new InstanceAdapter(instances); - lv_instances.setAdapter(instanceAdapter); - lv_instances.setLayoutManager(new LinearLayoutManager(InstancePickerActivity.this)); + instanceAdapter.actionClick = this; + binding.lvInstances.setAdapter(instanceAdapter); + binding.lvInstances.setLayoutManager(new LinearLayoutManager(requireContext())); } } + + @Override + public void instance(String instance) { + Bundle bundle = new Bundle(); + bundle.putString(INSTANCE_ADDRESS, instance); + Helper.addFragment( + getParentFragmentManager(), android.R.id.content, new PeertubeRegisterFragment(), + bundle, null, PeertubeRegisterFragment.class.getName()); + } } diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeRegisterActivity.java b/app/src/main/java/app/fedilab/android/peertube/fragment/PeertubeRegisterFragment.java similarity index 51% rename from app/src/main/java/app/fedilab/android/peertube/activities/PeertubeRegisterActivity.java rename to app/src/main/java/app/fedilab/android/peertube/fragment/PeertubeRegisterFragment.java index 021791f54..9e7270ad2 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeRegisterActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/fragment/PeertubeRegisterFragment.java @@ -1,4 +1,4 @@ -package app.fedilab.android.peertube.activities; +package app.fedilab.android.peertube.fragment; /* Copyright 2023 Thomas Schneider * * This file is a part of Fedilab @@ -14,22 +14,22 @@ package app.fedilab.android.peertube.activities; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ -import static app.fedilab.android.peertube.activities.PeertubeMainActivity.PICK_INSTANCE; +import static app.fedilab.android.peertube.activities.PeertubeMainActivity.INSTANCE_ADDRESS; -import android.annotation.SuppressLint; -import android.app.Activity; -import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.text.Html; import android.text.method.LinkMovementMethod; import android.util.Patterns; -import android.view.MenuItem; +import android.view.LayoutInflater; import android.view.View; -import android.widget.TextView; +import android.view.ViewGroup; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -37,102 +37,88 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import app.fedilab.android.R; -import app.fedilab.android.databinding.ActivityRegisterPeertubeBinding; -import app.fedilab.android.mastodon.activities.BaseBarActivity; +import app.fedilab.android.databinding.FragmentLoginRegisterPeertubeBinding; import app.fedilab.android.peertube.client.APIResponse; import app.fedilab.android.peertube.client.RetrofitPeertubeAPI; import app.fedilab.android.peertube.client.entities.AccountCreation; import es.dmoral.toasty.Toasty; -public class PeertubeRegisterActivity extends BaseBarActivity { +public class PeertubeRegisterFragment extends Fragment { private String instance; - private ActivityRegisterPeertubeBinding binding; + private FragmentLoginRegisterPeertubeBinding binding; - - @SuppressLint("SetTextI18n") + @Nullable @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - binding = ActivityRegisterPeertubeBinding.inflate(getLayoutInflater()); - View mainView = binding.getRoot(); - setContentView(mainView); - - if (getSupportActionBar() != null) - getSupportActionBar().setDisplayHomeAsUpEnabled(true); + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + binding = FragmentLoginRegisterPeertubeBinding.inflate(inflater, container, false); + View root = binding.getRoot(); - binding.loginInstanceContainer.setVisibility(View.VISIBLE); - binding.titleLoginInstance.setVisibility(View.VISIBLE); + binding.loginInstanceLayout.setVisibility(View.VISIBLE); - binding.username.setOnFocusChangeListener((view, focused) -> { - if (!focused && binding.username.getText() != null) { + binding.loginUsername.setOnFocusChangeListener((view, focused) -> { + if (!focused && binding.loginUsername.getText() != null) { Pattern patternUsername = Pattern.compile("^[a-z0-9._]{1,50}$"); - Matcher matcherMaxId = patternUsername.matcher(binding.username.getText().toString()); + Matcher matcherMaxId = patternUsername.matcher(binding.loginUsername.getText().toString()); if (!matcherMaxId.matches()) { - binding.username.setError(getString(R.string.username_error)); + binding.loginUsername.setError(getString(R.string.username_error)); } } }); - - binding.instanceHelp.setOnClickListener(v -> { - Intent intent = new Intent(PeertubeRegisterActivity.this, InstancePickerActivity.class); - startActivityForResult(intent, PICK_INSTANCE); - }); - - binding.email.setOnFocusChangeListener((view, focused) -> { - if (!focused && binding.email.getText() != null) { + binding.loginEmail.setOnFocusChangeListener((view, focused) -> { + if (!focused && binding.loginEmail.getText() != null) { Pattern patternUsername = Patterns.EMAIL_ADDRESS; - Matcher matcherMaxId = patternUsername.matcher(binding.email.getText().toString()); + Matcher matcherMaxId = patternUsername.matcher(binding.loginEmail.getText().toString()); if (!matcherMaxId.matches()) { - binding.email.setError(getString(R.string.email_error)); + binding.loginEmail.setError(getString(R.string.email_error)); } } }); - binding.password.setOnFocusChangeListener((view, focused) -> { - if (!focused && binding.password.getText() != null) { - if (binding.password.getText().length() < 6) { - binding.password.setError(getString(R.string.password_length_error)); + binding.loginPassword.setOnFocusChangeListener((view, focused) -> { + if (!focused && binding.loginPassword.getText() != null) { + if (binding.loginPassword.getText().length() < 6) { + binding.loginPassword.setError(getString(R.string.password_length_error)); } } }); - binding.passwordConfirm.setOnFocusChangeListener((view, focused) -> { - if (!focused && binding.passwordConfirm.getText() != null && binding.password.getText() != null) { - if (binding.passwordConfirm.getText().toString().compareTo(binding.password.getText().toString()) != 0) { - binding.passwordConfirm.setError(getString(R.string.password)); + binding.loginPasswordConfirm.setOnFocusChangeListener((view, focused) -> { + if (!focused && binding.loginPasswordConfirm.getText() != null && binding.loginPassword.getText() != null) { + if (binding.loginPasswordConfirm.getText().toString().compareTo(binding.loginPassword.getText().toString()) != 0) { + binding.loginPasswordConfirm.setError(getString(R.string.password)); } } }); setTextAgreement(); binding.signup.setOnClickListener(view -> { binding.errorMessage.setVisibility(View.GONE); - if (binding.username.getText() == null || binding.email.getText() == null || binding.password.getText() == null || binding.passwordConfirm.getText() == null || binding.username.getText().toString().trim().length() == 0 || binding.email.getText().toString().trim().length() == 0 || - binding.password.getText().toString().trim().length() == 0 || binding.passwordConfirm.getText().toString().trim().length() == 0 || !binding.agreement.isChecked()) { - Toasty.error(PeertubeRegisterActivity.this, getString(R.string.all_field_filled)).show(); + if (binding.loginUsername.getText() == null || binding.loginEmail.getText() == null || binding.loginPassword.getText() == null || binding.loginPasswordConfirm.getText() == null || binding.loginUsername.getText().toString().trim().length() == 0 || binding.loginEmail.getText().toString().trim().length() == 0 || + binding.loginPassword.getText().toString().trim().length() == 0 || binding.loginPasswordConfirm.getText().toString().trim().length() == 0 || !binding.agreement.isChecked()) { + Toasty.error(requireContext(), getString(R.string.all_field_filled)).show(); return; } - if (!binding.password.getText().toString().trim().equals(binding.passwordConfirm.getText().toString().trim())) { - Toasty.error(PeertubeRegisterActivity.this, getString(R.string.password_error)).show(); + if (!binding.loginPassword.getText().toString().trim().equals(binding.loginPasswordConfirm.getText().toString().trim())) { + Toasty.error(requireContext(), getString(R.string.password_error)).show(); return; } - if (!Patterns.EMAIL_ADDRESS.matcher(binding.email.getText().toString().trim()).matches()) { - Toasty.error(PeertubeRegisterActivity.this, getString(R.string.email_error)).show(); + if (!Patterns.EMAIL_ADDRESS.matcher(binding.loginEmail.getText().toString().trim()).matches()) { + Toasty.error(requireContext(), getString(R.string.email_error)).show(); return; } - String[] emailArray = binding.email.getText().toString().split("@"); + String[] emailArray = binding.loginEmail.getText().toString().split("@"); - if (binding.password.getText().toString().trim().length() < 8) { - Toasty.error(PeertubeRegisterActivity.this, getString(R.string.password_too_short)).show(); + if (binding.loginPassword.getText().toString().trim().length() < 8) { + Toasty.error(requireContext(), getString(R.string.password_too_short)).show(); return; } - if (binding.username.getText().toString().matches("[a-z0-9_]")) { - Toasty.error(PeertubeRegisterActivity.this, getString(R.string.username_error)).show(); + if (binding.loginUsername.getText().toString().matches("[a-z0-9_]")) { + Toasty.error(requireContext(), getString(R.string.username_error)).show(); return; } binding.signup.setEnabled(false); @@ -152,15 +138,15 @@ public class PeertubeRegisterActivity extends BaseBarActivity { } AccountCreation accountCreation = new AccountCreation(); - accountCreation.setEmail(binding.email.getText().toString().trim()); - accountCreation.setPassword(binding.password.getText().toString().trim()); - accountCreation.setPasswordConfirm(binding.passwordConfirm.getText().toString().trim()); - accountCreation.setUsername(binding.username.getText().toString().trim()); + accountCreation.setEmail(binding.loginEmail.getText().toString().trim()); + accountCreation.setPassword(binding.loginPassword.getText().toString().trim()); + accountCreation.setPasswordConfirm(binding.loginPasswordConfirm.getText().toString().trim()); + accountCreation.setUsername(binding.loginUsername.getText().toString().trim()); accountCreation.setInstance(instance); new Thread(() -> { try { - APIResponse apiResponse = new RetrofitPeertubeAPI(PeertubeRegisterActivity.this, instance, null).createAccount(accountCreation); + APIResponse apiResponse = new RetrofitPeertubeAPI(requireContext(), instance, null).createAccount(accountCreation); Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { if (apiResponse.getError() != null) { @@ -186,11 +172,11 @@ public class PeertubeRegisterActivity extends BaseBarActivity { return; } - AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(PeertubeRegisterActivity.this, app.fedilab.android.mastodon.helper.Helper.dialogStyle()); + AlertDialog.Builder dialogBuilder = new MaterialAlertDialogBuilder(requireContext(), app.fedilab.android.mastodon.helper.Helper.dialogStyle()); dialogBuilder.setCancelable(false); dialogBuilder.setPositiveButton(R.string.validate, (dialog, which) -> { dialog.dismiss(); - finish(); + getParentFragmentManager().popBackStack(); }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.setTitle(getString(R.string.account_created)); @@ -205,53 +191,29 @@ public class PeertubeRegisterActivity extends BaseBarActivity { }); - setTitle(R.string.create_an_account); - } - - @Override - protected void onResume() { - super.onResume(); - } - - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - finish(); - return true; + if (getArguments() != null) { + String instance = getArguments().getString(INSTANCE_ADDRESS, null); + binding.loginInstance.setText(instance); + setTextAgreement(); } - return super.onOptionsItemSelected(item); - } - @SuppressLint("ApplySharedPref") - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == PICK_INSTANCE && resultCode == Activity.RESULT_OK) { - if (data != null && data.getData() != null) { - String instance = String.valueOf(data.getData()); - binding.loginInstance.setText(instance); - binding.loginInstance.setSelection(instance.length()); - setTextAgreement(); - } - } + return root; } private void setTextAgreement() { - TextView agreement_text = findViewById(R.id.agreement_text); String tos = getString(R.string.tos); String serverrules = getString(R.string.server_rules); String content_agreement = null; - agreement_text.setMovementMethod(null); - agreement_text.setText(null); + binding.agreement.setMovementMethod(null); + binding.agreement.setText(null); if (binding.loginInstance.getText() != null) { content_agreement = getString(R.string.agreement_check_peertube, "" + tos + "" ); } - agreement_text.setMovementMethod(LinkMovementMethod.getInstance()); + binding.agreement.setMovementMethod(LinkMovementMethod.getInstance()); if (content_agreement != null) { - agreement_text.setText(Html.fromHtml(content_agreement)); + binding.agreement.setText(Html.fromHtml(content_agreement)); } } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginJoin.java b/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginJoin.java index 07e181357..681c6437a 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginJoin.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginJoin.java @@ -14,7 +14,6 @@ package app.fedilab.android.ui.fragment; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ -import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -25,7 +24,7 @@ import androidx.fragment.app.Fragment; import app.fedilab.android.databinding.FragmentLoginJoinBinding; import app.fedilab.android.mastodon.helper.Helper; -import app.fedilab.android.peertube.activities.PeertubeRegisterActivity; +import app.fedilab.android.peertube.fragment.FragmentLoginPickInstancePeertube; public class FragmentLoginJoin extends Fragment { @@ -41,10 +40,9 @@ public class FragmentLoginJoin extends Fragment { getParentFragmentManager(), android.R.id.content, new FragmentLoginPickInstanceMastodon(), null, null, FragmentLoginPickInstanceMastodon.class.getName()); }); - binding.joinPeertube.setOnClickListener(v -> { - Intent mainActivity = new Intent(requireActivity(), PeertubeRegisterActivity.class); - startActivity(mainActivity); - }); + binding.joinPeertube.setOnClickListener(v -> Helper.addFragment( + getParentFragmentManager(), android.R.id.content, new FragmentLoginPickInstancePeertube(), + null, null, FragmentLoginPickInstancePeertube.class.getName())); return root; } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginRegisterMastodon.java b/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginRegisterMastodon.java index fcf05adca..d85f42a90 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginRegisterMastodon.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/FragmentLoginRegisterMastodon.java @@ -67,8 +67,8 @@ public class FragmentLoginRegisterMastodon extends Fragment { "" + serverrules + "", "" + tos + "" ); - binding.agreementText.setMovementMethod(LinkMovementMethod.getInstance()); - binding.agreementText.setText(Html.fromHtml(content_agreement)); + binding.agreement.setMovementMethod(LinkMovementMethod.getInstance()); + binding.agreement.setText(Html.fromHtml(content_agreement)); } else { binding.loginInstance.setOnFocusChangeListener((v, hasFocus) -> { if (!hasFocus) { @@ -80,8 +80,8 @@ public class FragmentLoginRegisterMastodon extends Fragment { "" + serverrules + "", "" + tos + "" ); - binding.agreementText.setMovementMethod(LinkMovementMethod.getInstance()); - binding.agreementText.setText(Html.fromHtml(content_agreement)); + binding.agreement.setMovementMethod(LinkMovementMethod.getInstance()); + binding.agreement.setText(Html.fromHtml(content_agreement)); } else { binding.loginInstanceLayout.setError(getString(R.string.instance_not_valid)); } diff --git a/app/src/main/res/layouts/mastodon/layout/fragment_login_register_mastodon.xml b/app/src/main/res/layouts/mastodon/layout/fragment_login_register_mastodon.xml index f2be10fbb..bd30468e7 100644 --- a/app/src/main/res/layouts/mastodon/layout/fragment_login_register_mastodon.xml +++ b/app/src/main/res/layouts/mastodon/layout/fragment_login_register_mastodon.xml @@ -14,148 +14,146 @@ You should have received a copy of the GNU General Public License along with Fedilab; if not, see . --> - + android:background="?android:windowBackground"> - + android:orientation="vertical" + android:padding="12dp"> - - - - - - + android:layout_gravity="center" + android:background="@drawable/red_border" + android:gravity="center" + android:padding="6dp" + android:textColor="?colorError" + android:visibility="gone" /> - - - - + android:layout_gravity="center" + android:gravity="center" + android:text="@string/validation_needed" + android:textColor="?colorError" + android:visibility="gone" /> - - - - - + app:errorEnabled="true"> + - + - - - - + android:layout_marginTop="12dp" + app:errorEnabled="true"> - + - + - - + android:layout_marginTop="12dp" + app:errorEnabled="true" + app:helperText="@string/email_indicator"> + - + + + + + + + + + + + + + + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginTop="12dp" + tools:text="@string/agreement_check_peertube" /> - - + - + - \ No newline at end of file + diff --git a/app/src/main/res/layouts/peertube/layout/activity_instance_picker_peertube.xml b/app/src/main/res/layouts/peertube/layout/activity_instance_picker_peertube.xml deleted file mode 100644 index c41e34fb0..000000000 --- a/app/src/main/res/layouts/peertube/layout/activity_instance_picker_peertube.xml +++ /dev/null @@ -1,161 +0,0 @@ - - - - - - - - - - - -