Browse Source
Convert FavoriteDao, FavoriteDataSource and FavoriteRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Convert FavoriteDao, FavoriteDataSource and FavoriteRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
13 changed files with 385 additions and 453 deletions
-
2app/src/main/java/awais/instagrabber/db/dao/AccountDao.kt
-
35app/src/main/java/awais/instagrabber/db/dao/FavoriteDao.java
-
26app/src/main/java/awais/instagrabber/db/dao/FavoriteDao.kt
-
62app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.java
-
44app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.kt
-
88app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.java
-
28app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.kt
-
135app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java
-
142app/src/main/java/awais/instagrabber/fragments/LocationFragment.java
-
135app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
-
77app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java
-
40app/src/main/java/awais/instagrabber/viewmodels/FavoritesViewModel.kt
-
24app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java
@ -1,35 +0,0 @@ |
|||||
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.Favorite; |
|
||||
import awais.instagrabber.models.enums.FavoriteType; |
|
||||
|
|
||||
@Dao |
|
||||
public interface FavoriteDao { |
|
||||
|
|
||||
@Query("SELECT * FROM favorites") |
|
||||
List<Favorite> getAllFavorites(); |
|
||||
|
|
||||
@Query("SELECT * FROM favorites WHERE query_text = :query and type = :type") |
|
||||
Favorite findFavoriteByQueryAndType(String query, FavoriteType type); |
|
||||
|
|
||||
@Insert |
|
||||
List<Long> insertFavorites(Favorite... favorites); |
|
||||
|
|
||||
@Update |
|
||||
void updateFavorites(Favorite... favorites); |
|
||||
|
|
||||
@Delete |
|
||||
void deleteFavorites(Favorite... favorites); |
|
||||
|
|
||||
@Query("DELETE from favorites") |
|
||||
void deleteAllFavorites(); |
|
||||
} |
|
@ -0,0 +1,26 @@ |
|||||
|
package awais.instagrabber.db.dao |
||||
|
|
||||
|
import androidx.room.* |
||||
|
import awais.instagrabber.db.entities.Favorite |
||||
|
import awais.instagrabber.models.enums.FavoriteType |
||||
|
|
||||
|
@Dao |
||||
|
interface FavoriteDao { |
||||
|
@Query("SELECT * FROM favorites") |
||||
|
suspend fun getAllFavorites(): List<Favorite> |
||||
|
|
||||
|
@Query("SELECT * FROM favorites WHERE query_text = :query and type = :type") |
||||
|
suspend fun findFavoriteByQueryAndType(query: String, type: FavoriteType): Favorite? |
||||
|
|
||||
|
@Insert |
||||
|
suspend fun insertFavorites(vararg favorites: Favorite) |
||||
|
|
||||
|
@Update |
||||
|
suspend fun updateFavorites(vararg favorites: Favorite) |
||||
|
|
||||
|
@Delete |
||||
|
suspend fun deleteFavorites(vararg favorites: Favorite) |
||||
|
|
||||
|
@Query("DELETE from favorites") |
||||
|
suspend fun deleteAllFavorites() |
||||
|
} |
@ -1,62 +0,0 @@ |
|||||
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.FavoriteDao; |
|
||||
import awais.instagrabber.db.entities.Favorite; |
|
||||
import awais.instagrabber.models.enums.FavoriteType; |
|
||||
|
|
||||
public class FavoriteDataSource { |
|
||||
private static final String TAG = FavoriteDataSource.class.getSimpleName(); |
|
||||
|
|
||||
private static FavoriteDataSource INSTANCE; |
|
||||
|
|
||||
private final FavoriteDao favoriteDao; |
|
||||
|
|
||||
private FavoriteDataSource(final FavoriteDao favoriteDao) { |
|
||||
this.favoriteDao = favoriteDao; |
|
||||
} |
|
||||
|
|
||||
public static synchronized FavoriteDataSource getInstance(@NonNull Context context) { |
|
||||
if (INSTANCE == null) { |
|
||||
synchronized (FavoriteDataSource.class) { |
|
||||
if (INSTANCE == null) { |
|
||||
final AppDatabase database = AppDatabase.getDatabase(context); |
|
||||
INSTANCE = new FavoriteDataSource(database.favoriteDao()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return INSTANCE; |
|
||||
} |
|
||||
|
|
||||
|
|
||||
@Nullable |
|
||||
public final Favorite getFavorite(@NonNull final String query, @NonNull final FavoriteType type) { |
|
||||
return favoriteDao.findFavoriteByQueryAndType(query, type); |
|
||||
} |
|
||||
|
|
||||
@NonNull |
|
||||
public final List<Favorite> getAllFavorites() { |
|
||||
return favoriteDao.getAllFavorites(); |
|
||||
} |
|
||||
|
|
||||
public final void insertOrUpdateFavorite(@NonNull final Favorite favorite) { |
|
||||
if (favorite.getId() != 0) { |
|
||||
favoriteDao.updateFavorites(favorite); |
|
||||
return; |
|
||||
} |
|
||||
favoriteDao.insertFavorites(favorite); |
|
||||
} |
|
||||
|
|
||||
public final void deleteFavorite(@NonNull final String query, @NonNull final FavoriteType type) { |
|
||||
final Favorite favorite = getFavorite(query, type); |
|
||||
if (favorite == null) return; |
|
||||
favoriteDao.deleteFavorites(favorite); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,44 @@ |
|||||
|
package awais.instagrabber.db.datasources |
||||
|
|
||||
|
import android.content.Context |
||||
|
import awais.instagrabber.db.AppDatabase |
||||
|
import awais.instagrabber.db.dao.FavoriteDao |
||||
|
import awais.instagrabber.db.entities.Favorite |
||||
|
import awais.instagrabber.models.enums.FavoriteType |
||||
|
|
||||
|
class FavoriteDataSource private constructor(private val favoriteDao: FavoriteDao) { |
||||
|
suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDao.findFavoriteByQueryAndType(query, type) |
||||
|
|
||||
|
suspend fun getAllFavorites(): List<Favorite> = favoriteDao.getAllFavorites() |
||||
|
|
||||
|
suspend fun insertOrUpdateFavorite(favorite: Favorite) { |
||||
|
if (favorite.id != 0) { |
||||
|
favoriteDao.updateFavorites(favorite) |
||||
|
return |
||||
|
} |
||||
|
favoriteDao.insertFavorites(favorite) |
||||
|
} |
||||
|
|
||||
|
suspend fun deleteFavorite(query: String, type: FavoriteType) { |
||||
|
val favorite = getFavorite(query, type) ?: return |
||||
|
favoriteDao.deleteFavorites(favorite) |
||||
|
} |
||||
|
|
||||
|
companion object { |
||||
|
private lateinit var INSTANCE: FavoriteDataSource |
||||
|
|
||||
|
@JvmStatic |
||||
|
@Synchronized |
||||
|
fun getInstance(context: Context): FavoriteDataSource { |
||||
|
if (!this::INSTANCE.isInitialized) { |
||||
|
synchronized(FavoriteDataSource::class.java) { |
||||
|
if (!this::INSTANCE.isInitialized) { |
||||
|
val database = AppDatabase.getDatabase(context) |
||||
|
INSTANCE = FavoriteDataSource(database.favoriteDao()) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return INSTANCE |
||||
|
} |
||||
|
} |
||||
|
} |
@ -1,88 +0,0 @@ |
|||||
package awais.instagrabber.db.repositories; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import awais.instagrabber.db.datasources.FavoriteDataSource; |
|
||||
import awais.instagrabber.db.entities.Favorite; |
|
||||
import awais.instagrabber.models.enums.FavoriteType; |
|
||||
import awais.instagrabber.utils.AppExecutors; |
|
||||
|
|
||||
public class FavoriteRepository { |
|
||||
private static final String TAG = FavoriteRepository.class.getSimpleName(); |
|
||||
|
|
||||
private static FavoriteRepository instance; |
|
||||
|
|
||||
private final AppExecutors appExecutors; |
|
||||
private final FavoriteDataSource favoriteDataSource; |
|
||||
|
|
||||
private FavoriteRepository(final AppExecutors appExecutors, final FavoriteDataSource favoriteDataSource) { |
|
||||
this.appExecutors = appExecutors; |
|
||||
this.favoriteDataSource = favoriteDataSource; |
|
||||
} |
|
||||
|
|
||||
public static FavoriteRepository getInstance(final FavoriteDataSource favoriteDataSource) { |
|
||||
if (instance == null) { |
|
||||
instance = new FavoriteRepository(AppExecutors.INSTANCE, favoriteDataSource); |
|
||||
} |
|
||||
return instance; |
|
||||
} |
|
||||
|
|
||||
public void getFavorite(final String query, final FavoriteType type, final RepositoryCallback<Favorite> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
final Favorite favorite = favoriteDataSource.getFavorite(query, type); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
if (favorite == null) { |
|
||||
callback.onDataNotAvailable(); |
|
||||
return; |
|
||||
} |
|
||||
callback.onSuccess(favorite); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
public void getAllFavorites(final RepositoryCallback<List<Favorite>> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
final List<Favorite> favorites = favoriteDataSource.getAllFavorites(); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
if (favorites == null) { |
|
||||
callback.onDataNotAvailable(); |
|
||||
return; |
|
||||
} |
|
||||
callback.onSuccess(favorites); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
public void insertOrUpdateFavorite(final Favorite favorite, |
|
||||
final RepositoryCallback<Void> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
favoriteDataSource.insertOrUpdateFavorite(favorite); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
callback.onSuccess(null); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
public void deleteFavorite(final String query, |
|
||||
final FavoriteType type, |
|
||||
final RepositoryCallback<Void> callback) { |
|
||||
// request on the I/O thread |
|
||||
appExecutors.getDiskIO().execute(() -> { |
|
||||
favoriteDataSource.deleteFavorite(query, type); |
|
||||
// notify on the main thread |
|
||||
appExecutors.getMainThread().execute(() -> { |
|
||||
if (callback == null) return; |
|
||||
callback.onSuccess(null); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,28 @@ |
|||||
|
package awais.instagrabber.db.repositories |
||||
|
|
||||
|
import awais.instagrabber.db.datasources.FavoriteDataSource |
||||
|
import awais.instagrabber.db.entities.Favorite |
||||
|
import awais.instagrabber.models.enums.FavoriteType |
||||
|
|
||||
|
class FavoriteRepository private constructor(private val favoriteDataSource: FavoriteDataSource) { |
||||
|
|
||||
|
suspend fun getFavorite(query: String, type: FavoriteType): Favorite? = favoriteDataSource.getFavorite(query, type) |
||||
|
|
||||
|
suspend fun getAllFavorites(): List<Favorite> = favoriteDataSource.getAllFavorites() |
||||
|
|
||||
|
suspend fun insertOrUpdateFavorite(favorite: Favorite) = favoriteDataSource.insertOrUpdateFavorite(favorite) |
||||
|
|
||||
|
suspend fun deleteFavorite(query: String, type: FavoriteType) = favoriteDataSource.deleteFavorite(query, type) |
||||
|
|
||||
|
companion object { |
||||
|
private lateinit var instance: FavoriteRepository |
||||
|
|
||||
|
@JvmStatic |
||||
|
fun getInstance(favoriteDataSource: FavoriteDataSource): FavoriteRepository { |
||||
|
if (!this::instance.isInitialized) { |
||||
|
instance = FavoriteRepository(favoriteDataSource) |
||||
|
} |
||||
|
return instance |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue