1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-04-26 16:08:44 +02:00

Some improvements

This commit is contained in:
Thomas 2020-10-02 19:29:00 +02:00
parent d62ff6d18f
commit c2ab7a3608
10 changed files with 87 additions and 64 deletions

View File

@ -83,7 +83,6 @@ public class AccountActivity extends AppCompatActivity {
TextView instanceView = findViewById(R.id.instance); TextView instanceView = findViewById(R.id.instance);
Account account = new AccountDAO(AccountActivity.this, db).getAccountByToken(token); Account account = new AccountDAO(AccountActivity.this, db).getAccountByToken(token);
if (account == null) { if (account == null) {
Helper.logoutCurrentUser(AccountActivity.this, null); Helper.logoutCurrentUser(AccountActivity.this, null);
return; return;
@ -102,12 +101,11 @@ public class AccountActivity extends AppCompatActivity {
instanceView.setText(account.getHost()); instanceView.setText(account.getHost());
Button logout_button = findViewById(R.id.logout_button); Button logout_button = findViewById(R.id.logout_button);
Account finalAccount = account;
logout_button.setOnClickListener(v -> { logout_button.setOnClickListener(v -> {
AlertDialog.Builder dialogBuilderLogoutAccount = new AlertDialog.Builder(AccountActivity.this); AlertDialog.Builder dialogBuilderLogoutAccount = new AlertDialog.Builder(AccountActivity.this);
dialogBuilderLogoutAccount.setMessage(getString(R.string.logout_account_confirmation, finalAccount.getUsername(), finalAccount.getHost())); dialogBuilderLogoutAccount.setMessage(getString(R.string.logout_account_confirmation, account.getUsername(), account.getHost()));
dialogBuilderLogoutAccount.setPositiveButton(R.string.action_logout, (dialog, id) -> { dialogBuilderLogoutAccount.setPositiveButton(R.string.action_logout, (dialog, id) -> {
Helper.logoutCurrentUser(AccountActivity.this, finalAccount); Helper.logoutCurrentUser(AccountActivity.this, account);
dialog.dismiss(); dialog.dismiss();
}); });
dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
@ -217,6 +215,7 @@ public class AccountActivity extends AppCompatActivity {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(AccountActivity.this); AlertDialog.Builder builderSingle = new AlertDialog.Builder(AccountActivity.this);
builderSingle.setTitle(getString(R.string.list_of_accounts)); builderSingle.setTitle(getString(R.string.list_of_accounts));
if( accounts != null) {
final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(AccountActivity.this, accounts); final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(AccountActivity.this, accounts);
final Account[] accountArray = new Account[accounts.size()]; final Account[] accountArray = new Account[accounts.size()];
int i = 0; int i = 0;
@ -224,12 +223,6 @@ public class AccountActivity extends AppCompatActivity {
accountArray[i] = account; accountArray[i] = account;
i++; i++;
} }
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setPositiveButton(R.string.add_account, (dialog, which) -> {
Intent intent = new Intent(AccountActivity.this, LoginActivity.class);
startActivity(intent);
finish();
});
builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> { builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> {
final Account account = accountArray[which]; final Account account = accountArray[which];
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
@ -244,6 +237,14 @@ public class AccountActivity extends AppCompatActivity {
startActivity(intent); startActivity(intent);
finish(); finish();
}); });
}
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setPositiveButton(R.string.add_account, (dialog, which) -> {
Intent intent = new Intent(AccountActivity.this, LoginActivity.class);
startActivity(intent);
finish();
});
builderSingle.show(); builderSingle.show();
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
@ -258,7 +259,7 @@ public class AccountActivity extends AppCompatActivity {
/** /**
* Pager adapter for three tabs (notifications, muted, blocked) * Pager adapter for three tabs (notifications, muted, blocked)
*/ */
private class AccountsPagerAdapter extends FragmentStatePagerAdapter { private static class AccountsPagerAdapter extends FragmentStatePagerAdapter {
AccountsPagerAdapter(FragmentManager fm) { AccountsPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

View File

@ -92,14 +92,12 @@ public class MainActivity extends AppCompatActivity {
try { try {
Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams); Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
if( token == null) { if( token == null) {
runOnUiThread(() -> { runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, account));
Helper.logoutCurrentUser(MainActivity.this, account);
});
return; return;
} }
UserMe userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials(); UserMe userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
if( userMe != null && userMe.getAccount() != null) { if( userMe != null && userMe.getAccount() != null) {
new AccountDAO(MainActivity.this.getApplicationContext(), db).updateAccount(userMe.getAccount()); new AccountDAO(MainActivity.this, db).updateAccount(userMe.getAccount());
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());

View File

@ -109,6 +109,7 @@ public class PeertubeEditUploadActivity extends AppCompatActivity {
} }
if (videoId == null) { if (videoId == null) {
videoId = sharedpreferences.getString(Helper.VIDEO_ID, null); videoId = sharedpreferences.getString(Helper.VIDEO_ID, null);
sharedpreferences.edit().remove(Helper.VIDEO_ID).apply();
} }
if (getSupportActionBar() != null) if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);

View File

@ -79,7 +79,7 @@ public class OwnAccountsAdapter extends ArrayAdapter<Account> {
holder.account_un.setText(String.format("@%s", account.getAcct())); holder.account_un.setText(String.format("@%s", account.getAcct()));
//Profile picture //Profile picture
Helper.loadGiF(holder.account_pp.getContext(), account.getAvatar().getPath(), holder.account_pp); Helper.loadGiF(holder.account_pp.getContext(), "https://" + account.getHost() + account.getAvatar().getPath(), holder.account_pp);
return convertView; return convertView;
} }

View File

@ -139,7 +139,6 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
= new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
lv_accounts.setLayoutManager(layoutManager); lv_accounts.setLayoutManager(layoutManager);
lv_accounts.setAdapter(accountsHorizontalListAdapter); lv_accounts.setAdapter(accountsHorizontalListAdapter);
if (!Helper.isTablet(context)) { if (!Helper.isTablet(context)) {
mLayoutManager = new LinearLayoutManager(context); mLayoutManager = new LinearLayoutManager(context);
lv_status.setLayoutManager(mLayoutManager); lv_status.setLayoutManager(mLayoutManager);
@ -157,10 +156,10 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() { lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if (dy > 0) { if (dy > 0) {
int visibleItemCount = mLayoutManager.getChildCount(); int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount(); int totalItemCount = layoutManager.getItemCount();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) { if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
viewModelAccounts.getAccounts(RetrofitPeertubeAPI.DataType.SUBSCRIBER, max_id_accounts).observe(DisplayVideosFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); viewModelAccounts.getAccounts(RetrofitPeertubeAPI.DataType.SUBSCRIBER, max_id_accounts).observe(DisplayVideosFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse));
} }

View File

@ -382,9 +382,13 @@ public class Helper {
return format; return format;
} else if (months > 0 || days > 7) { } else if (months > 0 || days > 7) {
//Removes the year depending of the locale from DateFormat.SHORT format //Removes the year depending of the locale from DateFormat.SHORT format
try {
SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()); SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
df.applyPattern(df.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", "")); df.applyPattern(df.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
return df.format(dateToot); return df.format(dateToot);
}catch (Exception e) {
return format;
}
} else if (days > 0) } else if (days > 0)
return context.getString(R.string.date_day, days); return context.getString(R.string.date_day, days);
else if (hours > 0) else if (hours > 0)

View File

@ -63,7 +63,7 @@ public class PeertubeUploadReceiver extends UploadServiceBroadcastReceiver {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.VIDEO_ID, videoID); editor.putString(Helper.VIDEO_ID, videoID);
editor.commit(); editor.apply();
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -226,6 +226,7 @@ public class AccountDAO {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null); Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
return cursorToListUser(c); return cursorToListUser(c);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
return null; return null;
} }
} }
@ -243,6 +244,7 @@ public class AccountDAO {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " = \"" + token + "\"", null, null, null, null, "1"); Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " = \"" + token + "\"", null, null, null, null, "1");
return cursorToUser(c); return cursorToUser(c);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
return null; return null;
} }
} }
@ -264,29 +266,6 @@ public class AccountDAO {
} }
/***
* 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()) {
Account account = cursorToUser(c);
accounts.add(account);
}
//Close the cursor
c.close();
//Users list is returned
return accounts;
}
/*** /***
* Method to hydrate an Account from database * Method to hydrate an Account from database
* @param c Cursor * @param c Cursor
@ -294,6 +273,7 @@ public class AccountDAO {
*/ */
private Account cursorToUser(Cursor c) { private Account cursorToUser(Cursor c) {
//No element found //No element found
if (c.getCount() == 0) { if (c.getCount() == 0) {
c.close(); c.close();
return null; return null;
@ -327,4 +307,46 @@ public class AccountDAO {
} }
/***
* 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)));
Avatar avatar = new Avatar();
avatar.setPath(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR)));
account.setAvatar(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.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

@ -58,7 +58,6 @@ public class ChannelsVM extends AndroidViewModel {
SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
String instance = Helper.getLiveInstance(_mContext);
AccountData.Account account = new AccountDAO(_mContext, db).getAccountByToken(token); AccountData.Account account = new AccountDAO(_mContext, db).getAccountByToken(token);
finalElement = account.getUsername() + "@" + account.getHost(); finalElement = account.getUsername() + "@" + account.getHost();
} }

View File

@ -74,7 +74,6 @@ public class PlaylistsVM extends AndroidViewModel {
try { try {
SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
String instance = Helper.getLiveInstance(_mContext);
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(_mContext, db).getAccountByToken(token); Account account = new AccountDAO(_mContext, db).getAccountByToken(token);
int statusCode = -1; int statusCode = -1;