Commit 5809bb86 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

feed use case

parent 06bbe455
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
<words> <words>
<w>Biganto</w> <w>Biganto</w>
<w>snackbar</w> <w>snackbar</w>
<w>upsert</w>
</words> </words>
</dictionary> </dictionary>
</component> </component>
\ No newline at end of file
package com.biganto.visual.roompark.data.data_provider package com.biganto.visual.roompark.data.data_provider
import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.data.local.UserState
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.mapper.fromRaw
import com.biganto.visual.roompark.domain.contract.AuthContract
import com.biganto.visual.roompark.domain.model.AuthInfoModel
import com.biganto.visual.roompark.domain.model.fromEntity
import io.reactivex.Completable
import io.reactivex.Observable
import timber.log.Timber
import javax.inject.Inject
/** /**
* Created by Vladislav Bogdashkin on 29.10.2019. * Created by Vladislav Bogdashkin on 29.10.2019.
*/ */
// //
//class AuthRepository @Inject constructor(
// val roomparkApi:IRoomParkApi
//) : AuthContract{ class AuthContractModule @Inject constructor(
// private val local: ILocalStore,
// override fun validateAuthState(): Observable<Boolean> { private val api: IRoomParkApi,
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates. private val db: IDb
// } ): AuthContract {
//
// override fun signIn(email: String, password: String): Observable<AuthInfoModel> = init {
// roomparkApi.authenticate(email,password) Timber.d("Auth Repository Created")
// .map { AuthInfoModel("",2,"","") } }
//
// override fun signOut(): Completable { override fun signOut(): Completable = local.setRecentUser(null)
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
// } override fun signIn(email: String, password: String): Observable<AuthInfoModel> =
// api.authenticate(email,password)
//} .map ( ::fromRaw )
\ No newline at end of file .flatMap{ db.upsertUser(it) }
.doOnNext{ Timber.d("user id: ${it.uuid}")}
.doOnNext { local.setRecentUser(it.uuid.toString()).blockingAwait() }
.map(::fromEntity)
override fun validateAuthState(): Observable<Boolean> = local.recentUser()
.map { when(it){
is UserState.NotAuthenticated -> false
is UserState.Authenticated -> true
else ->false
} }
}
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.mapper.fromRaw
import com.biganto.visual.roompark.domain.contract.FeedsContract
import com.biganto.visual.roompark.domain.model.*
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
import javax.inject.Inject
/**
* Created by Vladislav Bogdashkin on 29.10.2019.
*/
class FeedsContractModule @Inject constructor(
private val api: IRoomParkApi,
private val db: IDb
): FeedsContract {
override fun fetchFeeds(): Single<FeedsHeaderModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun fetchFeedObservable(id: Int): Observable<ArticlesPreviewModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getArticle(id: Int): Single<ArticlesPreviewModel> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
init {
Timber.d("Feeds Repository Created")
}
private fun fetchFeedsApi(token:String) =
api.getFeeds(token)
.doOnNext { Timber.d("raw0 $it") }
.map(::fromRaw)
.doOnNext(db::blockingUpsert)
.subscribeOn(Schedulers.io())
private val fetchFeedsDb =
db.fetchFeeds()
.toList()
.toObservable()
.subscribeOn(Schedulers.io())
fun getZones(userToken:String): Observable<List<FeedModel>> =
Observable.mergeDelayError(
arrayListOf(fetchFeedsApi(userToken),fetchFeedsDb)
).map { fromEntity(it) {ent ->fromEntity2(ent)}}
}
package com.biganto.visual.roompark.data.repository.db package com.biganto.visual.roompark.data.repository.db
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.UserEntity
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single
import io.requery.Persistable
/** /**
* Created by Vladislav Bogdashkin on 29.10.2019. * Created by Vladislav Bogdashkin on 29.10.2019.
*/ */
interface IDb { interface IDb {
fun upsertUser(entity: UserEntity): Observable<UserEntity>? fun upsertUser(entity: UserEntity): Observable<UserEntity>?
fun <T : Persistable> upsert(entity: T): Single<T>
fun <T : List<Persistable>> upsert(entity: T): Single<Iterable<Persistable>>
fun fetchFeeds(): Observable<FeedEntity>
fun fetchUser(uuid: Int): Observable<UserEntity>
fun <T : List<Persistable>> blockingUpsert(entity: T)
} }
\ No newline at end of file
...@@ -2,11 +2,14 @@ package com.biganto.visual.roompark.data.repository.db.requrey ...@@ -2,11 +2,14 @@ package com.biganto.visual.roompark.data.repository.db.requrey
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.data.repository.db.IDb import com.biganto.visual.roompark.data.repository.db.IDb
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.Models
import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity import com.biganto.visual.roompark.data.repository.db.requrey.model.UserEntity
import com.biganto.visual.roompark.di.dagger.DATABASE_VERSION import com.biganto.visual.roompark.di.dagger.DATABASE_VERSION
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import io.reactivex.Observable
import io.reactivex.Single
import io.requery.Persistable import io.requery.Persistable
import io.requery.android.sqlite.DatabaseSource import io.requery.android.sqlite.DatabaseSource
import io.requery.reactivex.KotlinReactiveEntityStore import io.requery.reactivex.KotlinReactiveEntityStore
...@@ -40,6 +43,29 @@ class RequeryRepository @Inject constructor( ...@@ -40,6 +43,29 @@ class RequeryRepository @Inject constructor(
) )
: IDb { : IDb {
override fun upsertUser(entity:UserEntity) = store.upsert(entity).toObservable() private inline fun <reified T:Persistable> fetchAll() =
store.select(T::class)
override fun upsertUser(entity:UserEntity): Observable<UserEntity> =
store.upsert(entity).toObservable()
override fun <T:Persistable> upsert(entity:T) = store.upsert(entity)
override fun <T : List<Persistable>> upsert(entity: T): Single<Iterable<Persistable>> =
store.upsert(entity)
override fun <T:List<Persistable>> blockingUpsert(entity:T) =
store.upsert(entity).toObservable().blockingSubscribe()
override fun fetchFeeds(): Observable<FeedEntity> =
fetchAll<FeedEntity>().get().observable()
override fun fetchUser(uuid:Int): Observable<UserEntity> =
fetchAll<UserEntity>().where(UserEntity.UUID.eq(uuid)).get().observable()
fun upsertFeeds(entity:List<FeedEntity>) =
store.upsert(entity)
} }
\ No newline at end of file
...@@ -2,6 +2,8 @@ package com.biganto.visual.roompark.data.repository.mapper ...@@ -2,6 +2,8 @@ package com.biganto.visual.roompark.data.repository.mapper
import android.content.res.Resources import android.content.res.Resources
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.AuthRaw 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.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.UserEntity
import kotlin.math.max import kotlin.math.max
...@@ -21,4 +23,16 @@ fun fromRaw(raw: AuthRaw) : UserEntity { ...@@ -21,4 +23,16 @@ fun fromRaw(raw: AuthRaw) : UserEntity {
return user return user
} }
fun fromRaw(raw:FeedRaw) : FeedEntity {
val entity = FeedEntity()
entity.setId(raw.id)
entity.setTitle(raw.title)
entity.setAlias(raw.alias)
return entity
}
fun fromRaw(raw: List<FeedRaw>):List<FeedEntity> = List(raw.size) { index-> fromRaw(raw[index]) }
val calcTargetResolution = max(display.widthPixels,display.heightPixels) val calcTargetResolution = max(display.widthPixels,display.heightPixels)
...@@ -43,32 +43,3 @@ abstract class AppModule{ ...@@ -43,32 +43,3 @@ abstract class AppModule{
} }
class AuthContractModule @Inject constructor(
private val local:ILocalStore,
private val api:IRoomParkApi,
private val db:IDb
):AuthContract{
init {
Timber.d("Auth Repository Created")
}
override fun signOut(): Completable = local.setRecentUser(null)
override fun signIn(email: String, password: String): Observable<AuthInfoModel> =
api.authenticate(email,password)
.map ( ::fromRaw )
.flatMap{ db.upsertUser(it) }
.doOnNext{Timber.d("user id: ${it.uuid}")}
.doOnNext { local.setRecentUser(it.uuid.toString()).blockingAwait() }
.map(::fromEntity)
override fun validateAuthState(): Observable<Boolean> = local.recentUser()
.map { when(it){
is UserState.NotAuthenticated -> false
is UserState.Authenticated -> true
else ->false
} }
}
...@@ -2,8 +2,8 @@ package com.biganto.visual.roompark.domain.contract ...@@ -2,8 +2,8 @@ package com.biganto.visual.roompark.domain.contract
import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel import com.biganto.visual.roompark.domain.model.ArticlesPreviewModel
import com.biganto.visual.roompark.domain.model.FeedsHeaderModel import com.biganto.visual.roompark.domain.model.FeedsHeaderModel
import io.reactivex.rxjava3.core.Observable import io.reactivex.Observable
import io.reactivex.rxjava3.core.Single import io.reactivex.Single
/** /**
* Created by Vladislav Bogdashkin on 24.09.2019. * Created by Vladislav Bogdashkin on 24.09.2019.
......
package com.biganto.visual.roompark.domain.model package com.biganto.visual.roompark.domain.model
import com.biganto.visual.roompark.data.repository.db.requrey.model.FeedEntity
import java.util.* import java.util.*
/** /**
...@@ -30,4 +31,12 @@ data class ArticleModel( ...@@ -30,4 +31,12 @@ data class ArticleModel(
val htmlBody:String, val htmlBody:String,
val previewUrl:String, val previewUrl:String,
val isRead:Boolean val isRead:Boolean
) )
\ No newline at end of file
fun fromEntity2(entity: FeedEntity):FeedModel = FeedModel(
feedId = entity.id,
title = entity.title,
alias = entity.alias
)
fun <E,M> fromEntity(raw: List<E>,block:(E)->M):List<M> = List(raw.size) { index-> block(raw[index]) }
...@@ -3,9 +3,13 @@ package com.biganto.visual.roompark.presentation.screen.auth ...@@ -3,9 +3,13 @@ package com.biganto.visual.roompark.presentation.screen.auth
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.base.IBottomNavigation import com.biganto.visual.roompark.base.IBottomNavigation
import com.biganto.visual.roompark.base.RoomParkMainActivity import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.data.data_provider.AuthContractModule
import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule
import com.biganto.visual.roompark.data.repository.db.requrey.DbModule import com.biganto.visual.roompark.data.repository.db.requrey.DbModule
import com.biganto.visual.roompark.di.dagger.* import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.AppModule
import com.biganto.visual.roompark.di.dagger.DataModule
import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import dagger.Binds import dagger.Binds
import dagger.BindsInstance import dagger.BindsInstance
...@@ -15,7 +19,12 @@ import dagger.Module ...@@ -15,7 +19,12 @@ import dagger.Module
@PerScreen @PerScreen
@Component( @Component(
modules = [AuthScreenModule::class,DataModule::class,RetrofitModule::class,AppModule::class, DbModule::class modules = [
AuthScreenModule::class
,DataModule::class
,RetrofitModule::class
,AppModule::class
,DbModule::class
], ],
dependencies = [AppComponent::class]) dependencies = [AppComponent::class])
interface AuthScreenComponent { interface AuthScreenComponent {
......
...@@ -2,9 +2,13 @@ package com.biganto.visual.roompark.presentation.screen.settings ...@@ -2,9 +2,13 @@ package com.biganto.visual.roompark.presentation.screen.settings
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.base.RoomParkMainActivity import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.data.data_provider.AuthContractModule
import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule import com.biganto.visual.roompark.data.repository.api.retrofit.di.RetrofitModule
import com.biganto.visual.roompark.data.repository.db.requrey.DbModule import com.biganto.visual.roompark.data.repository.db.requrey.DbModule
import com.biganto.visual.roompark.di.dagger.* import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.AppModule
import com.biganto.visual.roompark.di.dagger.DataModule
import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.contract.AuthContract import com.biganto.visual.roompark.domain.contract.AuthContract
import dagger.Binds import dagger.Binds
import dagger.BindsInstance import dagger.BindsInstance
......
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