Browse Source
Convert AccountDao, AccountDataSource and AccountRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Convert AccountDao, AccountDataSource and AccountRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
9 changed files with 252 additions and 374 deletions
-
1app/build.gradle
-
29app/src/main/java/awais/instagrabber/db/dao/AccountDao.kt
-
97app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt
-
150app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt
-
54app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java
-
18app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
-
105app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
-
10app/src/main/java/awais/instagrabber/utils/CookieUtils.kt
-
20app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java
@ -1,34 +1,25 @@ |
|||||
package awais.instagrabber.db.dao; |
|
||||
|
package awais.instagrabber.db.dao |
||||
|
|
||||
import androidx.room.Dao; |
|
||||
import androidx.room.Delete; |
|
||||
import androidx.room.Insert; |
|
||||
import androidx.room.OnConflictStrategy; |
|
||||
import androidx.room.Query; |
|
||||
import androidx.room.Update; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import awais.instagrabber.db.entities.Account; |
|
||||
|
import androidx.room.* |
||||
|
import awais.instagrabber.db.entities.Account |
||||
|
|
||||
@Dao |
@Dao |
||||
public interface AccountDao { |
|
||||
|
|
||||
|
interface AccountDao { |
||||
@Query("SELECT * FROM accounts") |
@Query("SELECT * FROM accounts") |
||||
List<Account> getAllAccounts(); |
|
||||
|
suspend fun getAllAccounts(): List<Account> |
||||
|
|
||||
@Query("SELECT * FROM accounts WHERE uid = :uid") |
@Query("SELECT * FROM accounts WHERE uid = :uid") |
||||
Account findAccountByUid(String uid); |
|
||||
|
suspend fun findAccountByUid(uid: String): Account? |
||||
|
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE) |
@Insert(onConflict = OnConflictStrategy.REPLACE) |
||||
List<Long> insertAccounts(Account... accounts); |
|
||||
|
suspend fun insertAccounts(vararg accounts: Account): List<Long> |
||||
|
|
||||
@Update |
@Update |
||||
void updateAccounts(Account... accounts); |
|
||||
|
suspend fun updateAccounts(vararg accounts: Account) |
||||
|
|
||||
@Delete |
@Delete |
||||
void deleteAccounts(Account... accounts); |
|
||||
|
suspend fun deleteAccounts(vararg accounts: Account) |
||||
|
|
||||
@Query("DELETE from accounts") |
@Query("DELETE from accounts") |
||||
void deleteAllAccounts(); |
|
||||
|
suspend fun deleteAllAccounts() |
||||
} |
} |
@ -1,68 +1,49 @@ |
|||||
package awais.instagrabber.db.datasources; |
|
||||
|
|
||||
import android.content.Context; |
|
||||
|
|
||||
import androidx.annotation.NonNull; |
|
||||
import androidx.annotation.Nullable; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import awais.instagrabber.db.AppDatabase; |
|
||||
import awais.instagrabber.db.dao.AccountDao; |
|
||||
import awais.instagrabber.db.entities.Account; |
|
||||
|
|
||||
public class AccountDataSource { |
|
||||
private static final String TAG = AccountDataSource.class.getSimpleName(); |
|
||||
|
|
||||
private static AccountDataSource INSTANCE; |
|
||||
|
|
||||
private final AccountDao accountDao; |
|
||||
|
|
||||
private AccountDataSource(final AccountDao accountDao) { |
|
||||
this.accountDao = accountDao; |
|
||||
} |
|
||||
|
|
||||
public static AccountDataSource getInstance(@NonNull Context context) { |
|
||||
if (INSTANCE == null) { |
|
||||
synchronized (AccountDataSource.class) { |
|
||||
if (INSTANCE == null) { |
|
||||
final AppDatabase database = AppDatabase.getDatabase(context); |
|
||||
INSTANCE = new AccountDataSource(database.accountDao()); |
|
||||
} |
|
||||
} |
|
||||
|
package awais.instagrabber.db.datasources |
||||
|
|
||||
|
import android.content.Context |
||||
|
import awais.instagrabber.db.AppDatabase |
||||
|
import awais.instagrabber.db.dao.AccountDao |
||||
|
import awais.instagrabber.db.entities.Account |
||||
|
|
||||
|
class AccountDataSource private constructor(private val accountDao: AccountDao) { |
||||
|
suspend fun getAccount(uid: String): Account? = accountDao.findAccountByUid(uid) |
||||
|
|
||||
|
suspend fun getAllAccounts(): List<Account> = accountDao.getAllAccounts() |
||||
|
|
||||
|
suspend fun insertOrUpdateAccount( |
||||
|
uid: String, |
||||
|
username: String, |
||||
|
cookie: String, |
||||
|
fullName: String, |
||||
|
profilePicUrl: String?, |
||||
|
) { |
||||
|
val account = getAccount(uid) |
||||
|
val toUpdate = Account(account?.id ?: 0, uid, username, cookie, fullName, profilePicUrl) |
||||
|
if (account != null) { |
||||
|
accountDao.updateAccounts(toUpdate) |
||||
|
return |
||||
} |
} |
||||
return INSTANCE; |
|
||||
|
accountDao.insertAccounts(toUpdate) |
||||
} |
} |
||||
|
|
||||
@Nullable |
|
||||
public final Account getAccount(final String uid) { |
|
||||
return accountDao.findAccountByUid(uid); |
|
||||
} |
|
||||
|
suspend fun deleteAccount(account: Account) = accountDao.deleteAccounts(account) |
||||
|
|
||||
@NonNull |
|
||||
public final List<Account> getAllAccounts() { |
|
||||
return accountDao.getAllAccounts(); |
|
||||
} |
|
||||
|
suspend fun deleteAllAccounts() = accountDao.deleteAllAccounts() |
||||
|
|
||||
public final void insertOrUpdateAccount(final String uid, |
|
||||
final String username, |
|
||||
final String cookie, |
|
||||
final String fullName, |
|
||||
final String profilePicUrl) { |
|
||||
final Account account = getAccount(uid); |
|
||||
final Account toUpdate = new Account(account == null ? 0 : account.getId(), uid, username, cookie, fullName, profilePicUrl); |
|
||||
if (account != null) { |
|
||||
accountDao.updateAccounts(toUpdate); |
|
||||
return; |
|
||||
|
companion object { |
||||
|
private lateinit var INSTANCE: AccountDataSource |
||||
|
|
||||
|
@JvmStatic |
||||
|
fun getInstance(context: Context): AccountDataSource { |
||||
|
if (!this::INSTANCE.isInitialized) { |
||||
|
synchronized(AccountDataSource::class.java) { |
||||
|
if (!this::INSTANCE.isInitialized) { |
||||
|
val database = AppDatabase.getDatabase(context) |
||||
|
INSTANCE = AccountDataSource(database.accountDao()) |
||||
} |
} |
||||
accountDao.insertAccounts(toUpdate); |
|
||||
} |
} |
||||
|
|
||||
public final void deleteAccount(@NonNull final Account account) { |
|
||||
accountDao.deleteAccounts(account); |
|
||||
} |
} |
||||
|
|
||||
public final void deleteAllAccounts() { |
|
||||
accountDao.deleteAllAccounts(); |
|
||||
|
return INSTANCE |
||||
|
} |
||||
} |
} |
||||
} |
} |
@ -1,131 +1,49 @@ |
|||||
package awais.instagrabber.db.repositories; |
|
||||
|
package awais.instagrabber.db.repositories |
||||
|
|
||||
import java.util.List; |
|
||||
|
import awais.instagrabber.db.datasources.AccountDataSource |
||||
|
import awais.instagrabber.db.entities.Account |
||||
|
|
||||
import awais.instagrabber.db.datasources.AccountDataSource; |
|
||||
import awais.instagrabber.db.entities.Account; |
|
||||
import awais.instagrabber.utils.AppExecutors; |
|
||||
|
class AccountRepository private constructor(private val accountDataSource: AccountDataSource) { |
||||
|
suspend fun getAccount(uid: Long): Account? = accountDataSource.getAccount(uid.toString()) |
||||
|
|
||||
public class AccountRepository { |
|
||||
private static final String TAG = AccountRepository.class.getSimpleName(); |
|
||||
|
suspend fun getAllAccounts(): List<Account> = accountDataSource.getAllAccounts() |
||||
|
|
||||
private static AccountRepository instance; |
|
||||
|
|
||||
private final AppExecutors appExecutors; |
|
||||
private final AccountDataSource accountDataSource; |
|
||||
|
|
||||
// private List<Account> cachedAccounts; |
|
||||
|
|
||||
private AccountRepository(final AppExecutors appExecutors, final AccountDataSource accountDataSource) { |
|
||||
this.appExecutors = appExecutors; |
|
||||
this.accountDataSource = accountDataSource; |
|
||||
} |
|
||||
|
|
||||
public static AccountRepository getInstance(final AccountDataSource accountDataSource) { |
|
||||
if (instance == null) { |
|
||||
instance = new AccountRepository(AppExecutors.INSTANCE, accountDataSource); |
|
||||
|
suspend fun insertOrUpdateAccounts(accounts: List<Account>) { |
||||
|
for (account in accounts) { |
||||
|
accountDataSource.insertOrUpdateAccount( |
||||
|
account.uid, |
||||
|
account.username, |
||||
|
account.cookie, |
||||
|
account.fullName, |
||||
|
account.profilePic |
||||
|
) |
||||
} |
} |
||||
return instance; |
|
||||
} |
} |
||||
|
|
||||
public void getAccount(final long uid, |
|
||||
final RepositoryCallback<Account> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
final Account account = accountDataSource.getAccount(String.valueOf(uid)); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
if (account == null) { |
|
||||
callback.onDataNotAvailable(); |
|
||||
return; |
|
||||
} |
|
||||
callback.onSuccess(account); |
|
||||
}); |
|
||||
}); |
|
||||
|
suspend fun insertOrUpdateAccount( |
||||
|
uid: Long, |
||||
|
username: String, |
||||
|
cookie: String, |
||||
|
fullName: String, |
||||
|
profilePicUrl: String?, |
||||
|
): Account? { |
||||
|
accountDataSource.insertOrUpdateAccount(uid.toString(), username, cookie, fullName, profilePicUrl) |
||||
|
return accountDataSource.getAccount(uid.toString()) |
||||
} |
} |
||||
|
|
||||
public void getAllAccounts(final RepositoryCallback<List<Account>> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
final List<Account> accounts = accountDataSource.getAllAccounts(); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
if (accounts == null) { |
|
||||
callback.onDataNotAvailable(); |
|
||||
return; |
|
||||
} |
|
||||
// cachedAccounts = accounts; |
|
||||
callback.onSuccess(accounts); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
suspend fun deleteAccount(account: Account) = accountDataSource.deleteAccount(account) |
||||
|
|
||||
public void insertOrUpdateAccounts(final List<Account> accounts, |
|
||||
final RepositoryCallback<Void> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
for (final Account account : accounts) { |
|
||||
accountDataSource.insertOrUpdateAccount(account.getUid(), |
|
||||
account.getUsername(), |
|
||||
account.getCookie(), |
|
||||
account.getFullName(), |
|
||||
account.getProfilePic()); |
|
||||
} |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
callback.onSuccess(null); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
suspend fun deleteAllAccounts() = accountDataSource.deleteAllAccounts() |
||||
|
|
||||
public void insertOrUpdateAccount(final long uid, |
|
||||
final String username, |
|
||||
final String cookie, |
|
||||
final String fullName, |
|
||||
final String profilePicUrl, |
|
||||
final RepositoryCallback<Account> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
accountDataSource.insertOrUpdateAccount(String.valueOf(uid), username, cookie, fullName, profilePicUrl); |
|
||||
final Account updated = accountDataSource.getAccount(String.valueOf(uid)); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
if (updated == null) { |
|
||||
callback.onDataNotAvailable(); |
|
||||
return; |
|
||||
} |
|
||||
callback.onSuccess(updated); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
companion object { |
||||
|
private lateinit var instance: AccountRepository |
||||
|
|
||||
public void deleteAccount(final Account account, |
|
||||
final RepositoryCallback<Void> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
accountDataSource.deleteAccount(account); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
callback.onSuccess(null); |
|
||||
}); |
|
||||
}); |
|
||||
|
@JvmStatic |
||||
|
fun getInstance(accountDataSource: AccountDataSource): AccountRepository { |
||||
|
if (!this::instance.isInitialized) { |
||||
|
instance = AccountRepository(accountDataSource) |
||||
|
} |
||||
|
return instance |
||||
} |
} |
||||
|
|
||||
public void deleteAllAccounts(final RepositoryCallback<Void> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
accountDataSource.deleteAllAccounts(); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
callback.onSuccess(null); |
|
||||
}); |
|
||||
}); |
|
||||
} |
} |
||||
|
|
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue