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);
Account account = new AccountDAO(AccountActivity.this, db).getAccountByToken(token);
if (account == null) {
Helper.logoutCurrentUser(AccountActivity.this, null);
return;
@ -102,12 +101,11 @@ public class AccountActivity extends AppCompatActivity {
instanceView.setText(account.getHost());
Button logout_button = findViewById(R.id.logout_button);
Account finalAccount = account;
logout_button.setOnClickListener(v -> {
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) -> {
Helper.logoutCurrentUser(AccountActivity.this, finalAccount);
Helper.logoutCurrentUser(AccountActivity.this, account);
dialog.dismiss();
});
dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
@ -217,12 +215,28 @@ public class AccountActivity extends AppCompatActivity {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(AccountActivity.this);
builderSingle.setTitle(getString(R.string.list_of_accounts));
final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(AccountActivity.this, accounts);
final Account[] accountArray = new Account[accounts.size()];
int i = 0;
for (Account account : accounts) {
accountArray[i] = account;
i++;
if( accounts != null) {
final OwnAccountsAdapter accountsListAdapter = new OwnAccountsAdapter(AccountActivity.this, accounts);
final Account[] accountArray = new Account[accounts.size()];
int i = 0;
for (Account account : accounts) {
accountArray[i] = account;
i++;
}
builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> {
final Account account = accountArray[which];
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_INSTANCE, account.getHost());
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.apply();
dialog.dismiss();
Intent intent = new Intent(AccountActivity.this, MainActivity.class);
startActivity(intent);
finish();
});
}
builderSingle.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
builderSingle.setPositiveButton(R.string.add_account, (dialog, which) -> {
@ -230,20 +244,7 @@ public class AccountActivity extends AppCompatActivity {
startActivity(intent);
finish();
});
builderSingle.setAdapter(accountsListAdapter, (dialog, which) -> {
final Account account = accountArray[which];
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_INSTANCE, account.getHost());
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.apply();
dialog.dismiss();
Intent intent = new Intent(AccountActivity.this, MainActivity.class);
startActivity(intent);
finish();
});
builderSingle.show();
}
return super.onOptionsItemSelected(item);
@ -258,7 +259,7 @@ public class AccountActivity extends AppCompatActivity {
/**
* Pager adapter for three tabs (notifications, muted, blocked)
*/
private class AccountsPagerAdapter extends FragmentStatePagerAdapter {
private static class AccountsPagerAdapter extends FragmentStatePagerAdapter {
AccountsPagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

View File

@ -92,14 +92,12 @@ public class MainActivity extends AppCompatActivity {
try {
Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
if( token == null) {
runOnUiThread(() -> {
Helper.logoutCurrentUser(MainActivity.this, account);
});
runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, account));
return;
}
UserMe userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
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();
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());

View File

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

View File

@ -79,7 +79,7 @@ public class OwnAccountsAdapter extends ArrayAdapter<Account> {
holder.account_un.setText(String.format("@%s", account.getAcct()));
//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;
}

View File

@ -139,7 +139,6 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
= new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
lv_accounts.setLayoutManager(layoutManager);
lv_accounts.setAdapter(accountsHorizontalListAdapter);
if (!Helper.isTablet(context)) {
mLayoutManager = new LinearLayoutManager(context);
lv_status.setLayoutManager(mLayoutManager);
@ -157,10 +156,10 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if (dy > 0) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
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;
} else if (months > 0 || days > 7) {
//Removes the year depending of the locale from DateFormat.SHORT format
SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
df.applyPattern(df.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
return df.format(dateToot);
try {
SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
df.applyPattern(df.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
return df.format(dateToot);
}catch (Exception e) {
return format;
}
} else if (days > 0)
return context.getString(R.string.date_day, days);
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.Editor editor = sharedpreferences.edit();
editor.putString(Helper.VIDEO_ID, videoID);
editor.commit();
editor.apply();
}
} catch (JSONException e) {
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);
return cursorToListUser(c);
} catch (Exception e) {
e.printStackTrace();
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");
return cursorToUser(c);
} catch (Exception e) {
e.printStackTrace();
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
* @param c Cursor
@ -294,6 +273,7 @@ public class AccountDAO {
*/
private Account cursorToUser(Cursor c) {
//No element found
if (c.getCount() == 0) {
c.close();
return null;
@ -320,11 +300,53 @@ public class AccountDAO {
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();
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)));
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);
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 instance = Helper.getLiveInstance(_mContext);
AccountData.Account account = new AccountDAO(_mContext, db).getAccountByToken(token);
finalElement = account.getUsername() + "@" + account.getHost();
}

View File

@ -74,7 +74,6 @@ public class PlaylistsVM extends AndroidViewModel {
try {
SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
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();
Account account = new AccountDAO(_mContext, db).getAccountByToken(token);
int statusCode = -1;