Skip to content

Instantly share code, notes, and snippets.

View enginebai's full-sized avatar
📈
Focusing, Learning, Improving, Evolving

Engine Bai enginebai

📈
Focusing, Learning, Improving, Evolving
View GitHub Profile
@Dao
interface PostDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(post: Post): Long
@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(post: Post)
@Transaction
fun upsert(post: Post) {
class PostBoundaryCallback : PagedList.BoundaryCallback<Post>(), KoinComponent {
private val remoteDataSource: PostApiService by inject()
private val localDataSource: PostDao by inject()
private val httpClient: OkHttpClient by inject()
private val gson: Gson by inject()
private var nextPageUrl: String? = null
override fun onZeroItemsLoaded() {
interface PostRepository {
fun fetchFeeds(): Completable
fun getFeeds(): Observable<List<Post>>
}
class PostRepositoryImpl : PostRepository {
private val remoteDataSource: PostApiService by inject()
private val localDataSource: PostDao by inject()
interface PostApiService {
@GET("/feed")
fun getFeed(): Single<Response<List<Post>>>
}
@Dao
interface PostDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(post: Post): Long
@Entity
data class Post(
@PrimaryKey
@SerializedName("id")
val id: String,
@SerializedName("sender")
@ColumnInfo
val sender: String?,
@SerializedName("caption")
@ColumnInfo
data class Location(
val name: String,
val lat: Double,
val lng: Double
)
enum class PostType {
TEXT,
PHOTO,
VIDEO,
class FeedViewModel : ViewModel(), KoinComponent {
private val repo: PostRepository by inject()
fun getFeeds(): Observable<PagedList<Post>> = repo.getFeeds()
}
class FeedActivity : BaseActivity() {
private lateinit var list: RecyclerView
private val adapter = FeedAdapter()
private val viewModel by viewModel<FeedViewModel>()
interface PostRepository {
fun getFeeds(): Observable<PagedList<Post>>
}
class PostRepositoryImpl : PostRepository {
override fun getFeeds(): Observable<PagedList<Post>> {
val dataSource = PostPagingDataSourceFactory()
val pagedListConfig = PagedList.Config.Builder()
.setPageSize(10)
class PostPagingDataSource : PageKeyedDataSource<String, Post>(), KoinComponent {
private val api: PostApiService by inject()
private val httpClient: OkHttpClient by inject()
private val gson: Gson by inject()
override fun loadInitial(
params: LoadInitialParams<String>,
callback: LoadInitialCallback<String, Post>
) {
data class Post(
@SerializedName("id")
val id: String,
@SerializedName("sender")
val sender: String?,
@SerializedName("caption")
val caption: String?,
@SerializedName("media")
val media: String?,
@SerializedName("timestamp")