Commit bba046fb authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

albums junctions

parent b18b3d12
package com.biganto.visual.roompark.data.data_provider
import com.biganto.visual.roompark.data.repository.api.IRoomParkApi
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.ImageAlbumJunctionEntity
import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.data.repository.mapper.fromRawList
import com.biganto.visual.roompark.domain.contract.DevProgressContract
import com.biganto.visual.roompark.domain.model.*
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject
/**
* Created by Vladislav Bogdashkin on 29.10.2019.
*/
//@Singleton
class AlbumsContractModule @Inject constructor(
private val api: IRoomParkApi,
private val db: IDb
): DevProgressContract {
override fun getProgressCards(): Observable<List<AlbumPreviewModel>> = fetchTopLevelAlbums()
override fun getProgressAlbumList(albumId: Int): Observable<List<AlbumPreviewModel>> =
fetchTopLevelAlbums()
override fun getAlbumPhoto(photoId: Int): Observable<PhotoModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getWebCamsList(): Observable<WebCamListModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getWebCamStream(camId: Int): Observable<WebCamModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
init {
Timber.d("Devs Repository Created")
}
private val fetchTopLevelAlbumsApi =
api.getAlbums()
.doOnNext { Timber.d("raw0 $it") }
.map{ fromRawList(it,::fromRaw) }
.doOnNext(db::blockingUpsert)
.subscribeOn(Schedulers.io())
private val fetchTopLevelAlbumsDb =
db.getTopLevelAlbums()
.toList()
.toObservable()
.subscribeOn(Schedulers.io())
private fun fetchTopLevelAlbums(): Observable<List<AlbumPreviewModel>> =
Observable.mergeDelayError(
arrayListOf(fetchTopLevelAlbumsApi,fetchTopLevelAlbumsDb)
).take(1).map { fromEntity(it,::fromEntity) }
private fun fetchAlbums(parentAlbumId:Int) =
api.getAlbums(parentAlbumId)
.doOnNext { Timber.d("raw0 $it") }
.map{ fromRawList(it,::fromRaw) }
.doOnNext(db::blockingUpsert)
.doOnNext {
it.asSequence().map { album ->
if (db.checkIfExistsAlbumJunction(album.id, parentAlbumId) != null) {
return@map null
}
val entity = ImageAlbumJunctionEntity()
entity.setAlbumId(album.id)
entity.setParentId(parentAlbumId)
entity
}.filterNotNull().toList().also { junctions -> db.blockingUpsert(junctions) }
}
.subscribeOn(Schedulers.io())
}
......@@ -4,6 +4,7 @@ import com.biganto.visual.roompark.data.repository.api.IRoomParkApi
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.mapper.fromRaw
import com.biganto.visual.roompark.data.repository.mapper.fromRawList
import com.biganto.visual.roompark.domain.contract.FeedsContract
import com.biganto.visual.roompark.domain.custom_exception.CustomApiException
import com.biganto.visual.roompark.domain.model.ArticlePreviewModel
......@@ -82,7 +83,7 @@ class FeedsContractModule @Inject constructor(
private fun fetchFeedsApi() =
api.getFeeds()
.doOnNext { Timber.d("raw0 $it") }
.map(::fromRaw)
.map{fromRawList(it,::fromRaw)}
.doOnNext(db::blockingUpsert)
.subscribeOn(Schedulers.io())
......
package com.biganto.visual.roompark.data.repository.db
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.*
import io.reactivex.Observable
import io.reactivex.Single
import io.requery.Persistable
......@@ -18,7 +16,10 @@ interface IDb {
fun fetchFeeds(): Observable<FeedEntity>
fun fetchUser(uuid: Int): Observable<UserEntity>
fun <T : List<Persistable>> blockingUpsert(entity: T)
fun userObservableResult(uuid: Int): Observable<ReactiveResult<UserEntity>>?
fun userObservableResult(uuid: Int): Observable<ReactiveResult<UserEntity>>
fun fetchArticles(feedAlias: String, pageSize: Int, startIndex: Int): Observable<ArticleEntity>
fun getFeed(feedId: Int): ReactiveResult<FeedEntity>?
fun getFeed(feedId: Int): ReactiveResult<FeedEntity>
fun getTopLevelAlbums(): Observable<ImageAlbumEntity>
fun getChildAlbums(parentId: Int): Observable<ImageAlbumEntity>
fun checkIfExistsAlbumJunction(albumId: Int, parentAlbumId: Int): ImageAlbumJunctionEntity?
}
\ No newline at end of file
......@@ -2,10 +2,7 @@ package com.biganto.visual.roompark.data.repository.db.requrey
import android.content.Context
import com.biganto.visual.roompark.data.repository.db.IDb
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.Models
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.*
import com.biganto.visual.roompark.di.dagger.DATABASE_VERSION
import dagger.Module
import dagger.Provides
......@@ -66,10 +63,21 @@ class RequeryRepository @Inject constructor(
override fun fetchFeeds(): Observable<FeedEntity> =
fetchAll<FeedEntity>().get().observable()
override fun getFeed(feedId:Int): ReactiveResult<FeedEntity>? =
override fun getFeed(feedId:Int): ReactiveResult<FeedEntity> =
fetchAll<FeedEntity>().where(FeedEntity.ID.eq(feedId))
.get()
override fun getTopLevelAlbums(): Observable<ImageAlbumEntity> =
fetchAll<ImageAlbumEntity>()
.get().observable()
override fun getChildAlbums(parentId:Int): Observable<ImageAlbumEntity> =
store.select(ImageAlbumEntity::class)
.join(ImageAlbumJunctionEntity::class)
.on(ImageAlbumJunctionEntity.ALBUM_ID.eq(ImageAlbumEntity.ID))
.where(ImageAlbumJunctionEntity.PARENT_ID.eq(parentId))
.get().observable()
override fun fetchArticles(feedAlias:String,pageSize:Int,startIndex:Int)
: Observable<ArticleEntity> =
fetchAll<ArticleEntity>()
......@@ -79,6 +87,13 @@ class RequeryRepository @Inject constructor(
.offset(startIndex)
.get().observable()
override fun checkIfExistsAlbumJunction(albumId:Int,parentAlbumId:Int) =
fetchAll<ImageAlbumJunctionEntity>()
.where(ImageAlbumJunctionEntity.ALBUM_ID.eq(albumId))
.and(ImageAlbumJunctionEntity.PARENT_ID.eq(parentAlbumId))
.get()
.firstOrNull()
override fun fetchUser(uuid:Int): Observable<UserEntity> =
fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable()
......
package com.biganto.visual.roompark.data.repository.mapper
import android.content.res.Resources
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ArticleRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.AuthRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.FeedRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.NewsArticleRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.*
import com.biganto.visual.roompark.data.repository.db.requrey.model.ArticleEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.ImageAlbumEntity
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import kotlin.math.max
......@@ -62,7 +60,20 @@ fun fromRaw(raw:NewsArticleRaw,feedId:Int):ArticleEntity{
return entity
}
fun fromRaw(raw: List<FeedRaw>):List<FeedEntity> = List(raw.size) { index-> fromRaw(raw[index]) }
fun fromRaw(raw:ImageAlbumRaw) : ImageAlbumEntity {
val entity = ImageAlbumEntity()
entity.setId(raw.id)
entity.setTitle(raw.title)
entity.setPublished(raw.date)
entity.setSort(raw.sort)
return entity
}
//fun fromRaw(raw: List<FeedRaw>):List<FeedEntity> = List(raw.size) { index-> fromRaw(raw[index]) }
fun <E,M> fromRawList(raw: List<E>,block:(E)->M):List<M> = List(raw.size) { index-> block(raw[index]) }
fun fromRaw(raw: List<NewsArticleRaw>,feedId:Int):List<ArticleEntity> =
List(raw.size) { index-> fromRaw(raw[index],feedId) }
......
......@@ -13,9 +13,9 @@ import io.reactivex.rxjava3.core.Single
interface DevProgressContract{
fun getProgressCards(): Single<List<AlbumPreviewModel>>
fun getProgressAlbumList(albumId:Int): Observable<List<AlbumPreviewModel>>
fun getAlbumPhoto(photoId:Int): Observable<PhotoModel>
fun getWebCamsList(): Single<WebCamListModel>
fun getWebCamStream(camId:Int): Single<WebCamModel>
fun getProgressCards(): io.reactivex.Observable<List<AlbumPreviewModel>>
fun getProgressAlbumList(albumId:Int): io.reactivex.Observable<List<AlbumPreviewModel>>
fun getAlbumPhoto(photoId:Int): io.reactivex.Observable<PhotoModel>
fun getWebCamsList(): io.reactivex.Observable<WebCamListModel>
fun getWebCamStream(camId:Int): io.reactivex.Observable<WebCamModel>
}
\ No newline at end of file
package com.biganto.visual.roompark.domain.model
import com.biganto.visual.roompark.data.repository.db.requrey.model.ImageAlbumEntity
import java.util.*
/**
......@@ -33,3 +34,14 @@ data class PhotoResolutionModel(
val resWidth:Int,
val resHeight:Int
)
fun fromEntity(entity: ImageAlbumEntity):AlbumPreviewModel =
AlbumPreviewModel(
albumId = entity.id,
parentId = -1,
published = entity.published,
title = entity.title,
previewUrl = "" ,// SHOULD BE FIRST AVALIABLE IMAGE FROM ALBUM PHOTOS
isRead = false
)
\ No newline at end of file
......@@ -11,6 +11,7 @@ import io.reactivex.Observable
interface FeedsScreen : BigantoBaseContract<FeedsScreenViewState> {
fun feedsTabSelected(): Observable<Int>
fun requsetsNewArticles(): Observable<Pair<FeedModel, Int>>
fun onAllFeedArticles(): Observable<FeedModel>
}
......
package com.biganto.visual.roompark.presentation.screen.feeds
import android.annotation.SuppressLint
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
......@@ -70,6 +69,10 @@ class FeedsScreenController :
@BindView(R.id.feedsBlock)
lateinit var feedsBlockView:ViewGroup
@BindView(R.id.to_feed_articles)
lateinit var allFeedArticles:MaterialTextView
override fun requsetsNewArticles() =
feedsRecyclerView.scrollStateChanges()
......@@ -109,21 +112,16 @@ class FeedsScreenController :
@Inject
override lateinit var injectedPresenter: FeedsScreenPresenter
override fun onAllFeedArticles(): Observable<FeedModel> =
allFeedArticles.clicks()
.map{ feedsTabs.getTabAt(feedsTabs.selectedTabPosition)}
.map { storedFeedsList[it.position] }
override fun onViewBound(v: View) {
toolBar.hideAll()
bindRecycler()
feedsBlockView.findViewById<MaterialTextView>(R.id.to_feed_articles).clicks()
.map{ feedsTabs.getTabAt(feedsTabs.selectedTabPosition)}
.map { it.tag as? Int }
.subscribe {
it?.let {
router.pushController(
RouterTransaction.with(ArticlesScreenController(it))
.popChangeHandler(FadeChangeHandler())
.pushChangeHandler(FadeChangeHandler())
)
}
}
}
override fun render(viewState: FeedsScreenViewState) {
......@@ -137,6 +135,7 @@ class FeedsScreenController :
is FeedsScreenViewState.CamsList -> render(viewState)
is FeedsScreenViewState.GetFeedArticlesPreview -> render(viewState)
is FeedsScreenViewState.RestoreView -> render(viewState)
is FeedsScreenViewState.ToArticlesScreen -> render(viewState)
is FeedsScreenViewState.SomeError -> render(viewState)
}
}
......@@ -145,12 +144,19 @@ class FeedsScreenController :
}
private fun render(viewState: FeedsScreenViewState.ToArticlesScreen) {
router.pushController(
RouterTransaction.with(ArticlesScreenController(viewState.feedId))
.popChangeHandler(FadeChangeHandler())
.pushChangeHandler(FadeChangeHandler())
)
}
private fun render(viewState: FeedsScreenViewState.SomeError) =
showError(viewState.exception)
private fun allFeedName(feedId:Int) = if (feedId==1) "НОВОСТИ" else "БЛОГИ"
@SuppressLint("SetTextI18n")
private fun render(viewState: FeedsScreenViewState.FeedsPages){
feedsTabs.removeAllTabs()
......
......@@ -63,6 +63,11 @@ class FeedsScreenPresenter @Inject constructor(
FeedsScreenViewState.GetFeedArticlesPreview(it.articles.toList())
}
val switchToAllArtiles = intent(FeedsScreen::onAllFeedArticles)
.map {
FeedsScreenViewState.ToArticlesScreen(it.feedId)
}
val getNewArticlesPage = intent(FeedsScreen::requsetsNewArticles)
.flatMap { interactor.fetchArticles(it.first.feedId, FEED_PREVIEW_PAGE_SIZE,it.second) }
.map {
......@@ -76,6 +81,7 @@ class FeedsScreenPresenter @Inject constructor(
.mergeWith(fetchAlbums)
.mergeWith(fetchCams)
.mergeWith(getNewArticlesPage)
.mergeWith(switchToAllArtiles)
.doOnError{ Timber.e(it)}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
......
......@@ -24,6 +24,8 @@ sealed class FeedsScreenViewState : BigantoBaseViewState() {
class RestoreView(val restore:RestoreModel) : FeedsScreenViewState()
class SomeError(val exception: ExceptionString) : FeedsScreenViewState()
class ToArticlesScreen(val feedId:Int) : FeedsScreenViewState()
}
data class RestoreModel(
......
......@@ -25,8 +25,6 @@
android:id="@+id/feedsRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/feeds_divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment