Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
clean up feature / dog package (#75)
Browse files Browse the repository at this point in the history
* add KDoc

* rename main to dog

* create composable package

* create map package

* create navigation package

* create model package

* create view / viewmodel package

* move navigation
  • Loading branch information
okuzawats authored Jan 16, 2023
1 parent 21a34ff commit 38af3d0
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:usesCleartextTraffic="false"
tools:targetApi="31">
<activity
android:name=".feature.main.MainActivity"
android:name=".feature.dog.view.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.CleanArchitecture">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.okuzawats.cleanarchitecture.di

import com.okuzawats.cleanarchitecture.feature.main.MainNavigator
import com.okuzawats.cleanarchitecture.feature.dog.navigation.MainNavigator
import com.okuzawats.cleanarchitecture.navigator.MainNavigatorImpl
import dagger.Module
import dagger.Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.okuzawats.cleanarchitecture.navigator
import android.app.Activity
import android.content.Intent
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import com.okuzawats.cleanarchitecture.feature.main.MainNavigator
import com.okuzawats.cleanarchitecture.feature.dog.navigation.MainNavigator
import javax.inject.Inject

class MainNavigatorImpl @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.composable

import androidx.compose.material.Text
import androidx.compose.runtime.Composable

/**
* show dog error
*/
@Composable
fun ErrorView() = Text(text = "OOPS...SOMETHING HAPPEN!")
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.composable

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import coil.compose.AsyncImage
import coil.request.ImageRequest

/**
* show dog image
*/
@Composable
fun ImageView(imageUrl: String) = AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.composable

import androidx.compose.material.Text
import androidx.compose.runtime.Composable

/**
* initial view
*/
@Composable
fun InitialView() = Text(text = "")
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.composable

import androidx.compose.material.Text
import androidx.compose.runtime.Composable

/**
* loading a dog
*/
@Composable
fun LoadingView() = Text(text = "NOW LOADING...")
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.mapper

import com.okuzawats.cleanarchitecture.domain.getrandomdogimage.model.DogImage
import com.okuzawats.cleanarchitecture.feature.dog.model.DogImageGetResult
import javax.inject.Inject

/**
* mappter from domain to presentation
*/
class DomainToPresentationMapper @Inject constructor() {

/**
* map [DogImage] to [DogImageGetResult]
*/
fun toPresentation(dogImage: DogImage): DogImageGetResult =
when (dogImage) {
is DogImage.Image -> DogImageGetResult.Fetched(dogImage = dogImage.value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.mapper

import com.okuzawats.cleanarchitecture.feature.dog.viewmodel.MainViewModelEvent
import com.okuzawats.cleanarchitecture.feature.dog.viewmodel.MainViewModelState
import com.okuzawats.cleanarchitecture.feature.dog.view.UiEvent
import com.okuzawats.cleanarchitecture.feature.dog.view.UiState
import javax.inject.Inject

/**
* mapper from presentation to ui
*/
class PresentationToUiMapper @Inject constructor() {

/**
* map [MainViewModelState] to [UiState]
*/
fun toUi(presentationState: MainViewModelState): UiState =
when (presentationState) {
is MainViewModelState.Initial -> UiState.Initial
Expand All @@ -13,6 +24,9 @@ class PresentationToUiMapper @Inject constructor() {
is MainViewModelState.LoadFailed -> UiState.Error
}

/**
* map [MainViewModelEvent] to [UiEvent]
*/
fun toUi(presentationEvent: MainViewModelEvent): UiEvent =
when (presentationEvent) {
is MainViewModelEvent.NavigateToLicense -> UiEvent.NavigateToLicense
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.okuzawats.cleanarchitecture.feature.dog.model

/**
* presentation model of a dog
*/
sealed class DogImageGetResult {
/**
* represent a dog
*/
data class Fetched(val dogImage: String) : DogImageGetResult()

/**
* dog not loaded
*/
object FetchFailed : DogImageGetResult()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.okuzawats.cleanarchitecture.feature.dog.navigation

/**
* navigator for main
*/
interface MainNavigator {
/**
* navigate to license
*/
fun toLicense()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.view

import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -10,6 +10,9 @@ import androidx.compose.material.Surface
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import com.okuzawats.cleanarchitecture.feature.dog.mapper.PresentationToUiMapper
import com.okuzawats.cleanarchitecture.feature.dog.navigation.MainNavigator
import com.okuzawats.cleanarchitecture.feature.dog.viewmodel.MainViewModel
import com.okuzawats.cleanarchitecture.feature.theme.CleanArchitectureTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.view

sealed class UiEvent {
object NavigateToLicense : UiEvent()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.view

sealed class UiState {
companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.view

import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import com.okuzawats.cleanarchitecture.feature.dog.composable.ErrorView
import com.okuzawats.cleanarchitecture.feature.dog.composable.ImageView
import com.okuzawats.cleanarchitecture.feature.dog.composable.InitialView
import com.okuzawats.cleanarchitecture.feature.dog.composable.LoadingView
import javax.inject.Inject

/**
* renders [UiState] as Composable
*/
class UiStateRenderer @Inject constructor() {
@Composable
fun RenderAsComposable(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.viewmodel

import androidx.lifecycle.viewModelScope
import com.okuzawats.cleanarchitecture.domain.getrandomdogimage.GetRandomDogImageUseCase
import com.okuzawats.cleanarchitecture.feature.architecture.ViewModel
import com.okuzawats.cleanarchitecture.feature.dog.mapper.DomainToPresentationMapper
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.viewmodel

sealed class MainViewModelEvent {
object NavigateToLicense : MainViewModelEvent()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.okuzawats.cleanarchitecture.feature.main
package com.okuzawats.cleanarchitecture.feature.dog.viewmodel

import com.okuzawats.cleanarchitecture.feature.dog.model.DogImageGetResult

/**
* Express state of [MainViewModel]
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import com.okuzawats.cleanarchitecture.domain.getrandomdogimage.GetRandomDogImageUseCase
import com.okuzawats.cleanarchitecture.domain.getrandomdogimage.model.DogImage
import com.okuzawats.cleanarchitecture.feature.dog.mapper.DomainToPresentationMapper
import com.okuzawats.cleanarchitecture.feature.dog.viewmodel.MainViewModel
import com.okuzawats.cleanarchitecture.feature.dog.viewmodel.MainViewModelEvent
import com.okuzawats.cleanarchitecture.feature.dog.viewmodel.MainViewModelState
import com.okuzawats.cleanarchitecture.test.CoroutineRule
import io.mockk.coEvery
import io.mockk.mockk
Expand Down

0 comments on commit 38af3d0

Please sign in to comment.