-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
1,225 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/org/stepic/droid/features/stories/mapper/StoryTemplatesMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.stepic.droid.features.stories.mapper | ||
|
||
import org.stepic.droid.features.stories.model.PlainTextWithButtonStoryPart | ||
import org.stepik.android.model.StoryTemplate | ||
import ru.nobird.android.stories.model.Story | ||
import ru.nobird.android.stories.model.StoryPart | ||
|
||
fun StoryTemplate.toStory(): Story = | ||
Story( | ||
id, | ||
title, | ||
cover, | ||
parts.map(StoryTemplate.Part::toStoryPart) | ||
) | ||
|
||
fun StoryTemplate.Part.toStoryPart(): StoryPart = | ||
PlainTextWithButtonStoryPart( | ||
duration * 1000, // convert seconds to ms | ||
image, | ||
button, | ||
text | ||
) |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/org/stepic/droid/features/stories/model/PlainTextWithButtonStoryPart.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.stepic.droid.features.stories.model | ||
|
||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import org.stepik.android.model.StoryTemplate | ||
import ru.nobird.android.stories.model.StoryPart | ||
|
||
class PlainTextWithButtonStoryPart( | ||
duration: Long, | ||
image: String, | ||
|
||
val button: StoryTemplate.Button?, | ||
val text: StoryTemplate.Text? | ||
): StoryPart(duration, image) { | ||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
parcel.writeLong(duration) | ||
parcel.writeString(cover) | ||
parcel.writeParcelable(button, flags) | ||
parcel.writeParcelable(text, flags) | ||
} | ||
|
||
override fun describeContents(): Int = 0 | ||
|
||
companion object CREATOR : Parcelable.Creator<PlainTextWithButtonStoryPart> { | ||
override fun createFromParcel(parcel: Parcel) = PlainTextWithButtonStoryPart( | ||
parcel.readLong(), | ||
parcel.readString(), | ||
parcel.readParcelable(StoryTemplate.Button::class.java.classLoader), | ||
parcel.readParcelable(StoryTemplate.Text::class.java.classLoader) | ||
) | ||
|
||
override fun newArray(size: Int) = | ||
arrayOfNulls<PlainTextWithButtonStoryPart>(size) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/org/stepic/droid/features/stories/model/ViewedStoryTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.stepic.droid.features.stories.model | ||
|
||
class ViewedStoryTemplate(val storyTemplateId: Long) |
72 changes: 72 additions & 0 deletions
72
app/src/main/java/org/stepic/droid/features/stories/presentation/StoriesPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.stepic.droid.features.stories.presentation | ||
|
||
import io.reactivex.Scheduler | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.rxkotlin.Singles.zip | ||
import io.reactivex.rxkotlin.subscribeBy | ||
import org.stepic.droid.core.presenters.PresenterBase | ||
import org.stepic.droid.di.qualifiers.BackgroundScheduler | ||
import org.stepic.droid.di.qualifiers.MainScheduler | ||
import org.stepic.droid.features.stories.mapper.toStory | ||
import org.stepic.droid.features.stories.repository.StoryTemplatesRepository | ||
import org.stepic.droid.util.addDisposable | ||
import org.stepik.android.model.StoryTemplate | ||
import javax.inject.Inject | ||
|
||
class StoriesPresenter | ||
@Inject | ||
constructor( | ||
private val storiesRepository: StoryTemplatesRepository, | ||
|
||
@BackgroundScheduler | ||
private val backgroundScheduler: Scheduler, | ||
@MainScheduler | ||
private val mainScheduler: Scheduler | ||
) : PresenterBase<StoriesView>() { | ||
private var state : StoriesView.State = StoriesView.State.Idle | ||
set(value) { | ||
field = value | ||
view?.setState(value) | ||
} | ||
|
||
private val compositeDisposable = CompositeDisposable() | ||
|
||
init { | ||
fetchStories() | ||
} | ||
|
||
private fun fetchStories() { | ||
if (state != StoriesView.State.Idle) return | ||
state = StoriesView.State.Loading | ||
|
||
compositeDisposable addDisposable | ||
zip(storiesRepository.getStoryTemplates().map { it.map(StoryTemplate::toStory) }, storiesRepository.getViewedStoriesIds()) | ||
.subscribeOn(backgroundScheduler) | ||
.observeOn(mainScheduler) | ||
.subscribeBy({ | ||
state = StoriesView.State.Empty | ||
}) { (stories, viewedIds) -> | ||
state = if (stories.isNotEmpty()) { | ||
StoriesView.State.Success(stories, viewedIds) | ||
} else { | ||
StoriesView.State.Empty | ||
} | ||
} | ||
} | ||
|
||
override fun attachView(view: StoriesView) { | ||
super.attachView(view) | ||
view.setState(state) | ||
} | ||
|
||
fun onStoryViewed(storyId: Long) { | ||
compositeDisposable addDisposable storiesRepository | ||
.markStoryAsViewed(storyId) | ||
.subscribeBy({ /* ignore */ }) { | ||
val state = this.state | ||
if (state is StoriesView.State.Success) { | ||
this.state = state.copy(viewedStoriesIds = state.viewedStoriesIds + storyId) | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/org/stepic/droid/features/stories/presentation/StoriesView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.stepic.droid.features.stories.presentation | ||
|
||
import ru.nobird.android.stories.model.Story | ||
|
||
interface StoriesView { | ||
fun setState(state: State) | ||
|
||
sealed class State { | ||
object Idle : State() | ||
object Loading : State() | ||
object Empty : State() | ||
data class Success(val stories: List<Story>, val viewedStoriesIds: Set<Long>) : State() | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/org/stepic/droid/features/stories/repository/StoryTemplatesRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.stepic.droid.features.stories.repository | ||
|
||
import io.reactivex.Completable | ||
import io.reactivex.Single | ||
import org.stepik.android.model.StoryTemplate | ||
|
||
interface StoryTemplatesRepository { | ||
fun getStoryTemplates(): Single<List<StoryTemplate>> | ||
|
||
fun getViewedStoriesIds(): Single<Set<Long>> | ||
|
||
fun markStoryAsViewed(storyTemplateId: Long): Completable | ||
} |
61 changes: 61 additions & 0 deletions
61
...rc/main/java/org/stepic/droid/features/stories/repository/StoryTemplatesRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.stepic.droid.features.stories.repository | ||
|
||
import io.reactivex.Completable | ||
import io.reactivex.Observable | ||
import io.reactivex.Single | ||
import io.reactivex.rxkotlin.toObservable | ||
import org.stepic.droid.di.AppSingleton | ||
import org.stepic.droid.features.stories.model.ViewedStoryTemplate | ||
import org.stepic.droid.storage.dao.IDao | ||
import org.stepic.droid.web.Api | ||
import org.stepic.droid.web.model.story_templates.StoryTemplatesResponse | ||
import org.stepik.android.model.StoryTemplate | ||
import javax.inject.Inject | ||
|
||
@AppSingleton | ||
class StoryTemplatesRepositoryImpl | ||
@Inject | ||
constructor( | ||
private val api: Api, | ||
private val viewedStoryTemplateDao: IDao<ViewedStoryTemplate> | ||
) : StoryTemplatesRepository { | ||
companion object { | ||
const val STORY_TEMPLATES_VERSION = 1 | ||
} | ||
|
||
override fun getStoryTemplates(): Single<List<StoryTemplate>> = | ||
getStoryTemplatesByPage(1) | ||
.concatMap { it.storyTemplates.toObservable() } | ||
.toList() | ||
.map { | ||
it.asSequence() | ||
.filter { template -> template.version <= STORY_TEMPLATES_VERSION } | ||
.sortedBy(StoryTemplate::position) | ||
.toList() | ||
} | ||
|
||
private fun getStoryTemplatesByPage(page: Int): Observable<StoryTemplatesResponse> = | ||
api.getStoryTemplates(page) | ||
.concatMap { | ||
val templatesObservable = Observable.just(it) | ||
if (it.meta.hasNext) { | ||
templatesObservable.concatWith(getStoryTemplatesByPage(it.meta.page + 1)) | ||
} else { | ||
templatesObservable | ||
} | ||
} | ||
|
||
override fun getViewedStoriesIds(): Single<Set<Long>> = Single.create { emitter -> | ||
emitter.onSuccess( | ||
viewedStoryTemplateDao | ||
.getAll() | ||
.asSequence() | ||
.map { it.storyTemplateId } | ||
.toSet() | ||
) | ||
} | ||
|
||
override fun markStoryAsViewed(storyTemplateId: Long): Completable = Completable.fromAction { | ||
viewedStoryTemplateDao.insertOrReplace(ViewedStoryTemplate(storyTemplateId)) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...rc/main/java/org/stepic/droid/features/stories/storage/dao/ViewedStoryTemplatesDaoImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.stepic.droid.features.stories.storage.dao | ||
|
||
import android.content.ContentValues | ||
import android.database.Cursor | ||
import org.stepic.droid.di.storage.StorageSingleton | ||
import org.stepic.droid.features.stories.model.ViewedStoryTemplate | ||
import org.stepic.droid.features.stories.storage.structure.DbStructureViewedStoryTemplates | ||
import org.stepic.droid.storage.dao.DaoBase | ||
import org.stepic.droid.storage.operations.DatabaseOperations | ||
import javax.inject.Inject | ||
|
||
@StorageSingleton | ||
class ViewedStoryTemplatesDaoImpl | ||
@Inject | ||
constructor(databaseOperations: DatabaseOperations): DaoBase<ViewedStoryTemplate>(databaseOperations) { | ||
override fun getDbName(): String = | ||
DbStructureViewedStoryTemplates.VIEWED_STORY_TEMPLATES | ||
|
||
override fun getDefaultPrimaryColumn(): String = | ||
DbStructureViewedStoryTemplates.Columns.ID | ||
|
||
override fun getDefaultPrimaryValue(persistentObject: ViewedStoryTemplate): String = | ||
persistentObject.storyTemplateId.toString() | ||
|
||
override fun getContentValues(persistentObject: ViewedStoryTemplate): ContentValues = ContentValues(1).apply { | ||
put(DbStructureViewedStoryTemplates.Columns.ID, persistentObject.storyTemplateId) | ||
} | ||
|
||
override fun parsePersistentObject(cursor: Cursor): ViewedStoryTemplate = | ||
ViewedStoryTemplate(cursor.getLong(cursor.getColumnIndex(DbStructureViewedStoryTemplates.Columns.ID))) | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...va/org/stepic/droid/features/stories/storage/structure/DbStructureViewedStoryTemplates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.stepic.droid.features.stories.storage.structure | ||
|
||
import android.database.sqlite.SQLiteDatabase | ||
|
||
object DbStructureViewedStoryTemplates { | ||
const val VIEWED_STORY_TEMPLATES = "viewed_story_templates" | ||
|
||
object Columns { | ||
const val ID = "id" | ||
} | ||
|
||
fun createTable(db: SQLiteDatabase) { | ||
val sql = """ | ||
CREATE TABLE IF NOT EXISTS $VIEWED_STORY_TEMPLATES ( | ||
${Columns.ID} LONG, | ||
PRIMARY KEY(${Columns.ID}) | ||
)""".trimIndent() | ||
|
||
db.execSQL(sql) | ||
} | ||
} |
Oops, something went wrong.