mirror of
				https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
				synced 2025-10-31 00:48:09 +01:00 
			
		
		
		
	Switch between all logged-in reddit accounts is available.
This commit is contained in:
		| @@ -35,6 +35,9 @@ public interface AccountDao { | |||||||
|             "karma = :karma WHERE username = :username") |             "karma = :karma WHERE username = :username") | ||||||
|     void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma); |     void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma); | ||||||
|  |  | ||||||
|     @Query("SELECT * FROM accounts WHERE username != :username") |     @Query("SELECT * FROM accounts WHERE is_current_user = 0 ORDER BY username COLLATE NOCASE ASC") | ||||||
|     LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData(String username); |     LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData(); | ||||||
|  |  | ||||||
|  |     @Query("UPDATE accounts SET is_current_user = 1 WHERE username = :username") | ||||||
|  |     void markAccountCurrent(String username); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ public class AccountRepository { | |||||||
|     AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { |     AccountRepository(RedditDataRoomDatabase redditDataRoomDatabase, String username) { | ||||||
|         mAccountDao = redditDataRoomDatabase.accountDao(); |         mAccountDao = redditDataRoomDatabase.accountDao(); | ||||||
|         mAccountLiveData = mAccountDao.getAccountLiveData(username); |         mAccountLiveData = mAccountDao.getAccountLiveData(username); | ||||||
|         mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData(username); |         mAccountsExceptCurrentAccountLiveData = mAccountDao.getAccountsExceptCurrentAccountLiveData(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     LiveData<Account> getAccountLiveData() { |     LiveData<Account> getAccountLiveData() { | ||||||
|   | |||||||
| @@ -191,7 +191,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe | |||||||
|                 new AccountRecyclerViewAdapter.ItemSelectedListener() { |                 new AccountRecyclerViewAdapter.ItemSelectedListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void accountSelected(Account account) { |             public void accountSelected(Account account) { | ||||||
|  |                 new SwitchAccountAsyncTask(mRedditDataRoomDatabase, account.getUsername(), () -> relaunchActivity()).execute(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             @Override |             @Override | ||||||
| @@ -349,13 +349,17 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     private void relaunchActivity() { | ||||||
|  |         Intent intent = getIntent(); | ||||||
|  |         finish(); | ||||||
|  |         startActivity(intent); | ||||||
|  |         overridePendingTransition(0, 0); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     protected void onActivityResult(int requestCode, int resultCode, Intent data) { |     protected void onActivityResult(int requestCode, int resultCode, Intent data) { | ||||||
|         if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) { |         if(requestCode == LOGIN_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK) { | ||||||
|             Intent intent = getIntent(); |             relaunchActivity(); | ||||||
|             finish(); |  | ||||||
|             startActivity(intent); |  | ||||||
|             overridePendingTransition(0, 0); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         super.onActivityResult(requestCode, resultCode, data); |         super.onActivityResult(requestCode, resultCode, data); | ||||||
|   | |||||||
| @@ -0,0 +1,32 @@ | |||||||
|  | package ml.docilealligator.infinityforreddit; | ||||||
|  |  | ||||||
|  | import android.os.AsyncTask; | ||||||
|  |  | ||||||
|  | class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> { | ||||||
|  |     interface SwitchAccountAsyncTaskListener { | ||||||
|  |         void switched(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private RedditDataRoomDatabase redditDataRoomDatabase; | ||||||
|  |     private String newAccountName; | ||||||
|  |     private SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener; | ||||||
|  |  | ||||||
|  |     SwitchAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String newAccountName, | ||||||
|  |                            SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener) { | ||||||
|  |         this.redditDataRoomDatabase = redditDataRoomDatabase; | ||||||
|  |         this.newAccountName = newAccountName; | ||||||
|  |         this.switchAccountAsyncTaskListener = switchAccountAsyncTaskListener; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     protected Void doInBackground(Void... voids) { | ||||||
|  |         redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent(); | ||||||
|  |         redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName); | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     protected void onPostExecute(Void aVoid) { | ||||||
|  |         switchAccountAsyncTaskListener.switched(); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user