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
-
99app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.kt
-
152app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.kt
-
62app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java
-
18app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
-
199app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
-
10app/src/main/java/awais/instagrabber/utils/CookieUtils.kt
-
50app/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 |
|||
public interface AccountDao { |
|||
|
|||
interface AccountDao { |
|||
@Query("SELECT * FROM accounts") |
|||
List<Account> getAllAccounts(); |
|||
suspend fun getAllAccounts(): List<Account> |
|||
|
|||
@Query("SELECT * FROM accounts WHERE uid = :uid") |
|||
Account findAccountByUid(String uid); |
|||
suspend fun findAccountByUid(uid: String): Account? |
|||
|
|||
@Insert(onConflict = OnConflictStrategy.REPLACE) |
|||
List<Long> insertAccounts(Account... accounts); |
|||
suspend fun insertAccounts(vararg accounts: Account): List<Long> |
|||
|
|||
@Update |
|||
void updateAccounts(Account... accounts); |
|||
suspend fun updateAccounts(vararg accounts: Account) |
|||
|
|||
@Delete |
|||
void deleteAccounts(Account... accounts); |
|||
suspend fun deleteAccounts(vararg accounts: Account) |
|||
|
|||
@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(); |
|||
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 |
|||
} |
|||
accountDao.insertAccounts(toUpdate) |
|||
} |
|||
|
|||
private static AccountDataSource INSTANCE; |
|||
suspend fun deleteAccount(account: Account) = accountDao.deleteAccounts(account) |
|||
|
|||
private final AccountDao accountDao; |
|||
suspend fun deleteAllAccounts() = accountDao.deleteAllAccounts() |
|||
|
|||
private AccountDataSource(final AccountDao accountDao) { |
|||
this.accountDao = accountDao; |
|||
} |
|||
companion object { |
|||
private lateinit var INSTANCE: AccountDataSource |
|||
|
|||
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()); |
|||
@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()) |
|||
} |
|||
} |
|||
} |
|||
return INSTANCE |
|||
} |
|||
return INSTANCE; |
|||
} |
|||
|
|||
@Nullable |
|||
public final Account getAccount(final String uid) { |
|||
return accountDao.findAccountByUid(uid); |
|||
} |
|||
|
|||
@NonNull |
|||
public final List<Account> getAllAccounts() { |
|||
return accountDao.getAllAccounts(); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
accountDao.insertAccounts(toUpdate); |
|||
} |
|||
|
|||
public final void deleteAccount(@NonNull final Account account) { |
|||
accountDao.deleteAccounts(account); |
|||
} |
|||
|
|||
public final void deleteAllAccounts() { |
|||
accountDao.deleteAllAccounts(); |
|||
} |
|||
} |
@ -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); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
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 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 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 deleteAccount(account: Account) = accountDataSource.deleteAccount(account) |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
} |
|||
suspend fun deleteAllAccounts() = accountDataSource.deleteAllAccounts() |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
} |
|||
companion object { |
|||
private lateinit var instance: AccountRepository |
|||
|
|||
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); |
|||
}); |
|||
}); |
|||
@JvmStatic |
|||
fun getInstance(accountDataSource: AccountDataSource): AccountRepository { |
|||
if (!this::instance.isInitialized) { |
|||
instance = AccountRepository(accountDataSource) |
|||
} |
|||
return instance |
|||
} |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue