Skip to content

Commit

Permalink
Merge pull request #47 from wordpress-mobile/issue/44_extract_domain_…
Browse files Browse the repository at this point in the history
…module

Introduce domain module
  • Loading branch information
wzieba authored Jul 27, 2021
2 parents bec507b + d60a623 commit 784c7ad
Show file tree
Hide file tree
Showing 25 changed files with 219 additions and 317 deletions.
2 changes: 2 additions & 0 deletions MediaPicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ android {
}

dependencies {
implementation project(':MediaPicker:domain')

implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
Expand Down
1 change: 1 addition & 0 deletions MediaPicker/domain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
11 changes: 11 additions & 0 deletions MediaPicker/domain/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id "org.jetbrains.kotlin.jvm"
}
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'

testImplementation "org.mockito:mockito-inline:3.11.2"
testImplementation "org.assertj:assertj-core:3.19.0"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation 'junit:junit:4.13.2'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.wordpress.android.mediapicker

import org.wordpress.android.mediapicker.MediaItem.IdentifierType.LOCAL_ID
import org.wordpress.android.mediapicker.MediaItem.IdentifierType.LOCAL_URI
import org.wordpress.android.mediapicker.MediaItem.IdentifierType.REMOTE_ID
import org.wordpress.android.mediapicker.util.MediaUri

data class MediaItem(
val identifier: Identifier,
val url: String,
val name: String? = null,
val type: MediaType,
val mimeType: String? = null,
val dataModified: Long
) {
enum class IdentifierType {
LOCAL_URI,
REMOTE_ID,
LOCAL_ID,
}

sealed class Identifier(val type: IdentifierType) {
data class LocalUri(val value: MediaUri, val queued: Boolean = false) : Identifier(LOCAL_URI)

data class RemoteId(val value: Long) : Identifier(REMOTE_ID)

data class LocalId(val value: Int) : Identifier(LOCAL_ID)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.wordpress.android.mediapicker.loader.MediaSource.MediaLoadingResult
import org.wordpress.android.mediapicker.loader.MediaSource.MediaLoadingResult.Empty
import org.wordpress.android.mediapicker.loader.MediaSource.MediaLoadingResult.Failure
import org.wordpress.android.mediapicker.loader.MediaSource.MediaLoadingResult.Success
import org.wordpress.android.util.UiString

data class MediaLoader(private val mediaSource: MediaSource) {
suspend fun loadMedia(actions: Channel<LoadAction>): Flow<DomainModel> {
Expand Down Expand Up @@ -53,8 +52,8 @@ data class MediaLoader(private val mediaSource: MediaSource) {
is Start -> {
if (state.domainItems.isEmpty()) {
buildDomainModel(
mediaSource.load(filter = loadAction.filter),
state.copy(filter = loadAction.filter)
mediaSource.load(filter = loadAction.filter),
state.copy(filter = loadAction.filter)
)
} else {
state
Expand All @@ -63,10 +62,11 @@ data class MediaLoader(private val mediaSource: MediaSource) {
is Refresh -> {
if (loadAction.forced || state.domainItems.isEmpty()) {
buildDomainModel(
mediaSource.load(
filter = state.filter,
forced = loadAction.forced
), state
mediaSource.load(
filter = state.filter,
forced = loadAction.forced
),
state
)
} else {
state
Expand Down Expand Up @@ -103,34 +103,34 @@ data class MediaLoader(private val mediaSource: MediaSource) {
): DomainModel {
return when (partialResult) {
is Success -> state.copy(
isLoading = false,
hasMore = partialResult.hasMore,
domainItems = partialResult.data,
emptyState = null
isLoading = false,
hasMore = partialResult.hasMore,
domainItems = partialResult.data,
emptyState = null
)
is Empty -> state.copy(
isLoading = false,
hasMore = false,
domainItems = listOf(),
emptyState = EmptyState(
partialResult.title,
partialResult.htmlSubtitle,
partialResult.image,
partialResult.bottomImage,
partialResult.bottomImageContentDescription,
isError = false
)
isLoading = false,
hasMore = false,
domainItems = listOf(),
emptyState = EmptyState(
partialResult.title,
partialResult.htmlSubtitle,
partialResult.image,
partialResult.bottomImage,
partialResult.bottomImageContentDescription,
isError = false
)
)
is Failure -> state.copy(
isLoading = false,
hasMore = partialResult.data.isNotEmpty(),
domainItems = partialResult.data,
emptyState = EmptyState(
partialResult.title,
partialResult.htmlSubtitle,
partialResult.image,
isError = true
)
isLoading = false,
hasMore = partialResult.data.isNotEmpty(),
domainItems = partialResult.data,
emptyState = EmptyState(
partialResult.title,
partialResult.htmlSubtitle,
partialResult.image,
isError = true
)
)
}
}
Expand All @@ -153,11 +153,11 @@ data class MediaLoader(private val mediaSource: MediaSource) {
val emptyState: EmptyState? = null
) {
data class EmptyState(
val title: UiString,
val htmlSubtitle: UiString? = null,
val title: String,
val htmlSubtitle: String? = null,
val image: Int? = null,
val bottomImage: Int? = null,
val bottomImageDescription: UiString? = null,
val bottomImageDescription: String? = null,
val isError: Boolean = false
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.wordpress.android.mediapicker.loader

import org.wordpress.android.mediapicker.MediaItem
import org.wordpress.android.util.UiString

interface MediaSource {
suspend fun load(
Expand All @@ -11,18 +10,20 @@ interface MediaSource {
): MediaLoadingResult

sealed class MediaLoadingResult(open val data: List<MediaItem>) {

data class Success(override val data: List<MediaItem>, val hasMore: Boolean = false) : MediaLoadingResult(data)

data class Empty(
val title: UiString,
val htmlSubtitle: UiString? = null,
val title: String,
val htmlSubtitle: String? = null,
val image: Int? = null,
val bottomImage: Int? = null,
val bottomImageContentDescription: UiString? = null
val bottomImageContentDescription: String? = null
) : MediaLoadingResult(listOf())

data class Failure(
val title: UiString,
val htmlSubtitle: UiString? = null,
val title: String,
val htmlSubtitle: String? = null,
val image: Int? = null,
override val data: List<MediaItem> = listOf()
) : MediaLoadingResult(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.wordpress.android.mediapicker.util

@JvmInline
value class MediaUri(val s: String)
Loading

0 comments on commit 784c7ad

Please sign in to comment.