Skip to content

Commit

Permalink
Merge pull request #76 from RajashekarRaju/restructure-architecture
Browse files Browse the repository at this point in the history
Restructure project architecture, improve testability, separated classes to packages by features
  • Loading branch information
RajashekarRaju authored Dec 22, 2024
2 parents a5ab877 + 9ff7e7a commit f88df3b
Show file tree
Hide file tree
Showing 104 changed files with 1,222 additions and 1,119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import androidx.compose.ui.test.performClick
import androidx.paging.PagingData
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.developersbreach.composeactors.data.datasource.fake.nowPlayingMoviesList
import com.developersbreach.composeactors.data.datasource.fake.popularActorList
import com.developersbreach.composeactors.data.datasource.fake.trendingActorList
import com.developersbreach.composeactors.data.datasource.fake.popularPersonLists
import com.developersbreach.composeactors.data.datasource.fake.trendingPersonLists
import com.developersbreach.composeactors.data.datasource.fake.upcomingMoviesList
import com.developersbreach.composeactors.ui.screens.search.SearchType
import kotlinx.coroutines.flow.flowOf
Expand All @@ -28,28 +28,28 @@ class HomeScreenUITest {
val composeTestRule = createComposeRule()

private val mockHomeUIState: HomeUIState = HomeUIState(
popularActorList = popularActorList,
trendingActorList = trendingActorList,
isFetchingActors = false,
popularPersonList = popularPersonLists,
trendingPersonList = trendingPersonLists,
isFetchingPersons = false,
upcomingMoviesList = upcomingMoviesList,
nowPlayingMoviesList = flowOf(PagingData.from(nowPlayingMoviesList))
)

@Composable
private fun HomeScreenUIContent(
navigateToSelectedActor: (actorId: Int) -> Unit = { },
navigateToSelectedPerson: (personId: Int) -> Unit = { },
navigateToSelectedMovie: (movieId: Int) -> Unit = { },
navigateToFavorite: () -> Unit = { },
navigateToSearch: (searchType: SearchType) -> Unit = { },
updateHomeSearchType: (searchType: SearchType) -> Unit = { },
navigateToAbout: () -> Unit = { },
navigateToSearchBySearchType: SearchType = SearchType.Actors,
navigateToSearchBySearchType: SearchType = SearchType.Persons,
homeSheetUIState: HomeSheetUIState = HomeSheetUIState(),
homeUIState: HomeUIState = mockHomeUIState
) {
HomeScreenUI(
modifier = Modifier,
navigateToSelectedActor = navigateToSelectedActor,
navigateToSelectedPerson = navigateToSelectedPerson,
navigateToSelectedMovie = navigateToSelectedMovie,
navigateToFavorite = navigateToFavorite,
navigateToSearch = navigateToSearch,
Expand Down Expand Up @@ -145,7 +145,7 @@ class HomeScreenUITest {
fun onClickSearchTopAppBar_shouldNavigateToSearchScreen() {
composeTestRule.setContent {
HomeScreenUIContent(
navigateToSearch = { SearchType.Actors },
navigateToSearch = { SearchType.Persons },
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.developersbreach.composeactors.core.database

import androidx.room.Database
import androidx.room.RoomDatabase
import com.developersbreach.composeactors.core.database.dao.FavoritePersonsDao
import com.developersbreach.composeactors.core.database.dao.FavoriteMoviesDao
import com.developersbreach.composeactors.core.database.entity.FavoritePersonsEntity
import com.developersbreach.composeactors.core.database.entity.FavoriteMoviesEntity


@Database(
entities = [FavoritePersonsEntity::class, FavoriteMoviesEntity::class],
version = 4,
exportSchema = false,
)
abstract class AppDatabase : RoomDatabase() {
abstract val favoritePersonsDao: FavoritePersonsDao
abstract val favoriteMoviesDao: FavoriteMoviesDao
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.developersbreach.composeactors.data.datasource.database.dao
package com.developersbreach.composeactors.core.database.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.developersbreach.composeactors.data.datasource.database.entity.FavoriteMoviesEntity
import com.developersbreach.composeactors.core.database.entity.FavoriteMoviesEntity

@Dao
interface FavoriteMoviesDao {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.developersbreach.composeactors.core.database.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.developersbreach.composeactors.core.database.entity.FavoritePersonsEntity

@Dao
interface FavoritePersonsDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addPersonToFavorites(favoritePersonsEntity: FavoritePersonsEntity)

@Query("SELECT * FROM favorite_persons_table")
fun getAllFavoritePersons(): LiveData<List<FavoritePersonsEntity>>

@Query("DELETE FROM favorite_persons_table")
fun deleteAllFavoritePersons()

@Delete
fun deleteSelectedFavoritePerson(favoritePersonsEntity: FavoritePersonsEntity)

@Query("SELECT column_person_id FROM favorite_persons_table WHERE column_person_id = :personId")
fun checkIfPersonIsFavorite(personId: Int): LiveData<Int>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.developersbreach.composeactors.data.datasource.database.entity
package com.developersbreach.composeactors.core.database.entity

import androidx.compose.runtime.Stable
import androidx.room.ColumnInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.developersbreach.composeactors.core.database.entity

import androidx.compose.runtime.Stable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "favorite_persons_table")
data class FavoritePersonsEntity(

@Stable
@PrimaryKey
@ColumnInfo(name = "column_person_id")
val personId: Int,

@ColumnInfo(name = "column_person_name")
val personName: String,

@ColumnInfo(name = "column_person_profileUrl")
val personProfileUrl: String,

@ColumnInfo(name = "column_person_placeOfBirth")
val personPlaceOfBirth: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.developersbreach.composeactors.core.network

import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

open class BaseUrlProvider {

fun String.toEncodedQuery(): String {
return try {
URLEncoder.encode(this, StandardCharsets.UTF_8.toString())
} catch (e: UnsupportedEncodingException) {
throw IllegalArgumentException("Failed to encode query: $this", e)
}
}

protected companion object {
const val BASE_URL = "https://api.themoviedb.org/3/"
const val API_KEY = "api_key=${TmdbApiKey.TMDB_API_KEY}"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.developersbreach.composeactors.utils
package com.developersbreach.composeactors.core.network

const val LOW_RES_IMAGE = "https://image.tmdb.org/t/p/w200"
const val HIGH_RES_IMAGE = "https://image.tmdb.org/t/p/w500"
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.developersbreach.composeactors.utils
package com.developersbreach.composeactors.core.network

import com.developersbreach.composeactors.data.PagedResponse
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.developersbreach.composeactors.data
package com.developersbreach.composeactors.core.network

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.developersbreach.composeactors.utils
package com.developersbreach.composeactors.core.network

/**
* Add your own TMDB_API key here.
Expand All @@ -7,8 +7,4 @@ package com.developersbreach.composeactors.utils
*/
object TmdbApiKey {
const val TMDB_API_KEY = ""
}

fun isTmdbApiKeyNotValid(): Boolean {
return TmdbApiKey.TMDB_API_KEY.isEmpty()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,44 @@ package com.developersbreach.composeactors.data.datasource.database

import androidx.lifecycle.LiveData
import androidx.lifecycle.map
import com.developersbreach.composeactors.data.model.FavoriteActor
import com.developersbreach.composeactors.data.model.Movie
import com.developersbreach.composeactors.data.model.actorAsDatabaseModel
import com.developersbreach.composeactors.data.model.actorAsDomainModel
import com.developersbreach.composeactors.data.model.movieAsDatabaseModel
import com.developersbreach.composeactors.data.model.movieAsDomainModel
import com.developersbreach.composeactors.core.database.AppDatabase
import com.developersbreach.composeactors.data.person.model.FavoritePerson
import com.developersbreach.composeactors.data.movie.model.Movie
import com.developersbreach.composeactors.data.person.model.personAsDatabaseModel
import com.developersbreach.composeactors.data.person.model.personAsDomainModel
import com.developersbreach.composeactors.data.movie.model.movieAsDatabaseModel
import com.developersbreach.composeactors.data.movie.model.movieAsDomainModel
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext


@Singleton
class DatabaseDataSource @Inject constructor(
private val database: AppDatabase
) {

fun getAllFavoriteMovies(): LiveData<List<Movie>> {
val allFavoriteMovies = database.favoriteMoviesDao.getAllFavoriteMovies()
// Change to distinct until changed
return allFavoriteMovies.map { favEntityList ->
favEntityList.movieAsDomainModel()
}
}

fun getAllFavoriteActors(): LiveData<List<FavoriteActor>> {
val allFavoriteActors = database.favoriteActorsDao.getAllFavoriteActors()
// Change to distinct until changed
return allFavoriteActors.map { favEntityList ->
favEntityList.actorAsDomainModel()
fun getAllFavoritePersons(): LiveData<List<FavoritePerson>> {
val allFavoritePersons = database.favoritePersonsDao.getAllFavoritePersons()
return allFavoritePersons.map { favEntityList ->
favEntityList.personAsDomainModel()
}
}

fun checkIfMovieIsFavorite(
movieId: Int
) = database.favoriteMoviesDao.checkIfMovieIsFavorite(movieId)

fun checkIfActorIsFavorite(
actorId: Int
) = database.favoriteActorsDao.checkIfActorIsFavorite(actorId)
fun checkIfPersonIsFavorite(
personId: Int
) = database.favoritePersonsDao.checkIfPersonIsFavorite(personId)

suspend fun addMovieToFavorites(
movie: Movie
Expand All @@ -51,11 +49,11 @@ class DatabaseDataSource @Inject constructor(
}
}

suspend fun addActorToFavorites(
favoriteActor: FavoriteActor
suspend fun addPersonToFavorites(
favoritePerson: FavoritePerson
) = withContext(Dispatchers.IO) {
with(favoriteActor.actorAsDatabaseModel()) {
database.favoriteActorsDao.addActorToFavorites(favoriteActorsEntity = this)
with(favoritePerson.personAsDatabaseModel()) {
database.favoritePersonsDao.addPersonToFavorites(favoritePersonsEntity = this)
}
}

Expand All @@ -67,11 +65,11 @@ class DatabaseDataSource @Inject constructor(
}
}

suspend fun deleteSelectedFavoriteActor(
favoriteActor: FavoriteActor
suspend fun deleteSelectedFavoritePerson(
favoritePerson: FavoritePerson
) = withContext(Dispatchers.IO) {
with(favoriteActor.actorAsDatabaseModel()) {
database.favoriteActorsDao.deleteSelectedFavoriteActor(favoriteActorsEntity = this)
with(favoritePerson.personAsDatabaseModel()) {
database.favoritePersonsDao.deleteSelectedFavoritePerson(favoritePersonsEntity = this)
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f88df3b

Please sign in to comment.