From 860edec2e67955f0ffd068be814f9fd5a89a74b4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 11 Nov 2020 15:36:15 +0100 Subject: [PATCH] Follow remote accounts --- .../fedilab/fedilabtube/AccountActivity.java | 4 +- .../app/fedilab/fedilabtube/MainActivity.java | 2 +- .../fedilabtube/ShowChannelActivity.java | 62 ++++++++++++++++++- .../drawer/AboutInstanceAdapter.java | 7 +-- .../fedilab/fedilabtube/helper/Helper.java | 1 - .../fedilabtube/sqlite/StoredInstanceDAO.java | 29 +++++++-- .../main/res/layout/drawer_about_instance.xml | 2 +- 7 files changed, 91 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java index 9d7b897..8f94d47 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java @@ -99,9 +99,7 @@ public class AccountActivity extends AppCompatActivity { instanceView.setText(account.getHost()); FloatingActionButton edit_profile = findViewById(R.id.edit_profile); - edit_profile.setOnClickListener(v -> { - startActivity(new Intent(AccountActivity.this, MyAccountActivity.class)); - }); + edit_profile.setOnClickListener(v -> startActivity(new Intent(AccountActivity.this, MyAccountActivity.class))); Button logout_button = findViewById(R.id.logout_button); logout_button.setOnClickListener(v -> { diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index fe431ac..49b5abc 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -121,7 +121,7 @@ public class MainActivity extends AppCompatActivity { return false; } }; - private TypeOfConnection typeOfConnection; + public static TypeOfConnection typeOfConnection; @SuppressLint("ApplySharedPref") public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java b/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java index 6d7aba7..a01d2c7 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowChannelActivity.java @@ -17,8 +17,11 @@ package app.fedilab.fedilabtube; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.text.Html; import android.text.SpannableString; import android.text.method.LinkMovementMethod; @@ -54,10 +57,14 @@ import java.util.Map; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; +import app.fedilab.fedilabtube.client.data.AccountData; import app.fedilab.fedilabtube.client.data.ChannelData.Channel; +import app.fedilab.fedilabtube.drawer.OwnAccountsAdapter; import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment; import app.fedilab.fedilabtube.fragment.DisplayVideosFragment; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.sqlite.AccountDAO; +import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.viewmodel.ChannelsVM; import app.fedilab.fedilabtube.viewmodel.PostActionsVM; import app.fedilab.fedilabtube.viewmodel.RelationshipVM; @@ -65,6 +72,8 @@ import app.fedilab.fedilabtube.viewmodel.TimelineVM; import es.dmoral.toasty.Toasty; import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY; +import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.NORMAL; +import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.SURFING; import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.FOLLOW; import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.MUTE; import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT; @@ -122,6 +131,57 @@ public class ShowChannelActivity extends AppCompatActivity { ChannelsVM viewModel = new ViewModelProvider(ShowChannelActivity.this).get(ChannelsVM.class); viewModel.get(sepiaSearch ? peertubeInstance : null, CHANNEL, channelAcct == null ? channel.getAcct() : channelAcct).observe(ShowChannelActivity.this, this::manageViewAccounts); manageChannel(); + + if (MainActivity.typeOfConnection == MainActivity.TypeOfConnection.SURFING) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + int[][] states = new int[][]{ + new int[]{android.R.attr.state_enabled}, // enabled + new int[]{-android.R.attr.state_enabled}, // disabled + new int[]{-android.R.attr.state_checked}, // unchecked + new int[]{android.R.attr.state_pressed} // pressed + }; + + int[] colors = new int[]{ + ContextCompat.getColor(ShowChannelActivity.this, Helper.getColorAccent()), + ContextCompat.getColor(ShowChannelActivity.this, Helper.getColorAccent()), + ContextCompat.getColor(ShowChannelActivity.this, Helper.getColorAccent()), + ContextCompat.getColor(ShowChannelActivity.this, Helper.getColorAccent()) + }; + account_follow.setBackgroundTintList(new ColorStateList(states, colors)); + } + account_follow.setText(getString(R.string.action_follow)); + account_follow.setEnabled(true); + new Thread(() -> { + try { + SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + List accounts = new AccountDAO(ShowChannelActivity.this, db).getAllAccount(); + runOnUiThread(() -> { + account_follow.setVisibility(View.VISIBLE); + account_follow.setOnClickListener(v -> { + AlertDialog.Builder builderSingle = new AlertDialog.Builder(ShowChannelActivity.this); + builderSingle.setTitle(getString(R.string.list_of_accounts)); + if (accounts != null) { + final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(ShowChannelActivity.this, accounts); + builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> { + new Thread(() -> { + try { + RetrofitPeertubeAPI peertubeAPI = new RetrofitPeertubeAPI(ShowChannelActivity.this, accounts.get(which).getHost(), accounts.get(which).getToken()); + peertubeAPI.post(FOLLOW, channel.getAcct(), null); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + }); + } + builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); + builderSingle.show(); + }); + }); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + } } @Override @@ -312,7 +372,7 @@ public class ShowChannelActivity extends AppCompatActivity { //Manages the visibility of the button private void manageButtonVisibility() { - if (relationship == null) + if (relationship == null || MainActivity.typeOfConnection == SURFING) return; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/AboutInstanceAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/AboutInstanceAdapter.java index f0f8cb6..c6f5acf 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/AboutInstanceAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/AboutInstanceAdapter.java @@ -100,15 +100,12 @@ public class AboutInstanceAdapter extends RecyclerView.Adapter { + holder.binding.instanceContainer.setOnClickListener(v -> { final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.PREF_INSTANCE, aboutInstance.getHost()); editor.commit(); - ((Activity) context).runOnUiThread(() -> { - Intent intent = new Intent(context, MainActivity.class); - context.startActivity(intent); - }); + Helper.logoutNoRemoval((Activity) context); }); holder.binding.instanceMore.setOnClickListener(v -> { PopupMenu popup = new PopupMenu(context, holder.binding.instanceMore); diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 650e4e8..491c68d 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -639,7 +639,6 @@ public class Helper { editor.putString(CLIENT_ID, null); editor.putString(CLIENT_SECRET, null); editor.putString(PREF_KEY_ID, null); - editor.putString(PREF_INSTANCE, null); editor.putString(ID, null); editor.apply(); Intent loginActivity = new Intent(activity, MainActivity.class); diff --git a/app/src/main/java/app/fedilab/fedilabtube/sqlite/StoredInstanceDAO.java b/app/src/main/java/app/fedilab/fedilabtube/sqlite/StoredInstanceDAO.java index eda559c..aea97a1 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/sqlite/StoredInstanceDAO.java +++ b/app/src/main/java/app/fedilab/fedilabtube/sqlite/StoredInstanceDAO.java @@ -80,8 +80,11 @@ public class StoredInstanceDAO { * @return boolean */ public boolean insertInstance(InstanceData.AboutInstance aboutInstance, String targetedInstance) { - ContentValues values = new ContentValues(); + if (checkExists(targetedInstance)) { + return true; + } + ContentValues values = new ContentValues(); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); String instance = Helper.getLiveInstance(context); @@ -90,7 +93,7 @@ public class StoredInstanceDAO { values.put(Sqlite.COL_USER_INSTANCE, instance != null ? instance : "_ALL_"); values.put(Sqlite.COL_ABOUT, aboutInstanceToStringStorage(aboutInstance)); values.put(Sqlite.COL_INSTANCE, targetedInstance); - //Inserts account + //Inserts instance try { db.insertOrThrow(Sqlite.TABLE_BOOKMARKED_INSTANCES, null, values); @@ -101,6 +104,23 @@ public class StoredInstanceDAO { return true; } + /** + * Check if instance exists + * + * @param targetedInstance String + * @return int + */ + private boolean checkExists(String targetedInstance) { + try { + Cursor c = db.query(Sqlite.TABLE_BOOKMARKED_INSTANCES, null, Sqlite.COL_INSTANCE + " = \"" + targetedInstance + "\"", null, null, null, null, "1"); + return cursorToInstance(c) != null; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + /** * Insert instance info in database * @@ -108,6 +128,7 @@ public class StoredInstanceDAO { * @param targetedInstance String * @return int */ + @SuppressWarnings({"unused", "RedundantSuppression"}) public int updateInstance(InstanceData.AboutInstance aboutInstance, String targetedInstance) { ContentValues values = new ContentValues(); values.put(Sqlite.COL_ABOUT, aboutInstanceToStringStorage(aboutInstance)); @@ -146,9 +167,9 @@ public class StoredInstanceDAO { /*** - * Method to hydrate an Account from database + * Method to hydrate an AboutInstance from database * @param c Cursor - * @return Account + * @return AboutInstance */ private InstanceData.AboutInstance cursorToInstance(Cursor c) { //No element found diff --git a/app/src/main/res/layout/drawer_about_instance.xml b/app/src/main/res/layout/drawer_about_instance.xml index d89cd6f..619cc1f 100644 --- a/app/src/main/res/layout/drawer_about_instance.xml +++ b/app/src/main/res/layout/drawer_about_instance.xml @@ -18,7 +18,7 @@