This commit is contained in:
Thomas 2020-09-08 15:54:07 +02:00
parent 8c7d1c692b
commit a1b816030a
11 changed files with 114 additions and 158 deletions

View File

@ -30,7 +30,6 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.textfield.TextInputLayout; 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.client.HttpsConnection;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.viewmodel.OwnAccountVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
import static app.fedilab.fedilabtube.client.HttpsConnection.updateCredential;
public class LoginActivity extends AppCompatActivity { public class LoginActivity extends AppCompatActivity {
@ -292,8 +292,7 @@ public class LoginActivity extends AppCompatActivity {
editor.putString(Helper.PREF_INSTANCE, host); editor.putString(Helper.PREF_INSTANCE, host);
editor.apply(); editor.apply();
//Update the account with the token; //Update the account with the token;
OwnAccountVM viewModelC = new ViewModelProvider(LoginActivity.this).get(OwnAccountVM.class); updateCredential(LoginActivity.this, token, client_id, client_secret, refresh_token, host);
viewModelC.updateCredential(token, client_id, client_secret, refresh_token, host);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
runOnUiThread(() -> connectionButton.setEnabled(true)); runOnUiThread(() -> connectionButton.setEnabled(true));

View File

@ -118,7 +118,7 @@ public class ShowAccountActivity extends AppCompatActivity {
AccountsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(AccountsVM.class); AccountsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(AccountsVM.class);
if (account != null) { if (account != null) {
manageAccount(); 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 { } else {
viewModel.getAccounts(null, accountId, AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts); viewModel.getAccounts(null, accountId, AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts);
} }

View File

@ -28,7 +28,6 @@ import android.widget.ProgressBar;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import org.json.JSONObject; import org.json.JSONObject;
@ -38,9 +37,10 @@ import java.util.regex.Matcher;
import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.viewmodel.OwnAccountVM;
import app.fedilab.fedilabtube.webview.CustomWebview; import app.fedilab.fedilabtube.webview.CustomWebview;
import static app.fedilab.fedilabtube.client.HttpsConnection.updateCredential;
public class WebviewConnectActivity extends AppCompatActivity { 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_KEY_OAUTH_TOKEN, token);
editor.putString(Helper.PREF_INSTANCE, new URL(url).getHost()); editor.putString(Helper.PREF_INSTANCE, new URL(url).getHost());
editor.apply(); editor.apply();
OwnAccountVM viewModelC = new ViewModelProvider(WebviewConnectActivity.this).get(OwnAccountVM.class); updateCredential(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, new URL(url).getHost());
viewModelC.updateCredential(token, clientId, clientSecret, refresh_token, new URL(url).getHost());
finish(); finish();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -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, * You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build; import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.Html; import android.text.Html;
import android.text.SpannableString; import android.text.SpannableString;
@ -34,6 +39,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -41,6 +47,7 @@ import java.net.MalformedURLException;
import java.net.PasswordAuthentication; import java.net.PasswordAuthentication;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -55,8 +62,12 @@ import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import app.fedilab.fedilabtube.MainActivity;
import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
@SuppressWarnings("unused") @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 * Get calls
* *
@ -320,7 +391,6 @@ public class HttpsConnection {
return writer; return writer;
} }
public String post(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { public String post(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
URL url = new URL(urlConnection); URL url = new URL(urlConnection);
Map<String, Object> params = new LinkedHashMap<>(); Map<String, Object> params = new LinkedHashMap<>();
@ -386,7 +456,6 @@ public class HttpsConnection {
return response; return response;
} }
public InputStream getPicture(final String downloadUrl) { public InputStream getPicture(final String downloadUrl) {
URL url; URL url;
@ -694,7 +763,6 @@ public class HttpsConnection {
return max_id; return max_id;
} }
private void getSinceMaxId() { private void getSinceMaxId() {
if (httpsURLConnection == null) if (httpsURLConnection == null)
@ -739,7 +807,6 @@ public class HttpsConnection {
} }
} }
public class HttpsConnectionException extends Exception { public class HttpsConnectionException extends Exception {
private int statusCode; private int statusCode;

View File

@ -196,13 +196,12 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
if (firstVisibleItem + visibleItemCount == totalItemCount) { if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) { if (!flag_loading) {
flag_loading = true; flag_loading = true;
AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this).get(AccountsVM.class);
if (accountFetch == null) { 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)); viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse));
} else { } else {
String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : max_id; String param = accountFetch == AccountsVM.accountFetch.CHANNEL ? name : max_id;
AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); viewModel.getAccounts(param, null, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse));
viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse));
} }
nextElementLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.VISIBLE);
} }
@ -213,14 +212,12 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
} }
}); });
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);
AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class);
if (accountFetch == null) { 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); viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
} else { } else {
String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null; String param = accountFetch == AccountsVM.accountFetch.CHANNEL ? name : null;
AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); viewModel.getAccounts(param, null, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
} }
return rootView; return rootView;
} }
@ -228,7 +225,7 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL) { if (accountFetch == AccountsVM.accountFetch.CHANNEL) {
action_button.setVisibility(View.VISIBLE); action_button.setVisibility(View.VISIBLE);
} else { } else {
action_button.setVisibility(View.GONE); action_button.setVisibility(View.GONE);
@ -274,7 +271,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
return; return;
} }
flag_loading = (apiResponse.getMax_id() == null); flag_loading = (apiResponse.getMax_id() == null);
List<Account> accounts = apiResponse.getAccounts(); List<Account> accounts = apiResponse.getAccounts();
if (!swiped && firstLoad && (accounts == null || accounts.size() == 0)) if (!swiped && firstLoad && (accounts == null || accounts.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE); textviewNoAction.setVisibility(View.VISIBLE);
@ -282,7 +278,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
textviewNoAction.setVisibility(View.GONE); textviewNoAction.setVisibility(View.GONE);
max_id = apiResponse.getMax_id(); max_id = apiResponse.getMax_id();
if (swiped) { if (swiped) {
accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts); accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts);
lv_accounts.setAdapter(accountsListAdapter); lv_accounts.setAdapter(accountsListAdapter);
@ -304,13 +299,12 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
flag_loading = true; flag_loading = true;
swiped = true; swiped = true;
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class);
if (accountFetch == null) { 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); viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
} else { } else {
String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null; String param = accountFetch == AccountsVM.accountFetch.CHANNEL ? name : null;
AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); viewModel.getAccounts(param, null, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
} }
} }

View File

@ -92,7 +92,7 @@ public class DisplayNotificationsFragment extends Fragment {
if (firstVisibleItem + visibleItemCount == totalItemCount) { if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) { if (!flag_loading) {
flag_loading = true; 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)); viewModel.getNotifications(null, max_id).observe(DisplayNotificationsFragment.this.requireActivity(), apiResponse -> manageVIewNotifications(apiResponse));
nextElementLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.VISIBLE);
} }
@ -104,7 +104,7 @@ public class DisplayNotificationsFragment extends Fragment {
}); });
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); 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); viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications);
return rootView; return rootView;
} }
@ -144,7 +144,7 @@ public class DisplayNotificationsFragment extends Fragment {
flag_loading = true; flag_loading = true;
swiped = true; swiped = true;
swipeRefreshLayout.setRefreshing(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); viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications);
} }

View File

@ -102,7 +102,7 @@ public class DisplayPlaylistsFragment extends Fragment {
playlists = new ArrayList<>(); playlists = new ArrayList<>();
playlistAdapter = new PlaylistAdapter(context, playlists, textviewNoAction); playlistAdapter = new PlaylistAdapter(context, playlists, textviewNoAction);
lv_playlist.setAdapter(playlistAdapter); 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)); 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); 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); 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); viewModelC.get().observe(DisplayPlaylistsFragment.this.requireActivity(), this::manageVIewChannels);
display_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(120)}); display_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(120)});

View File

@ -161,10 +161,10 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);
//Load data depending of the value //Load data depending of the value
if (search_peertube == null) { //Not a Peertube search 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); viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos);
} else { } 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); 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 visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount(); int totalItemCount = mLayoutManager.getItemCount();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) { 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)); 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) { if (!flag_loading) {
flag_loading = true; flag_loading = true;
if (search_peertube == null) { //Not a Peertube search 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)); viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
} else { } 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)); viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
} }
nextElementLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.VISIBLE);
@ -232,10 +232,10 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta
if (!flag_loading) { if (!flag_loading) {
flag_loading = true; flag_loading = true;
if (search_peertube == null) { //Not a Peertube search 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)); viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
} else { } 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)); viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
} }
nextElementLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.VISIBLE);
@ -249,7 +249,7 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta
}); });
} }
if (type == PSUBSCRIPTIONS) { 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); 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()); accountsHorizontalListAdapter.notifyItemRangeRemoved(0, accounts.size());
} }
if (search_peertube == null) { //Not a Peertube search 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); viewModel.getVideos(type, "0", targetedId, null).observe(this.requireActivity(), this::manageVIewVideos);
} else { } 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); viewModel.getVideos("0", search_peertube).observe(this.requireActivity(), this::manageVIewVideos);
} }
} }

View File

@ -46,8 +46,10 @@ public class FeedsVM extends AndroidViewModel {
} }
public LiveData<APIResponse> getVideos(Type action, String max_id, String target, String forAccount) { public LiveData<APIResponse> getVideos(Type action, String max_id, String target, String forAccount) {
apiResponseMutableLiveData = new MutableLiveData<>(); if (apiResponseMutableLiveData == null) {
loadVideos(action, max_id, target, forAccount); apiResponseMutableLiveData = new MutableLiveData<>();
loadVideos(action, max_id, target, forAccount);
}
return apiResponseMutableLiveData; return apiResponseMutableLiveData;
} }
@ -73,7 +75,7 @@ public class FeedsVM extends AndroidViewModel {
if (apiResponse != null && apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0) if (apiResponse != null && apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0)
apiResponse.getPeertubes().get(0).setUpdate(true); apiResponse.getPeertubes().get(0).setUpdate(true);
Handler mainHandler = new Handler(Looper.getMainLooper()); Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse); Runnable myRunnable = () -> apiResponseMutableLiveData.postValue(apiResponse);
mainHandler.post(myRunnable); mainHandler.post(myRunnable);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -40,8 +40,10 @@ public class NotificationsVM extends AndroidViewModel {
} }
public LiveData<APIResponse> getNotifications(Account account, String max_id) { public LiveData<APIResponse> getNotifications(Account account, String max_id) {
apiResponseMutableLiveData = new MutableLiveData<>(); if (apiResponseMutableLiveData == null) {
loadNotifications(account, max_id); apiResponseMutableLiveData = new MutableLiveData<>();
loadNotifications(account, max_id);
}
return apiResponseMutableLiveData; return apiResponseMutableLiveData;
} }

View File

@ -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 <http://www.gnu.org/licenses>. */
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<APIResponse> apiResponseMutableLiveData;
private Application application;
public OwnAccountVM(@NonNull Application application) {
super(application);
this.application = application;
}
public LiveData<APIResponse> 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;
}
}