Skip to content

Commit

Permalink
clean architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
5undays committed Apr 7, 2023
1 parent 59e8cbd commit e408710
Show file tree
Hide file tree
Showing 31 changed files with 133 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package com.cinema.classic.data.interest

import androidx.room.Room
import androidx.test.platform.app.InstrumentationRegistry
import com.cinema.classic.data.AppDatabase
import com.cinema.classic.data.local.AppDatabase
import kotlinx.coroutines.runBlocking
import com.cinema.classic.data.MovieClipDao
import com.cinema.classic.data.local.MovieClipApi
import org.junit.After
import org.junit.Before
import org.junit.Test

class MovieClipDaoTest {
class MovieClipApiTest {
private lateinit var database: AppDatabase
private lateinit var movieClipDao: MovieClipDao
private lateinit var movieClipApi: MovieClipApi

@Before
fun createDb() = runBlocking {
val context = InstrumentationRegistry.getInstrumentation().targetContext
database = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java).build()
movieClipDao = database.movieClipDao()
movieClipApi = database.movieClipDao()
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/cinema/classic/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import com.cinema.classic.compose.Home
import com.cinema.classic.viewmodels.MainViewModel
import com.cinema.classic.presentation.ui.Home
import com.cinema.classic.presentation.main_list.MainViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/cinema/classic/YoutubeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import android.view.WindowManager
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import com.cinema.classic.compose.movieData
import com.cinema.classic.viewmodels.VideoViewModel
import com.cinema.classic.presentation.ui.movieData
import com.cinema.classic.presentation.video_detail.VideoViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/cinema/classic/common/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cinema.classic.common

object Constants {
const val BASE_URL = "https://www.googleapis.com/youtube/v3/search/"
const val NAVER_URL = "https://openapi.naver.com/v1/search/movie.json"
const val KMDB_URL =
"https://api.koreafilm.or.kr/openapi-data2/wisenut/search_api/search_json2.jsp"

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.cinema.classic.data
package com.cinema.classic.common

import androidx.room.TypeConverter
import java.util.Calendar
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/cinema/classic/common/Resource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cinema.classic.common

sealed class Resource<T>(val data: T? = null, val message: String? = "") {
class Success
class Error
class Loading()
}
32 changes: 0 additions & 32 deletions app/src/main/java/com/cinema/classic/data/MovieClipRepository.kt

This file was deleted.

23 changes: 0 additions & 23 deletions app/src/main/java/com/cinema/classic/data/MovieRepository.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cinema.classic.data
package com.cinema.classic.data.local

import android.content.Context
import androidx.room.Database
Expand All @@ -9,13 +9,14 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import com.cinema.classic.works.SeedDatabaseWorker
import com.cinema.classic.works.SeedDatabaseWorker.Companion.KEY_FILENAME
import com.cinema.classic.common.Converters
import com.cinema.classic.data.local.dto.MovieClip
import com.cinema.classic.data.local.SeedDatabaseWorker.Companion.KEY_FILENAME

@Database(entities = [MovieClip::class], version = 3)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun movieClipDao(): MovieClipDao
abstract fun movieClipDao(): MovieClipApi

companion object {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.cinema.classic.data
package com.cinema.classic.data.local

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.cinema.classic.data.local.dto.MovieClip
import kotlinx.coroutines.flow.Flow

@Dao
interface MovieClipDao {
interface MovieClipApi {
@Query("SELECT * FROM movie_clip WHERE video_id = :videoId ORDER BY reg_date DESC")
fun get(videoId : String): Flow<List<MovieClip>>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cinema.classic.works
package com.cinema.classic.data.local

import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.google.gson.stream.JsonReader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cinema.classic.data
package com.cinema.classic.data.local.dto

import androidx.room.ColumnInfo
import androidx.room.Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cinema.classic.api
package com.cinema.classic.data.remote

import com.cinema.classic.BuildConfig
import com.cinema.classic.data.KmdbResult
Expand All @@ -14,7 +14,7 @@ import retrofit2.http.Query
import retrofit2.http.Url


interface MovieService {
interface MovieApi {
@GET
suspend fun get3(
@Url url: String,
Expand Down Expand Up @@ -48,7 +48,7 @@ interface MovieService {
companion object {
private const val BASE_URL = "https://www.googleapis.com/youtube/v3/"

fun create(): MovieService {
fun create(): MovieApi {
val gson = GsonBuilder()
.setLenient()
.create()
Expand All @@ -57,7 +57,7 @@ interface MovieService {
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(MovieService::class.java)
.create(MovieApi::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cinema.classic.data.repository

import com.cinema.classic.data.local.MovieClipApi
import com.cinema.classic.data.local.dto.MovieClip
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class MovieClipRepository @Inject constructor(
private val movieClipApi: MovieClipApi
) {
fun get(videoId: String) = movieClipApi.get(videoId)

suspend fun delete(movieClip: MovieClip) {
movieClipApi.delete(movieClip)
}

suspend fun insert(movieClip: MovieClip) {
movieClipApi.insert(movieClip)
}

fun getLastVideo() : Flow<MovieClip> = movieClipApi.getLastVideo()

companion object {
@Volatile
private var instance: MovieClipRepository? = null

fun getInstance(movieClipApi: MovieClipApi) =
instance ?: synchronized(this) {
instance ?: MovieClipRepository(movieClipApi).also { instance = it }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cinema.classic.data.repository

import com.cinema.classic.common.Constants
import com.cinema.classic.data.KmdbResult
import com.cinema.classic.data.NaverResult
import com.cinema.classic.data.Youtube
import com.cinema.classic.data.remote.MovieApi
import retrofit2.Response
import javax.inject.Inject

class MovieRepository @Inject constructor(
private val service: MovieApi
) {
suspend fun load(): Response<Youtube> {
return service.get(Constants.BASE_URL)
}

suspend fun get(title: String, year: Int): Response<NaverResult> {
return service.get2(Constants.NAVER_URL, title, year, year)
}

suspend fun get2(title: String): Response<KmdbResult> {
return service.get3(Constants.KMDB_URL, title)
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/cinema/classic/di/DatabaseModule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.cinema.classic.di

import android.content.Context
import com.cinema.classic.data.MovieClipDao
import com.cinema.classic.data.AppDatabase
import com.cinema.classic.data.local.MovieClipApi
import com.cinema.classic.data.local.AppDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -21,7 +21,7 @@ class DatabaseModule {
}

@Provides
fun providePlantDao(appDatabase: AppDatabase): MovieClipDao {
fun providePlantDao(appDatabase: AppDatabase): MovieClipApi {
return appDatabase.movieClipDao()
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/cinema/classic/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cinema.classic.di

import com.cinema.classic.api.MovieService
import com.cinema.classic.data.remote.MovieApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -13,7 +13,7 @@ class NetworkModule {

@Singleton
@Provides
fun movieService(): MovieService {
return MovieService.create()
fun movieService(): MovieApi {
return MovieApi.create()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cinema.classic.compose
package com.cinema.classic.presentation.ui

import android.content.Intent
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -26,12 +26,12 @@ import androidx.core.os.bundleOf
import coil.compose.AsyncImage
import com.cinema.classic.R
import com.cinema.classic.YoutubeActivity
import com.cinema.classic.compose.theme.ClassicTheme
import com.cinema.classic.presentation.ui.theme.ClassicTheme
import com.cinema.classic.data.Item
import com.cinema.classic.data.MovieClip
import com.cinema.classic.data.local.dto.MovieClip
import com.cinema.classic.data.NaverMovie
import com.cinema.classic.data.YoutubeRepo
import com.cinema.classic.viewmodels.MainViewModel
import com.cinema.classic.presentation.main_list.MainViewModel

@Composable
fun Home(viewModel: MainViewModel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.cinema.classic.viewmodels
package com.cinema.classic.presentation.main_list

import androidx.lifecycle.*
import com.cinema.classic.data.MovieClip
import com.cinema.classic.data.local.dto.MovieClip
import com.cinema.classic.data.Item
import com.cinema.classic.data.NaverMovie
import com.cinema.classic.data.MovieClipRepository
import com.cinema.classic.data.MovieRepository
import com.cinema.classic.data.repository.MovieClipRepository
import com.cinema.classic.data.repository.MovieRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cinema.classic.compose.theme
package com.cinema.classic.presentation.ui.theme

import androidx.compose.ui.graphics.Color

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.cinema.classic.compose.theme
package com.cinema.classic.presentation.ui.theme

import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.shape.RoundedCornerShape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cinema.classic.compose.theme
package com.cinema.classic.presentation.ui.theme

import android.annotation.SuppressLint
import androidx.compose.foundation.isSystemInDarkTheme
Expand Down

0 comments on commit e408710

Please sign in to comment.