diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index 1468a4a..e41f68e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -16,7 +16,6 @@ package app.fedilab.fedilabtube; import android.content.Intent; import android.content.SharedPreferences; -import android.os.AsyncTask; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; @@ -31,6 +30,7 @@ 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; @@ -42,9 +42,9 @@ import java.net.URLEncoder; import java.util.Arrays; import java.util.HashMap; -import app.fedilab.fedilabtube.asynctasks.UpdateAccountInfoAsyncTask; import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.viewmodel.OwnAccountVM; import es.dmoral.toasty.Toasty; @@ -292,7 +292,8 @@ public class LoginActivity extends AppCompatActivity { editor.putString(Helper.PREF_INSTANCE, host); editor.apply(); //Update the account with the token; - new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, host).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + OwnAccountVM viewModelC = new ViewModelProvider(LoginActivity.this).get(OwnAccountVM.class); + viewModelC.updateCredential(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 da76471..60060a5 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java @@ -17,7 +17,6 @@ package app.fedilab.fedilabtube; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.text.Html; @@ -51,20 +50,17 @@ import org.jetbrains.annotations.NotNull; import java.util.List; -import app.fedilab.fedilabtube.asynctasks.RetrieveRelationshipAsyncTask; -import app.fedilab.fedilabtube.asynctasks.RetrieveSingleAccountAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Account; -import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.client.entities.Relationship; import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment; import app.fedilab.fedilabtube.fragment.DisplayStatusFragment; import app.fedilab.fedilabtube.helper.Helper; -import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; -import app.fedilab.fedilabtube.interfaces.OnRetrieveRelationshipInterface; +import app.fedilab.fedilabtube.viewmodel.AccountsVM; import app.fedilab.fedilabtube.viewmodel.FeedsVM; import app.fedilab.fedilabtube.viewmodel.PostActionsVM; +import app.fedilab.fedilabtube.viewmodel.RelationshipVM; import es.dmoral.toasty.Toasty; import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY; @@ -72,7 +68,7 @@ import static app.fedilab.fedilabtube.helper.Helper.getLiveInstance; import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn; -public class ShowAccountActivity extends AppCompatActivity implements OnRetrieveRelationshipInterface, OnRetrieveAccountsInterface { +public class ShowAccountActivity extends AppCompatActivity { private Button account_follow; @@ -85,7 +81,6 @@ public class ShowAccountActivity extends AppCompatActivity implements OnRetrieve private Account account; private String accountId; private boolean ischannel; - private AsyncTask> retrieveRelationship; private action doAction; @Override @@ -120,11 +115,12 @@ public class ShowAccountActivity extends AppCompatActivity implements OnRetrieve account_note = findViewById(R.id.account_note); + AccountsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(AccountsVM.class); if (account != null) { manageAccount(); - new RetrieveSingleAccountAsyncTask(ShowAccountActivity.this, account.getAcct(), RetrieveSingleAccountAsyncTask.actionType.CHANNEL, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + viewModel.getAccounts(null, account.getAcct(), AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(ShowAccountActivity.this, this::manageViewAccounts); } else { - new RetrieveSingleAccountAsyncTask(ShowAccountActivity.this, accountId, RetrieveSingleAccountAsyncTask.actionType.CHANNEL, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + viewModel.getAccounts(null, accountId, AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts); } } @@ -172,7 +168,8 @@ public class ShowAccountActivity extends AppCompatActivity implements OnRetrieve String accountIdRelation = account.getAcct(); if (isLoggedIn(ShowAccountActivity.this)) { - retrieveRelationship = new RetrieveRelationshipAsyncTask(ShowAccountActivity.this, accountIdRelation, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + RelationshipVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(RelationshipVM.class); + viewModel.get(accountIdRelation).observe(ShowAccountActivity.this, this::manageVIewRelationship); } setTitle(account.getAcct()); @@ -288,13 +285,13 @@ public class ShowAccountActivity extends AppCompatActivity implements OnRetrieve } - @Override - public void onRetrieveRelationship(List relationships, Error error) { + public void manageVIewRelationship(APIResponse apiResponse) { - if (error != null) { - Toasty.error(ShowAccountActivity.this, error.getError(), Toast.LENGTH_LONG).show(); + if (apiResponse.getError() != null) { + Toasty.error(ShowAccountActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); return; } + List relationships = apiResponse.getRelationships(); this.relationship = relationships.get(0); manageButtonVisibility(); @@ -358,9 +355,6 @@ public class ShowAccountActivity extends AppCompatActivity implements OnRetrieve @Override public void onStop() { super.onStop(); - if (retrieveRelationship != null && !retrieveRelationship.isCancelled()) { - retrieveRelationship.cancel(true); - } } @@ -379,14 +373,14 @@ public class ShowAccountActivity extends AppCompatActivity implements OnRetrieve intentBC.putExtras(b); } if (statusAction == PeertubeAPI.StatusAction.UNFOLLOW || statusAction == PeertubeAPI.StatusAction.FOLLOW) { - retrieveRelationship = new RetrieveRelationshipAsyncTask(ShowAccountActivity.this, target, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + RelationshipVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(RelationshipVM.class); + viewModel.get(target).observe(ShowAccountActivity.this, this::manageVIewRelationship); } else if (statusAction == PeertubeAPI.StatusAction.MUTE) { Toasty.info(ShowAccountActivity.this, getString(R.string.muted_done), Toast.LENGTH_LONG).show(); } } - @Override - public void onRetrieveAccounts(APIResponse apiResponse) { + public void manageViewAccounts(APIResponse apiResponse) { if (apiResponse.getAccounts() != null && apiResponse.getAccounts().size() == 1) { Account account = apiResponse.getAccounts().get(0); if (this.account == null) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java b/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java index a3d47c1..1e5eb40 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java @@ -17,7 +17,6 @@ package app.fedilab.fedilabtube; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.webkit.CookieManager; @@ -29,6 +28,7 @@ import android.widget.ProgressBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.ViewModelProvider; import org.json.JSONObject; @@ -36,9 +36,9 @@ import java.net.URL; import java.util.HashMap; import java.util.regex.Matcher; -import app.fedilab.fedilabtube.asynctasks.UpdateAccountInfoAsyncTask; import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.viewmodel.OwnAccountVM; import app.fedilab.fedilabtube.webview.CustomWebview; @@ -135,7 +135,8 @@ public class WebviewConnectActivity extends AppCompatActivity { editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); editor.putString(Helper.PREF_INSTANCE, new URL(url).getHost()); editor.apply(); - new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, new URL(url).getHost()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + OwnAccountVM viewModelC = new ViewModelProvider(WebviewConnectActivity.this).get(OwnAccountVM.class); + viewModelC.updateCredential(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/asynctasks/RetrieveRelationshipAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveRelationshipAsyncTask.java deleted file mode 100644 index 2f6de3c..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveRelationshipAsyncTask.java +++ /dev/null @@ -1,57 +0,0 @@ -package app.fedilab.fedilabtube.asynctasks; -/* 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.content.Context; -import android.os.AsyncTask; - -import java.lang.ref.WeakReference; -import java.util.List; - -import app.fedilab.fedilabtube.client.PeertubeAPI; -import app.fedilab.fedilabtube.client.entities.Error; -import app.fedilab.fedilabtube.client.entities.Relationship; -import app.fedilab.fedilabtube.interfaces.OnRetrieveRelationshipInterface; - - -public class RetrieveRelationshipAsyncTask extends AsyncTask> { - - - private String accountId; - private OnRetrieveRelationshipInterface listener; - private Error error; - private WeakReference contextReference; - - public RetrieveRelationshipAsyncTask(Context context, String accountId, OnRetrieveRelationshipInterface onRetrieveRelationshipInterface) { - this.contextReference = new WeakReference<>(context); - this.listener = onRetrieveRelationshipInterface; - this.accountId = accountId; - } - - @Override - protected List doInBackground(Void... params) { - - PeertubeAPI api = new PeertubeAPI(this.contextReference.get()); - List relationships = api.isFollowing(accountId); - error = api.getError(); - return relationships; - } - - @Override - protected void onPostExecute(List relationships) { - listener.onRetrieveRelationship(relationships, error); - } - -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveSingleAccountAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveSingleAccountAsyncTask.java deleted file mode 100644 index f2f40d2..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveSingleAccountAsyncTask.java +++ /dev/null @@ -1,63 +0,0 @@ -package app.fedilab.fedilabtube.asynctasks; -/* 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.content.Context; -import android.os.AsyncTask; - -import java.lang.ref.WeakReference; - -import app.fedilab.fedilabtube.client.APIResponse; -import app.fedilab.fedilabtube.client.PeertubeAPI; -import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; - - -public class RetrieveSingleAccountAsyncTask extends AsyncTask { - - private OnRetrieveAccountsInterface listener; - private WeakReference contextReference; - private String name; - private actionType type; - - public RetrieveSingleAccountAsyncTask(Context context, String name, actionType type, OnRetrieveAccountsInterface onRetrieveAccountsInterface) { - this.contextReference = new WeakReference<>(context); - this.name = name; - this.listener = onRetrieveAccountsInterface; - this.type = type; - } - - @Override - protected APIResponse doInBackground(Void... params) { - PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get()); - if (type == actionType.ACCOUNT) { - return peertubeAPI.getPeertubeChannel(name); - } else if (type == actionType.CHANNEL) { - return peertubeAPI.getPeertubeChannelInfo(name); - } else { - return null; - } - } - - @Override - protected void onPostExecute(APIResponse apiResponse) { - listener.onRetrieveAccounts(apiResponse); - } - - public enum actionType { - ACCOUNT, - CHANNEL - } - -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java deleted file mode 100644 index c045d98..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/UpdateAccountInfoAsyncTask.java +++ /dev/null @@ -1,110 +0,0 @@ -package app.fedilab.fedilabtube.asynctasks; -/* 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.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.database.sqlite.SQLiteDatabase; -import android.os.AsyncTask; - -import java.io.UnsupportedEncodingException; -import java.lang.ref.WeakReference; -import java.net.URLDecoder; - -import app.fedilab.fedilabtube.MainActivity; -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 UpdateAccountInfoAsyncTask extends AsyncTask { - - private String token, client_id, client_secret, refresh_token; - private String host; - private WeakReference contextReference; - - public UpdateAccountInfoAsyncTask(Context context, String token, String client_id, String client_secret, String refresh_token, String host) { - this.contextReference = new WeakReference<>(context); - this.token = token; - this.host = host; - this.client_id = client_id; - this.client_secret = client_secret; - this.refresh_token = refresh_token; - } - - @Override - protected Void doInBackground(Void... params) { - Account account; - if (this.contextReference == null) { - return null; - } - String instance; - if (host.startsWith("tube")) { - instance = host; - } else { - instance = Helper.getPeertubeUrl(host); - } - account = new PeertubeAPI(this.contextReference.get()).verifyCredentials(token, instance); - if (account == null) - return null; - try { - //At the state the instance can be encoded - instance = URLDecoder.decode(instance, "utf-8"); - } catch (UnsupportedEncodingException ignored) { - } - - SharedPreferences sharedpreferences = this.contextReference.get().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(this.contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - boolean userExists = new AccountDAO(this.contextReference.get(), 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(this.contextReference.get(), db).updateAccountCredential(account); - else { - if (account.getUsername() != null && account.getCreated_at() != null) - new AccountDAO(this.contextReference.get(), db).insertAccount(account); - } - - return null; - } - - @Override - protected void onPostExecute(Void result) { - - if (this.contextReference.get() != null) { - Intent mainActivity = new Intent(this.contextReference.get(), MainActivity.class); - mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT); - this.contextReference.get().startActivity(mainActivity); - ((Activity) this.contextReference.get()).finish(); - } - - } - -} \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java b/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java index 2a3fbc9..933802c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/APIResponse.java @@ -23,6 +23,7 @@ import app.fedilab.fedilabtube.client.entities.Peertube; import app.fedilab.fedilabtube.client.entities.PeertubeNotification; import app.fedilab.fedilabtube.client.entities.Playlist; import app.fedilab.fedilabtube.client.entities.PlaylistElement; +import app.fedilab.fedilabtube.client.entities.Relationship; import app.fedilab.fedilabtube.client.entities.Status; @SuppressWarnings("unused") @@ -35,6 +36,7 @@ public class APIResponse { private List peertubeNotifications = null; private List playlists = null; private List domains = null; + private List relationships = null; private Error error = null; private String since_id, max_id; private List playlistForVideos; @@ -159,4 +161,12 @@ public class APIResponse { public void setStatusCode(int statusCode) { this.statusCode = statusCode; } + + public List getRelationships() { + return relationships; + } + + public void setRelationships(List relationships) { + this.relationships = relationships; + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveAccountsInterface.java b/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveAccountsInterface.java deleted file mode 100644 index 42275a9..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveAccountsInterface.java +++ /dev/null @@ -1,21 +0,0 @@ -package app.fedilab.fedilabtube.interfaces; -/* 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 app.fedilab.fedilabtube.client.APIResponse; - -public interface OnRetrieveAccountsInterface { - void onRetrieveAccounts(APIResponse apiResponse); -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java b/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java deleted file mode 100644 index 959f461..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java +++ /dev/null @@ -1,24 +0,0 @@ -package app.fedilab.fedilabtube.interfaces; -/* 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 java.util.List; - -import app.fedilab.fedilabtube.client.entities.Error; -import app.fedilab.fedilabtube.client.entities.Relationship; - -public interface OnRetrieveRelationshipInterface { - void onRetrieveRelationship(List relationships, Error error); -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java new file mode 100644 index 0000000..f146aa9 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/OwnAccountVM.java @@ -0,0 +1,107 @@ +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; + } + +} diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java new file mode 100644 index 0000000..b253f69 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java @@ -0,0 +1,71 @@ +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.Application; +import android.content.Context; +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.util.List; + +import app.fedilab.fedilabtube.client.APIResponse; +import app.fedilab.fedilabtube.client.PeertubeAPI; +import app.fedilab.fedilabtube.client.entities.Error; +import app.fedilab.fedilabtube.client.entities.Relationship; + + +public class RelationshipVM extends AndroidViewModel { + private MutableLiveData apiResponseMutableLiveData; + private Application application; + + public RelationshipVM(@NonNull Application application) { + super(application); + this.application = application; + } + + public LiveData get(String targetedId) { + if (apiResponseMutableLiveData == null) { + apiResponseMutableLiveData = new MutableLiveData<>(); + getRelationShip(targetedId); + } + return apiResponseMutableLiveData; + } + + private void getRelationShip(String targetedId) { + Context _mContext = this.application.getApplicationContext(); + new Thread(() -> { + try { + PeertubeAPI api = new PeertubeAPI(_mContext); + List relationships = api.isFollowing(targetedId); + Error error = api.getError(); + APIResponse apiResponse = new APIResponse(); + apiResponse.setRelationships(relationships); + apiResponse.setError(error); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(apiResponse); + mainHandler.post(myRunnable); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + } + +}