Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save region to local and get region specific data #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.developersbreach.composeactors.data.datasource.database

import android.content.Context
import com.developersbreach.composeactors.utils.PreferenceConstants
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class PreferenceStoreDatabase @Inject constructor(@ApplicationContext context: Context){

private val sharedPreferences =
context.applicationContext.getSharedPreferences("compose_actors", Context.MODE_PRIVATE)

fun getRegion(): String? {
return sharedPreferences.getString(PreferenceConstants.USER_REGION, null)
}

fun setString(key: String, value: String) {
sharedPreferences.edit().putString(key, value).apply()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.developersbreach.composeactors.data.datasource.database.dao

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.developersbreach.composeactors.data.datasource.database.entity.UserRegionEntity

@Dao
interface UserRegionDao {

@Update(onConflict = OnConflictStrategy.REPLACE)
fun editUserRegion(userRegionEntity: UserRegionEntity)

@Query("SELECT * FROM user_region_table")
fun getSavedUserRegion(): LiveData<UserRegionEntity>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.developersbreach.composeactors.data.datasource.database.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "user_region_table")
data class UserRegionEntity(

@PrimaryKey
@ColumnInfo(name = "user_region_country_code")
val countryCode: Int,

@ColumnInfo(name = "user_region_country_name")
val countryName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,12 @@ class JsonRemoteData @Inject constructor(
val movieList: MutableList<Movie> = ArrayList()
val baseJsonArray = JSONObject(response)
val moviesJsonArray = baseJsonArray.getJSONArray("results")

for (notI: Int in 0 until 5) {
val upcomingMovieCount = if (moviesJsonArray.length() >= 5) {
5
} else {
moviesJsonArray.length()
}
for (notI: Int in 0 until upcomingMovieCount) {
val jsonObject = moviesJsonArray.getJSONObject(notI)
val movieId = jsonObject.getInt("id")
val originalTitle = jsonObject.getString("original_title")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class NetworkDataSource @Inject constructor(
) {

/*** @return the result of latest list of all popular actors fetched from the network.*/
suspend fun getPopularActorsData(): List<Actor> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getPopularActorsUrl()
suspend fun getPopularActorsData(region: String?): List<Actor> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getPopularActorsUrl(region)
val response: String = queryUtils.getResponseFromHttpUrl(requestUrl)
jsonData.fetchActorsJsonData(response)
}

/** @return the result of latest list of all trending actors fetched from the network. */
suspend fun getTrendingActorsData(): List<Actor> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getTrendingActorsUrl()
suspend fun getTrendingActorsData(region: String?): List<Actor> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getTrendingActorsUrl(region)
val response = queryUtils.getResponseFromHttpUrl(requestUrl)
jsonData.fetchActorsJsonData(response)
}
Expand Down Expand Up @@ -79,9 +79,10 @@ class NetworkDataSource @Inject constructor(
* @return the result of list of movies which are based on current movie id.
*/
suspend fun getSimilarMoviesByIdData(
movieId: Int
movieId: Int,
region: String?
): List<Movie> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getSimilarMoviesUrl(movieId)
val requestUrl = requestUrls.getSimilarMoviesUrl(movieId, region = region)
val response = queryUtils.getResponseFromHttpUrl(requestUrl)
jsonData.fetchSimilarAndRecommendedMoviesJsonData(response)
}
Expand All @@ -91,9 +92,10 @@ class NetworkDataSource @Inject constructor(
* @return the result of list of movies which are based on current movie id.
*/
suspend fun getRecommendedMoviesByIdData(
movieId: Int
movieId: Int,
region: String?
): List<Movie> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getRecommendedMoviesUrl(movieId)
val requestUrl = requestUrls.getRecommendedMoviesUrl(movieId, region = region)
val response = queryUtils.getResponseFromHttpUrl(requestUrl)
jsonData.fetchSimilarAndRecommendedMoviesJsonData(response)
}
Expand All @@ -110,8 +112,8 @@ class NetworkDataSource @Inject constructor(
jsonData.fetchMovieCastByIdJsonData(response)
}

suspend fun getUpcomingMoviesData(): List<Movie> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getUpcomingMoviesUrl()
suspend fun getUpcomingMoviesData(region: String?): List<Movie> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getUpcomingMoviesUrl(region = region)
val response = queryUtils.getResponseFromHttpUrl(requestUrl)
jsonData.fetchUpcomingMoviesJsonData(response)
}
Expand All @@ -125,9 +127,10 @@ class NetworkDataSource @Inject constructor(
}

suspend fun getNowPlayingMoviesData(
page: Int
page: Int,
region: String?
): PagedResponse<Movie> = withContext(Dispatchers.IO) {
val requestUrl = requestUrls.getNowPlayingMoviesUrl(page)
val requestUrl = requestUrls.getNowPlayingMoviesUrl(page, region)
val response = queryUtils.getResponseFromHttpUrl(requestUrl)
jsonData.fetchNowPlayingMoviesJsonData(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import javax.inject.Singleton
class RequestUrls @Inject constructor() {

// https://api.themoviedb.org/3/person/popular?api_key=API_KEY
fun getPopularActorsUrl(): URL {
return URL("${BASE_URL}person/popular?$API_KEY")
fun getPopularActorsUrl(region: String?): URL {
return URL("${BASE_URL}person/popular?$API_KEY&region=$region")
}

// https://api.themoviedb.org/3/trending/person/week?api_key=API_KEY
fun getTrendingActorsUrl(): URL {
return URL("${BASE_URL}trending/person/week?$API_KEY")
fun getTrendingActorsUrl(region: String?): URL {
return URL("${BASE_URL}trending/person/week?$API_KEY&region=$region")
}

// https://api.themoviedb.org/3/person/3233?api_key=API_KEY
Expand Down Expand Up @@ -59,17 +59,19 @@ class RequestUrls @Inject constructor() {
// https://api.themoviedb.org/3/movie/{movie_id}/recommendations?api_key=API_KEY&page=1
fun getRecommendedMoviesUrl(
movieId: Int,
page: Int = 1
page: Int = 1,
region: String?
): URL {
return URL("${BASE_URL}movie/$movieId/recommendations?$API_KEY&page=$page")
return URL("${BASE_URL}movie/$movieId/recommendations?$API_KEY&page=$page&region=$region")
}

// https://api.themoviedb.org/3/movie/{movie_id}/similar?api_key=API_KEY&page=1
fun getSimilarMoviesUrl(
movieId: Int,
page: Int = 1
page: Int = 1,
region: String?
): URL {
return URL("${BASE_URL}movie/$movieId/similar?$API_KEY&page=$page")
return URL("${BASE_URL}movie/$movieId/similar?$API_KEY&page=$page&region=$region")
}

// 299536
Expand Down Expand Up @@ -97,9 +99,10 @@ class RequestUrls @Inject constructor() {

// https://api.themoviedb.org/3/movie/upcoming?api_key=API_KEY&page=1
fun getUpcomingMoviesUrl(
page: Int = 1
page: Int = 1,
region: String?
): URL {
return URL("${BASE_URL}movie/upcoming?$API_KEY&page=$page")
return URL("${BASE_URL}movie/upcoming?$API_KEY&page=$page&region=$region")
}

// https://api.themoviedb.org/3/movie/{movie_id}/watch/providers?api_key
Expand All @@ -111,9 +114,10 @@ class RequestUrls @Inject constructor() {

// https://api.themoviedb.org/3/movie/now_playing?api_key=API_KEY&page=1
fun getNowPlayingMoviesUrl(
page: Int
page: Int,
region: String?
): URL {
return URL("${BASE_URL}movie/now_playing?$API_KEY&page=$page")
return URL("${BASE_URL}movie/now_playing?$API_KEY&page=$page&region=${region}")
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ data class HomeOptionItems(
id = 4,
title = "About",
icon = R.drawable.ic_about
),
HomeOptionItems(
id = 5,
title = "Region preference",
icon = R.drawable.ic_edit_location
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import com.developersbreach.composeactors.data.model.Actor
import com.developersbreach.composeactors.data.model.ActorDetail
import com.developersbreach.composeactors.data.model.FavoriteActor
import com.developersbreach.composeactors.data.model.Movie
import com.developersbreach.composeactors.data.repository.user.UserRepository
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ActorRepository @Inject constructor(
private val networkDataSource: NetworkDataSource,
private val databaseDataSource: DatabaseDataSource
private val databaseDataSource: DatabaseDataSource,
private val userRepository: UserRepository
) {
suspend fun getPopularActorsData(): List<Actor> {
return networkDataSource.getPopularActorsData()
return networkDataSource.getPopularActorsData(userRepository.getRegion())
}

suspend fun getTrendingActorsData(): List<Actor> {
return networkDataSource.getTrendingActorsData()
return networkDataSource.getTrendingActorsData(userRepository.getRegion())
}

suspend fun getUpcomingMoviesData(): List<Movie> {
return networkDataSource.getUpcomingMoviesData()
return networkDataSource.getUpcomingMoviesData(userRepository.getRegion())
}

suspend fun getSelectedActorData(actorInt: Int): ActorDetail {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ import com.developersbreach.composeactors.data.model.Cast
import com.developersbreach.composeactors.data.model.Movie
import com.developersbreach.composeactors.data.model.MovieDetail
import com.developersbreach.composeactors.data.model.MovieProvider
import com.developersbreach.composeactors.data.repository.user.UserRepository
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class MovieRepository @Inject constructor(
private val networkDataSource: NetworkDataSource,
private val databaseDataSource: DatabaseDataSource
private val databaseDataSource: DatabaseDataSource,
private val userRepository: UserRepository
) {

suspend fun getNowPlayingMoviesData(page: Int): PagedResponse<Movie> {
return networkDataSource.getNowPlayingMoviesData(page)
return networkDataSource.getNowPlayingMoviesData(page, userRepository.getRegion())
}

suspend fun getSelectedMovieData(movieId: Int): MovieDetail {
return networkDataSource.getSelectedMovieData(movieId)
}

suspend fun getSimilarMoviesByIdData(movieId: Int): List<Movie> {
return networkDataSource.getSimilarMoviesByIdData(movieId)
return networkDataSource.getSimilarMoviesByIdData(movieId, userRepository.getRegion())
}

suspend fun getRecommendedMoviesByIdData(movieId: Int): List<Movie> {
return networkDataSource.getRecommendedMoviesByIdData(movieId)
return networkDataSource.getRecommendedMoviesByIdData(movieId, userRepository.getRegion())
}

suspend fun getMovieCastByIdData(movieId: Int): List<Cast> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.developersbreach.composeactors.data.repository.user

import androidx.compose.runtime.MutableState
import com.developersbreach.composeactors.data.datasource.database.PreferenceStoreDatabase
import com.developersbreach.composeactors.utils.PreferenceConstants
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class UserRepository @Inject constructor(private val preferenceStoreDatabase: PreferenceStoreDatabase) {

fun setRegion(countryCode: String, setRegionSuccessesCallBack: MutableState<Boolean>): Boolean {
return if (countryCode != getRegion()) {
preferenceStoreDatabase.setString(
PreferenceConstants.USER_REGION,
countryCode
)
setRegionSuccessesCallBack.value = true
true
} else false
}

fun setDefaultRegion() {
if (getRegion() == null) {
preferenceStoreDatabase.setString(
PreferenceConstants.USER_REGION,
Locale.getDefault().country
)
}
}

fun getRegion(): String? {
return preferenceStoreDatabase.getRegion()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.developersbreach.composeactors.di

import android.content.Context
import com.developersbreach.composeactors.data.datasource.database.PreferenceStoreDatabase
import com.google.android.datatransport.runtime.dagger.Provides
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object PreferenceStoreModule {
@Provides
@Singleton
fun providePreferenceStore(@ApplicationContext context: Context): PreferenceStoreDatabase {
return PreferenceStoreDatabase(context = context.applicationContext)
}
}
Loading
Loading