Browse Source
Convert RecentSearchDao, RecentSearchDataSource and RecentSearchRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Convert RecentSearchDao, RecentSearchDataSource and RecentSearchRepository to kotlin
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
5 changed files with 132 additions and 225 deletions
-
30app/src/main/java/awais/instagrabber/db/dao/RecentSearchDao.kt
-
72app/src/main/java/awais/instagrabber/db/datasources/RecentSearchDataSource.kt
-
142app/src/main/java/awais/instagrabber/db/repositories/RecentSearchRepository.kt
-
14app/src/main/java/awais/instagrabber/repositories/responses/search/SearchResponse.kt
-
63app/src/main/java/awais/instagrabber/viewmodels/SearchFragmentViewModel.java
@ -1,36 +1,28 @@ |
|||
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.Query; |
|||
import androidx.room.Update; |
|||
|
|||
import java.util.List; |
|||
|
|||
import awais.instagrabber.db.entities.RecentSearch; |
|||
import awais.instagrabber.models.enums.FavoriteType; |
|||
import androidx.room.* |
|||
import awais.instagrabber.db.entities.RecentSearch |
|||
import awais.instagrabber.models.enums.FavoriteType |
|||
|
|||
@Dao |
|||
public interface RecentSearchDao { |
|||
|
|||
interface RecentSearchDao { |
|||
@Query("SELECT * FROM recent_searches ORDER BY last_searched_on DESC") |
|||
List<RecentSearch> getAllRecentSearches(); |
|||
suspend fun getAllRecentSearches(): List<RecentSearch> |
|||
|
|||
@Query("SELECT * FROM recent_searches WHERE `ig_id` = :igId AND `type` = :type") |
|||
RecentSearch getRecentSearchByIgIdAndType(String igId, FavoriteType type); |
|||
suspend fun getRecentSearchByIgIdAndType(igId: String, type: FavoriteType): RecentSearch? |
|||
|
|||
@Query("SELECT * FROM recent_searches WHERE instr(`name`, :query) > 0") |
|||
List<RecentSearch> findRecentSearchesWithNameContaining(String query); |
|||
suspend fun findRecentSearchesWithNameContaining(query: String): List<RecentSearch> |
|||
|
|||
@Insert |
|||
Long insertRecentSearch(RecentSearch recentSearch); |
|||
suspend fun insertRecentSearch(recentSearch: RecentSearch) |
|||
|
|||
@Update |
|||
void updateRecentSearch(RecentSearch recentSearch); |
|||
suspend fun updateRecentSearch(recentSearch: RecentSearch) |
|||
|
|||
@Delete |
|||
void deleteRecentSearch(RecentSearch recentSearch); |
|||
suspend fun deleteRecentSearch(recentSearch: RecentSearch) |
|||
|
|||
// @Query("DELETE from recent_searches") |
|||
// void deleteAllRecentSearches(); |
@ -1,57 +1,43 @@ |
|||
package awais.instagrabber.db.datasources; |
|||
package awais.instagrabber.db.datasources |
|||
|
|||
import android.content.Context; |
|||
import android.content.Context |
|||
import awais.instagrabber.db.AppDatabase |
|||
import awais.instagrabber.db.dao.RecentSearchDao |
|||
import awais.instagrabber.db.entities.RecentSearch |
|||
import awais.instagrabber.models.enums.FavoriteType |
|||
|
|||
import androidx.annotation.NonNull; |
|||
class RecentSearchDataSource private constructor(private val recentSearchDao: RecentSearchDao) { |
|||
|
|||
import java.util.List; |
|||
suspend fun getRecentSearchByIgIdAndType(igId: String, type: FavoriteType): RecentSearch? = |
|||
recentSearchDao.getRecentSearchByIgIdAndType(igId, type) |
|||
|
|||
import awais.instagrabber.db.AppDatabase; |
|||
import awais.instagrabber.db.dao.RecentSearchDao; |
|||
import awais.instagrabber.db.entities.RecentSearch; |
|||
import awais.instagrabber.models.enums.FavoriteType; |
|||
suspend fun getAllRecentSearches(): List<RecentSearch> = recentSearchDao.getAllRecentSearches() |
|||
|
|||
public class RecentSearchDataSource { |
|||
private static final String TAG = RecentSearchDataSource.class.getSimpleName(); |
|||
|
|||
private static RecentSearchDataSource INSTANCE; |
|||
|
|||
private final RecentSearchDao recentSearchDao; |
|||
|
|||
private RecentSearchDataSource(final RecentSearchDao recentSearchDao) { |
|||
this.recentSearchDao = recentSearchDao; |
|||
} |
|||
|
|||
public static synchronized RecentSearchDataSource getInstance(@NonNull Context context) { |
|||
if (INSTANCE == null) { |
|||
synchronized (RecentSearchDataSource.class) { |
|||
if (INSTANCE == null) { |
|||
final AppDatabase database = AppDatabase.getDatabase(context); |
|||
INSTANCE = new RecentSearchDataSource(database.recentSearchDao()); |
|||
} |
|||
suspend fun insertOrUpdateRecentSearch(recentSearch: RecentSearch) { |
|||
if (recentSearch.id != 0) { |
|||
recentSearchDao.updateRecentSearch(recentSearch) |
|||
return |
|||
} |
|||
} |
|||
return INSTANCE; |
|||
recentSearchDao.insertRecentSearch(recentSearch) |
|||
} |
|||
|
|||
public RecentSearch getRecentSearchByIgIdAndType(@NonNull final String igId, @NonNull final FavoriteType type) { |
|||
return recentSearchDao.getRecentSearchByIgIdAndType(igId, type); |
|||
} |
|||
suspend fun deleteRecentSearch(recentSearch: RecentSearch) = recentSearchDao.deleteRecentSearch(recentSearch) |
|||
|
|||
@NonNull |
|||
public final List<RecentSearch> getAllRecentSearches() { |
|||
return recentSearchDao.getAllRecentSearches(); |
|||
} |
|||
companion object { |
|||
private lateinit var INSTANCE: RecentSearchDataSource |
|||
|
|||
public final void insertOrUpdateRecentSearch(@NonNull final RecentSearch recentSearch) { |
|||
if (recentSearch.getId() != 0) { |
|||
recentSearchDao.updateRecentSearch(recentSearch); |
|||
return; |
|||
@JvmStatic |
|||
@Synchronized |
|||
fun getInstance(context: Context): RecentSearchDataSource { |
|||
if (!this::INSTANCE.isInitialized) { |
|||
synchronized(RecentSearchDataSource::class.java) { |
|||
if (!this::INSTANCE.isInitialized) { |
|||
val database = AppDatabase.getDatabase(context) |
|||
INSTANCE = RecentSearchDataSource(database.recentSearchDao()) |
|||
} |
|||
recentSearchDao.insertRecentSearch(recentSearch); |
|||
} |
|||
|
|||
public final void deleteRecentSearch(@NonNull final RecentSearch recentSearch) { |
|||
recentSearchDao.deleteRecentSearch(recentSearch); |
|||
} |
|||
return INSTANCE |
|||
} |
|||
} |
|||
} |
@ -1,124 +1,52 @@ |
|||
package awais.instagrabber.db.repositories; |
|||
package awais.instagrabber.db.repositories |
|||
|
|||
import androidx.annotation.NonNull; |
|||
import awais.instagrabber.db.datasources.RecentSearchDataSource |
|||
import awais.instagrabber.db.entities.RecentSearch |
|||
import awais.instagrabber.models.enums.FavoriteType |
|||
import java.time.LocalDateTime |
|||
|
|||
import java.time.LocalDateTime; |
|||
import java.util.List; |
|||
class RecentSearchRepository private constructor(private val recentSearchDataSource: RecentSearchDataSource) { |
|||
suspend fun getRecentSearch(igId: String, type: FavoriteType): RecentSearch? = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type) |
|||
|
|||
import awais.instagrabber.db.datasources.RecentSearchDataSource; |
|||
import awais.instagrabber.db.entities.RecentSearch; |
|||
import awais.instagrabber.models.enums.FavoriteType; |
|||
import awais.instagrabber.utils.AppExecutors; |
|||
suspend fun getAllRecentSearches(): List<RecentSearch> = recentSearchDataSource.getAllRecentSearches() |
|||
|
|||
public class RecentSearchRepository { |
|||
private static final String TAG = RecentSearchRepository.class.getSimpleName(); |
|||
suspend fun insertOrUpdateRecentSearch(recentSearch: RecentSearch) = |
|||
insertOrUpdateRecentSearch(recentSearch.igId, recentSearch.name, recentSearch.username, recentSearch.picUrl, recentSearch.type) |
|||
|
|||
private static RecentSearchRepository instance; |
|||
|
|||
private final AppExecutors appExecutors; |
|||
private final RecentSearchDataSource recentSearchDataSource; |
|||
|
|||
private RecentSearchRepository(final AppExecutors appExecutors, final RecentSearchDataSource recentSearchDataSource) { |
|||
this.appExecutors = appExecutors; |
|||
this.recentSearchDataSource = recentSearchDataSource; |
|||
} |
|||
|
|||
public static RecentSearchRepository getInstance(final RecentSearchDataSource recentSearchDataSource) { |
|||
if (instance == null) { |
|||
instance = new RecentSearchRepository(AppExecutors.INSTANCE, recentSearchDataSource); |
|||
private suspend fun insertOrUpdateRecentSearch( |
|||
igId: String, |
|||
name: String, |
|||
username: String?, |
|||
picUrl: String?, |
|||
type: FavoriteType, |
|||
) { |
|||
var recentSearch = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type) |
|||
recentSearch = if (recentSearch == null) { |
|||
RecentSearch(igId, name, username, picUrl, type, LocalDateTime.now()) |
|||
} else { |
|||
RecentSearch(recentSearch.id, igId, name, username, picUrl, type, LocalDateTime.now()) |
|||
} |
|||
return instance; |
|||
recentSearchDataSource.insertOrUpdateRecentSearch(recentSearch) |
|||
} |
|||
|
|||
public void getRecentSearch(@NonNull final String igId, |
|||
@NonNull final FavoriteType type, |
|||
final RepositoryCallback<RecentSearch> callback) { |
|||
// request on the I/O thread |
|||
appExecutors.getDiskIO().execute(() -> { |
|||
final RecentSearch recentSearch = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type); |
|||
// notify on the main thread |
|||
appExecutors.getMainThread().execute(() -> { |
|||
if (callback == null) return; |
|||
if (recentSearch == null) { |
|||
callback.onDataNotAvailable(); |
|||
return; |
|||
suspend fun deleteRecentSearchByIgIdAndType(igId: String, type: FavoriteType) { |
|||
val recentSearch = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type) |
|||
if (recentSearch != null) { |
|||
recentSearchDataSource.deleteRecentSearch(recentSearch) |
|||
} |
|||
callback.onSuccess(recentSearch); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
public void getAllRecentSearches(final RepositoryCallback<List<RecentSearch>> callback) { |
|||
// request on the I/O thread |
|||
appExecutors.getDiskIO().execute(() -> { |
|||
final List<RecentSearch> recentSearches = recentSearchDataSource.getAllRecentSearches(); |
|||
// notify on the main thread |
|||
appExecutors.getMainThread().execute(() -> { |
|||
if (callback == null) return; |
|||
callback.onSuccess(recentSearches); |
|||
}); |
|||
}); |
|||
} |
|||
suspend fun deleteRecentSearch(recentSearch: RecentSearch) = recentSearchDataSource.deleteRecentSearch(recentSearch) |
|||
|
|||
public void insertOrUpdateRecentSearch(@NonNull final RecentSearch recentSearch, |
|||
final RepositoryCallback<Void> callback) { |
|||
insertOrUpdateRecentSearch(recentSearch.getIgId(), recentSearch.getName(), recentSearch.getUsername(), recentSearch.getPicUrl(), |
|||
recentSearch.getType(), callback); |
|||
} |
|||
companion object { |
|||
private lateinit var instance: RecentSearchRepository |
|||
|
|||
public void insertOrUpdateRecentSearch(@NonNull final String igId, |
|||
@NonNull final String name, |
|||
final String username, |
|||
final String picUrl, |
|||
@NonNull final FavoriteType type, |
|||
final RepositoryCallback<Void> callback) { |
|||
// request on the I/O thread |
|||
appExecutors.getDiskIO().execute(() -> { |
|||
RecentSearch recentSearch = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type); |
|||
recentSearch = recentSearch == null |
|||
? new RecentSearch(igId, name, username, picUrl, type, LocalDateTime.now()) |
|||
: new RecentSearch(recentSearch.getId(), igId, name, username, picUrl, type, LocalDateTime.now()); |
|||
recentSearchDataSource.insertOrUpdateRecentSearch(recentSearch); |
|||
// notify on the main thread |
|||
appExecutors.getMainThread().execute(() -> { |
|||
if (callback == null) return; |
|||
callback.onSuccess(null); |
|||
}); |
|||
}); |
|||
@JvmStatic |
|||
fun getInstance(recentSearchDataSource: RecentSearchDataSource): RecentSearchRepository { |
|||
if (!this::instance.isInitialized) { |
|||
instance = RecentSearchRepository(recentSearchDataSource) |
|||
} |
|||
|
|||
public void deleteRecentSearchByIgIdAndType(@NonNull final String igId, |
|||
@NonNull final FavoriteType type, |
|||
final RepositoryCallback<Void> callback) { |
|||
// request on the I/O thread |
|||
appExecutors.getDiskIO().execute(() -> { |
|||
final RecentSearch recentSearch = recentSearchDataSource.getRecentSearchByIgIdAndType(igId, type); |
|||
if (recentSearch != null) { |
|||
recentSearchDataSource.deleteRecentSearch(recentSearch); |
|||
} |
|||
// notify on the main thread |
|||
appExecutors.getMainThread().execute(() -> { |
|||
if (callback == null) return; |
|||
if (recentSearch == null) { |
|||
callback.onDataNotAvailable(); |
|||
return; |
|||
return instance |
|||
} |
|||
callback.onSuccess(null); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
public void deleteRecentSearch(@NonNull final RecentSearch recentSearch, |
|||
final RepositoryCallback<Void> callback) { |
|||
// request on the I/O thread |
|||
appExecutors.getDiskIO().execute(() -> { |
|||
|
|||
recentSearchDataSource.deleteRecentSearch(recentSearch); |
|||
// notify on the main thread |
|||
appExecutors.getMainThread().execute(() -> { |
|||
if (callback == null) return; |
|||
callback.onSuccess(null); |
|||
}); |
|||
}); |
|||
} |
|||
} |
@ -1,10 +1,12 @@ |
|||
package awais.instagrabber.repositories.responses.search |
|||
|
|||
class SearchResponse(// app |
|||
val list: List<SearchItem>, |
|||
data class SearchResponse( |
|||
// app |
|||
val list: List<SearchItem>?, |
|||
// browser |
|||
val users: List<SearchItem>, |
|||
val places: List<SearchItem>, |
|||
val hashtags: List<SearchItem>, |
|||
val users: List<SearchItem>?, |
|||
val places: List<SearchItem>?, |
|||
val hashtags: List<SearchItem>?, |
|||
// universal |
|||
val status: String) |
|||
val status: String?, |
|||
) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue