Some fixes

This commit is contained in:
Thomas 2020-12-31 15:32:38 +01:00
parent 36073d340d
commit b932ed2c13
7 changed files with 11 additions and 154 deletions

View File

@ -15,6 +15,7 @@ package app.fedilab.fedilabtube;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle; import android.os.Bundle;
import android.text.SpannableString; import android.text.SpannableString;

View File

@ -799,7 +799,7 @@ public class MainActivity extends AppCompatActivity implements ChromeCastsListen
if (typeOfConnection == SURFING) { if (typeOfConnection == SURFING) {
SwitchAccountHelper.switchDialog(MainActivity.this, false); SwitchAccountHelper.switchDialog(MainActivity.this, false);
} else { } else {
if (Helper.isLoggedIn(MainActivity.this)) { if (Helper.canMakeAction(MainActivity.this)) {
intent = new Intent(MainActivity.this, AccountActivity.class); intent = new Intent(MainActivity.this, AccountActivity.class);
} else { } else {
intent = new Intent(MainActivity.this, LoginActivity.class); intent = new Intent(MainActivity.this, LoginActivity.class);

View File

@ -69,14 +69,16 @@ public class RetrofitMastodonAPI {
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_ID, account.getId()); editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername()); editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
editor.putString(Helper.PREF_SOFTWARE, software); editor.putString(Helper.PREF_SOFTWARE, software);
editor.apply(); editor.apply();
if (userExists) if (userExists) {
new MastodonAccountDAO(activity, db).updateAccountCredential(account); new MastodonAccountDAO(activity, db).updateAccountCredential(account);
else { } else {
if (account.getUsername() != null && account.getCreatedAt() != null) if (account.getUsername() != null && account.getCreatedAt() != null) {
new MastodonAccountDAO(activity, db).insertAccount(account); new MastodonAccountDAO(activity, db).insertAccount(account);
} }
}
Handler mainHandler = new Handler(Looper.getMainLooper()); Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> { Runnable myRunnable = () -> {
Intent mainActivity = new Intent(activity, MainActivity.class); Intent mainActivity = new Intent(activity, MainActivity.class);

View File

@ -40,7 +40,7 @@ public class MastodonAccount {
}; };
@SerializedName("avatar") @SerializedName("avatar")
private String avatar; private String avatar;
@SerializedName("createdAt") @SerializedName("created_at")
private Date createdAt; private Date createdAt;
@SerializedName("note") @SerializedName("note")
private String description; private String description;
@ -54,7 +54,7 @@ public class MastodonAccount {
private String id; private String id;
@SerializedName("username") @SerializedName("username")
private String username; private String username;
@SerializedName("updatedAt") @SerializedName("updated_at")
private Date updatedAt; private Date updatedAt;
@SerializedName("url") @SerializedName("url")
private String url; private String url;

View File

@ -20,10 +20,7 @@ import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import app.fedilab.fedilabtube.client.entities.MastodonAccount.Account; import app.fedilab.fedilabtube.client.entities.MastodonAccount.Account;
import app.fedilab.fedilabtube.client.entities.Token; import app.fedilab.fedilabtube.client.entities.Token;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
@ -202,73 +199,7 @@ public class MastodonAccountDAO {
} }
/**
* Returns last used account
*
* @return Account
*/
public Account getLastUsedAccount() {
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " != 'null'", null, null, null, Sqlite.COL_UPDATED_AT + " DESC", "1");
return cursorToUser(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns all Account in db
*
* @return Account List<Account>
*/
public List<Account> getAllAccount() {
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
return cursorToListUser(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Returns an Account by token
*
* @param token String
* @return Account
*/
public Account getAccountByToken(String token) {
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " = \"" + token + "\"", null, null, null, null, "1");
return cursorToUser(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Returns an Account by id and instance
*
* @param id String
* @param instance String
* @return Account
*/
public Account getAccountByIdInstance(String id, String instance) {
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = \"" + id + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, null, null, null, "1");
return cursorToUser(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** /**
* Test if the current user is already stored in data base * Test if the current user is already stored in data base
@ -286,84 +217,5 @@ public class MastodonAccountDAO {
} }
/***
* Method to hydrate an Account from database
* @param c Cursor
* @return Account
*/
private Account cursorToUser(Cursor c) {
//No element found
if (c.getCount() == 0) {
c.close();
return null;
}
//Take the first element
c.moveToFirst();
//New user
Account account = new Account();
account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME)));
account.setDisplayName(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME)));
account.setFollowersCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT)));
account.setFollowingCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT)));
account.setDescription(c.getString(c.getColumnIndex(Sqlite.COL_NOTE)));
account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
account.setAvatar(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR)));
account.setUpdatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT))));
account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
account.setSoftware(c.getString(c.getColumnIndex(Sqlite.COL_SOFTWARE)));
account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID)));
account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET)));
account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN)));
//Close the cursor
c.close();
//User is returned
return account;
}
/***
* Method to hydrate an Accounts from database
* @param c Cursor
* @return List<Account>
*/
private List<Account> cursorToListUser(Cursor c) {
//No element found
if (c.getCount() == 0) {
c.close();
return null;
}
List<Account> accounts = new ArrayList<>();
while (c.moveToNext()) {
//New user
Account account = new Account();
account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME)));
account.setDisplayName(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME)));
account.setFollowersCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT)));
account.setFollowingCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT)));
account.setDescription(c.getString(c.getColumnIndex(Sqlite.COL_NOTE)));
account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
account.setAvatar(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR)));
account.setUpdatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT))));
account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
account.setSoftware(c.getString(c.getColumnIndex(Sqlite.COL_SOFTWARE)));
account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID)));
account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET)));
account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN)));
accounts.add(account);
}
//Close the cursor
c.close();
//Users list is returned
return accounts;
}
} }

View File

@ -26,6 +26,7 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import app.fedilab.fedilabtube.AccountActivity;
import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData; import app.fedilab.fedilabtube.client.data.AccountData;

View File

@ -16,6 +16,7 @@ package app.fedilab.fedilabtube.viewmodel;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;