From a1b816030a7e506bc6aae9d160ee726284d9508b Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 8 Sep 2020 15:54:07 +0200 Subject: [PATCH] VM-v7 --- .../fedilab/fedilabtube/LoginActivity.java | 7 +- .../fedilabtube/ShowAccountActivity.java | 2 +- .../fedilabtube/WebviewConnectActivity.java | 7 +- .../fedilabtube/client/HttpsConnection.java | 75 +++++++++++- .../fragment/DisplayAccountsFragment.java | 26 ++--- .../DisplayNotificationsFragment.java | 6 +- .../fragment/DisplayPlaylistsFragment.java | 4 +- .../fragment/DisplayStatusFragment.java | 24 ++-- .../fedilabtube/viewmodel/FeedsVM.java | 8 +- .../viewmodel/NotificationsVM.java | 6 +- .../fedilabtube/viewmodel/OwnAccountVM.java | 107 ------------------ 11 files changed, 114 insertions(+), 158 deletions(-) delete mode 100644 app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index e41f68e..ba87e3b 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -30,7 +30,6 @@ import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; -import androidx.lifecycle.ViewModelProvider; import com.google.android.material.textfield.TextInputLayout; @@ -44,9 +43,10 @@ import java.util.HashMap; import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.helper.Helper; -import app.fedilab.fedilabtube.viewmodel.OwnAccountVM; import es.dmoral.toasty.Toasty; +import static app.fedilab.fedilabtube.client.HttpsConnection.updateCredential; + public class LoginActivity extends AppCompatActivity { @@ -292,8 +292,7 @@ public class LoginActivity extends AppCompatActivity { editor.putString(Helper.PREF_INSTANCE, host); editor.apply(); //Update the account with the token; - OwnAccountVM viewModelC = new ViewModelProvider(LoginActivity.this).get(OwnAccountVM.class); - viewModelC.updateCredential(token, client_id, client_secret, refresh_token, host); + updateCredential(LoginActivity.this, token, client_id, client_secret, refresh_token, host); } catch (JSONException e) { e.printStackTrace(); runOnUiThread(() -> connectionButton.setEnabled(true)); diff --git a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java index 60060a5..0600ee0 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java @@ -118,7 +118,7 @@ public class ShowAccountActivity extends AppCompatActivity { AccountsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(AccountsVM.class); if (account != null) { manageAccount(); - viewModel.getAccounts(null, account.getAcct(), AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(ShowAccountActivity.this, this::manageViewAccounts); + viewModel.getAccounts(null, account.getAcct(), AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts); } else { viewModel.getAccounts(null, accountId, AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java b/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java index 1e5eb40..6feca15 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java @@ -28,7 +28,6 @@ import android.widget.ProgressBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; -import androidx.lifecycle.ViewModelProvider; import org.json.JSONObject; @@ -38,9 +37,10 @@ import java.util.regex.Matcher; import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.helper.Helper; -import app.fedilab.fedilabtube.viewmodel.OwnAccountVM; import app.fedilab.fedilabtube.webview.CustomWebview; +import static app.fedilab.fedilabtube.client.HttpsConnection.updateCredential; + public class WebviewConnectActivity extends AppCompatActivity { @@ -135,8 +135,7 @@ public class WebviewConnectActivity extends AppCompatActivity { editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); editor.putString(Helper.PREF_INSTANCE, new URL(url).getHost()); editor.apply(); - OwnAccountVM viewModelC = new ViewModelProvider(WebviewConnectActivity.this).get(OwnAccountVM.class); - viewModelC.updateCredential(token, clientId, clientSecret, refresh_token, new URL(url).getHost()); + updateCredential(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, new URL(url).getHost()); finish(); } catch (Exception e) { e.printStackTrace(); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java b/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java index 4bfe07a..a22ff23 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/HttpsConnection.java @@ -14,9 +14,14 @@ package app.fedilab.fedilabtube.client; * You should have received a copy of the GNU General Public License along with TubeLab; if not, * see . */ +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; import android.os.Build; +import android.os.Handler; +import android.os.Looper; import android.text.Html; import android.text.SpannableString; @@ -34,6 +39,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.InetSocketAddress; @@ -41,6 +47,7 @@ import java.net.MalformedURLException; import java.net.PasswordAuthentication; import java.net.Proxy; import java.net.URL; +import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -55,8 +62,12 @@ import java.util.regex.Pattern; import javax.net.ssl.HttpsURLConnection; +import app.fedilab.fedilabtube.MainActivity; import app.fedilab.fedilabtube.R; +import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.sqlite.AccountDAO; +import app.fedilab.fedilabtube.sqlite.Sqlite; @SuppressWarnings("unused") @@ -106,6 +117,66 @@ public class HttpsConnection { } } + /** + * Update user credentials + * + * @param _mContext Context + * @param token String + * @param client_id String + * @param client_secret String + * @param refresh_token String + * @param host String + */ + public static void updateCredential(Context _mContext, String token, String client_id, String client_secret, String refresh_token, String host) { + new Thread(() -> { + Account account; + String instance; + if (host.startsWith("tube")) { + instance = host; + } else { + instance = Helper.getPeertubeUrl(host); + } + account = new PeertubeAPI(_mContext).verifyCredentials(token, instance); + if (account == null) + return; + try { + //At the state the instance can be encoded + instance = URLDecoder.decode(instance, "utf-8"); + } catch (UnsupportedEncodingException ignored) { + } + SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + account.setToken(token); + account.setClient_id(client_id); + account.setClient_secret(client_secret); + account.setRefresh_token(refresh_token); + account.setInstance(instance); + SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + boolean userExists = new AccountDAO(_mContext, db).userExist(account); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.PREF_KEY_ID, account.getId()); + editor.putBoolean(Helper.PREF_IS_MODERATOR, account.isModerator()); + editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, account.isAdmin()); + if (!host.startsWith("tube")) { + editor.putString(Helper.PREF_INSTANCE, host); + } + editor.apply(); + if (userExists) + new AccountDAO(_mContext, db).updateAccountCredential(account); + else { + if (account.getUsername() != null && account.getCreated_at() != null) + new AccountDAO(_mContext, db).insertAccount(account); + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + Intent mainActivity = new Intent(_mContext, MainActivity.class); + mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT); + _mContext.startActivity(mainActivity); + ((Activity) _mContext).finish(); + }; + mainHandler.post(myRunnable); + }).start(); + } + /** * Get calls * @@ -320,7 +391,6 @@ public class HttpsConnection { return writer; } - public String post(String urlConnection, int timeout, HashMap paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { URL url = new URL(urlConnection); Map params = new LinkedHashMap<>(); @@ -386,7 +456,6 @@ public class HttpsConnection { return response; } - public InputStream getPicture(final String downloadUrl) { URL url; @@ -694,7 +763,6 @@ public class HttpsConnection { return max_id; } - private void getSinceMaxId() { if (httpsURLConnection == null) @@ -739,7 +807,6 @@ public class HttpsConnection { } } - public class HttpsConnectionException extends Exception { private int statusCode; diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayAccountsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayAccountsFragment.java index 7782756..877e896 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayAccountsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayAccountsFragment.java @@ -196,13 +196,12 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda if (firstVisibleItem + visibleItemCount == totalItemCount) { if (!flag_loading) { flag_loading = true; + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this).get(AccountsVM.class); if (accountFetch == null) { - AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); } else { - String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : max_id; - AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); - viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); + String param = accountFetch == AccountsVM.accountFetch.CHANNEL ? name : max_id; + viewModel.getAccounts(param, null, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); } nextElementLoader.setVisibility(View.VISIBLE); } @@ -213,14 +212,12 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda } }); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); - + AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class); if (accountFetch == null) { - AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } else { - String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null; - AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); - viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); + String param = accountFetch == AccountsVM.accountFetch.CHANNEL ? name : null; + viewModel.getAccounts(param, null, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } return rootView; } @@ -228,7 +225,7 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda @Override public void onResume() { super.onResume(); - if (accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL) { + if (accountFetch == AccountsVM.accountFetch.CHANNEL) { action_button.setVisibility(View.VISIBLE); } else { action_button.setVisibility(View.GONE); @@ -274,7 +271,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda return; } flag_loading = (apiResponse.getMax_id() == null); - List accounts = apiResponse.getAccounts(); if (!swiped && firstLoad && (accounts == null || accounts.size() == 0)) textviewNoAction.setVisibility(View.VISIBLE); @@ -282,7 +278,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda textviewNoAction.setVisibility(View.GONE); max_id = apiResponse.getMax_id(); - if (swiped) { accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts); lv_accounts.setAdapter(accountsListAdapter); @@ -304,13 +299,12 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda flag_loading = true; swiped = true; swipeRefreshLayout.setRefreshing(true); + AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class); if (accountFetch == null) { - AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } else { - String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null; - AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); - viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); + String param = accountFetch == AccountsVM.accountFetch.CHANNEL ? name : null; + viewModel.getAccounts(param, null, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java index 0eca363..f7c10f9 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java @@ -92,7 +92,7 @@ public class DisplayNotificationsFragment extends Fragment { if (firstVisibleItem + visibleItemCount == totalItemCount) { if (!flag_loading) { flag_loading = true; - NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this.requireActivity()).get(NotificationsVM.class); + NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this).get(NotificationsVM.class); viewModel.getNotifications(null, max_id).observe(DisplayNotificationsFragment.this.requireActivity(), apiResponse -> manageVIewNotifications(apiResponse)); nextElementLoader.setVisibility(View.VISIBLE); } @@ -104,7 +104,7 @@ public class DisplayNotificationsFragment extends Fragment { }); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); - NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this.requireActivity()).get(NotificationsVM.class); + NotificationsVM viewModel = new ViewModelProvider(this).get(NotificationsVM.class); viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications); return rootView; } @@ -144,7 +144,7 @@ public class DisplayNotificationsFragment extends Fragment { flag_loading = true; swiped = true; swipeRefreshLayout.setRefreshing(true); - NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this.requireActivity()).get(NotificationsVM.class); + NotificationsVM viewModel = new ViewModelProvider(this).get(NotificationsVM.class); viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayPlaylistsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayPlaylistsFragment.java index ff7dd45..58a1e87 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayPlaylistsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayPlaylistsFragment.java @@ -102,7 +102,7 @@ public class DisplayPlaylistsFragment extends Fragment { playlists = new ArrayList<>(); playlistAdapter = new PlaylistAdapter(context, playlists, textviewNoAction); lv_playlist.setAdapter(playlistAdapter); - PlaylistsVM viewModel = new ViewModelProvider(DisplayPlaylistsFragment.this.requireActivity()).get(PlaylistsVM.class); + PlaylistsVM viewModel = new ViewModelProvider(this).get(PlaylistsVM.class); viewModel.manage(PlaylistsVM.action.GET_PLAYLIST, null, null, null).observe(DisplayPlaylistsFragment.this.requireActivity(), apiResponse -> manageVIewPlaylists(PlaylistsVM.action.GET_PLAYLIST, apiResponse)); add_new = rootView.findViewById(R.id.add_new); @@ -130,7 +130,7 @@ public class DisplayPlaylistsFragment extends Fragment { set_upload_privacy = dialogView.findViewById(R.id.set_upload_privacy); - ChannelsVM viewModelC = new ViewModelProvider(DisplayPlaylistsFragment.this.requireActivity()).get(ChannelsVM.class); + ChannelsVM viewModelC = new ViewModelProvider(this).get(ChannelsVM.class); viewModelC.get().observe(DisplayPlaylistsFragment.this.requireActivity(), this::manageVIewChannels); display_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(120)}); diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java index 9094615..e1d210f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java @@ -161,10 +161,10 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); //Load data depending of the value if (search_peertube == null) { //Not a Peertube search - FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + FeedsVM viewModel = new ViewModelProvider(this).get(FeedsVM.class); viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos); } else { - SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class); + SearchVM viewModel = new ViewModelProvider(this).get(SearchVM.class); viewModel.getVideos("0", search_peertube).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos); } @@ -175,7 +175,7 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta int visibleItemCount = mLayoutManager.getChildCount(); int totalItemCount = mLayoutManager.getItemCount(); if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) { - AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class); + AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(AccountsVM.class); viewModel.getAccounts(max_id_accounts, null, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); } } @@ -211,10 +211,10 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta if (!flag_loading) { flag_loading = true; if (search_peertube == null) { //Not a Peertube search - FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(FeedsVM.class); viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); } else { - SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class); + SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(SearchVM.class); viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); } nextElementLoader.setVisibility(View.VISIBLE); @@ -232,10 +232,10 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta if (!flag_loading) { flag_loading = true; if (search_peertube == null) { //Not a Peertube search - FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(FeedsVM.class); viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); } else { - SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class); + SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(SearchVM.class); viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); } nextElementLoader.setVisibility(View.VISIBLE); @@ -249,7 +249,7 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta }); } if (type == PSUBSCRIPTIONS) { - AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class); + AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class); viewModel.getAccounts(max_id, null, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), this::manageViewAccounts); } @@ -403,11 +403,11 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta accountsHorizontalListAdapter.notifyItemRangeRemoved(0, accounts.size()); } if (search_peertube == null) { //Not a Peertube search - FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); - viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos); + FeedsVM viewModel = new ViewModelProvider(this).get(FeedsVM.class); + viewModel.getVideos(type, "0", targetedId, null).observe(this.requireActivity(), this::manageVIewVideos); } else { - SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class); - viewModel.getVideos("0", search_peertube).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos); + SearchVM viewModel = new ViewModelProvider(this).get(SearchVM.class); + viewModel.getVideos("0", search_peertube).observe(this.requireActivity(), this::manageVIewVideos); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java index 2c6b103..7d168c2 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java @@ -46,8 +46,10 @@ public class FeedsVM extends AndroidViewModel { } public LiveData getVideos(Type action, String max_id, String target, String forAccount) { - apiResponseMutableLiveData = new MutableLiveData<>(); - loadVideos(action, max_id, target, forAccount); + if (apiResponseMutableLiveData == null) { + apiResponseMutableLiveData = new MutableLiveData<>(); + loadVideos(action, max_id, target, forAccount); + } return apiResponseMutableLiveData; } @@ -73,7 +75,7 @@ public class FeedsVM extends AndroidViewModel { if (apiResponse != null && apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0) apiResponse.getPeertubes().get(0).setUpdate(true); Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse); + Runnable myRunnable = () -> apiResponseMutableLiveData.postValue(apiResponse); mainHandler.post(myRunnable); } catch (Exception e) { e.printStackTrace(); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java index 1279f05..6ea23c0 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java @@ -40,8 +40,10 @@ public class NotificationsVM extends AndroidViewModel { } public LiveData getNotifications(Account account, String max_id) { - apiResponseMutableLiveData = new MutableLiveData<>(); - loadNotifications(account, max_id); + if (apiResponseMutableLiveData == null) { + apiResponseMutableLiveData = new MutableLiveData<>(); + loadNotifications(account, max_id); + } return apiResponseMutableLiveData; } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java deleted file mode 100644 index f146aa9..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java +++ /dev/null @@ -1,107 +0,0 @@ -package app.fedilab.fedilabtube.viewmodel; -/* Copyright 2020 Thomas Schneider - * - * This file is a part of TubeLab - * - * This program is free software; you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation; either version 3 of the - * License, or (at your option) any later version. - * - * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even - * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. - * - * You should have received a copy of the GNU General Public License along with TubeLab; if not, - * see . */ - -import android.app.Activity; -import android.app.Application; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.database.sqlite.SQLiteDatabase; -import android.os.Handler; -import android.os.Looper; - -import androidx.annotation.NonNull; -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; - -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; - -import app.fedilab.fedilabtube.MainActivity; -import app.fedilab.fedilabtube.client.APIResponse; -import app.fedilab.fedilabtube.client.PeertubeAPI; -import app.fedilab.fedilabtube.client.entities.Account; -import app.fedilab.fedilabtube.helper.Helper; -import app.fedilab.fedilabtube.sqlite.AccountDAO; -import app.fedilab.fedilabtube.sqlite.Sqlite; - - -public class OwnAccountVM extends AndroidViewModel { - private MutableLiveData apiResponseMutableLiveData; - private Application application; - - public OwnAccountVM(@NonNull Application application) { - super(application); - this.application = application; - } - - public LiveData updateCredential(String token, String client_id, String client_secret, String refresh_token, String host) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - Context _mContext = this.application.getApplicationContext(); - new Thread(() -> { - Account account; - String instance; - if (host.startsWith("tube")) { - instance = host; - } else { - instance = Helper.getPeertubeUrl(host); - } - account = new PeertubeAPI(_mContext).verifyCredentials(token, instance); - if (account == null) - return; - try { - //At the state the instance can be encoded - instance = URLDecoder.decode(instance, "utf-8"); - } catch (UnsupportedEncodingException ignored) { - } - SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - account.setToken(token); - account.setClient_id(client_id); - account.setClient_secret(client_secret); - account.setRefresh_token(refresh_token); - account.setInstance(instance); - SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - boolean userExists = new AccountDAO(_mContext, db).userExist(account); - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(Helper.PREF_KEY_ID, account.getId()); - editor.putBoolean(Helper.PREF_IS_MODERATOR, account.isModerator()); - editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, account.isAdmin()); - if (!host.startsWith("tube")) { - editor.putString(Helper.PREF_INSTANCE, host); - } - editor.apply(); - if (userExists) - new AccountDAO(_mContext, db).updateAccountCredential(account); - else { - if (account.getUsername() != null && account.getCreated_at() != null) - new AccountDAO(_mContext, db).insertAccount(account); - } - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> { - Intent mainActivity = new Intent(_mContext, MainActivity.class); - mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT); - _mContext.startActivity(mainActivity); - ((Activity) _mContext).finish(); - }; - mainHandler.post(myRunnable); - }).start(); - } - return apiResponseMutableLiveData; - } - -}