Skip to content

Commit

Permalink
Merge pull request #13 from piashcse/code-optimization
Browse files Browse the repository at this point in the history
- Removed duplicate code
  • Loading branch information
piashcse authored Nov 8, 2024
2 parents 3e2b2de + 5c42dcc commit 7792f5f
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 128 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,16 @@ This is an Xcode project that builds into an iOS application. It depends on and
## 👨 Developed By

<a href="https://twitter.com/piashcse" target="_blank">
<img src="https://avatars.githubusercontent.com/piashcse" width="80" align="left">
<img src="https://avatars.githubusercontent.com/piashcse" width="90" align="left">
</a>

**Mehedi Hassan Piash**

[![Twitter](https://img.shields.io/badge/-twitter-grey?logo=twitter)](https://twitter.com/piashcse)
[![Web](https://img.shields.io/badge/-web-grey?logo=appveyor)](https://piashcse.github.io/)
[![Medium](https://img.shields.io/badge/-medium-grey?logo=medium)](https://medium.com/@piashcse)
[![Linkedin](https://img.shields.io/badge/-linkedin-grey?logo=linkedin)](https://www.linkedin.com/in/piashcse/)
[![Twitter](https://img.shields.io/badge/-Twitter-1DA1F2?logo=x&logoColor=white&style=for-the-badge)](https://twitter.com/piashcse)
[![Medium](https://img.shields.io/badge/-Medium-00AB6C?logo=medium&logoColor=white&style=for-the-badge)](https://medium.com/@piashcse)
[![Linkedin](https://img.shields.io/badge/-LinkedIn-0077B5?logo=linkedin&logoColor=white&style=for-the-badge)](https://www.linkedin.com/in/piashcse/)
[![Web](https://img.shields.io/badge/-Web-0073E6?logo=appveyor&logoColor=white&style=for-the-badge)](https://piashcse.github.io/)
[![Blog](https://img.shields.io/badge/-Blog-0077B5?logo=readme&logoColor=white&style=for-the-badge)](https://piashcse.blogspot.com)

# License
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.http.encodedPath

class ApiImpl : ApiInterface {
class ApiService : ApiInterface {
override suspend fun nowPlayingMovieList(
page: Int,
): BaseModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package data.repository

import data.remote.ApiImpl
import data.remote.ApiService
import kotlinx.coroutines.flow.flow
import utils.network.UiState

class MovieRepository(private val api:ApiImpl = ApiImpl()) {
class Repository(private val api:ApiService = ApiService()) {
suspend fun nowPlayingMovie(page: Int) = flow {
try {
emit(UiState.Loading)
Expand Down
4 changes: 2 additions & 2 deletions composeApp/src/commonMain/kotlin/ui/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.MovieItem
import data.model.TvItem
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -19,7 +19,7 @@ import utils.network.UiState

@ExperimentalCoroutinesApi
class AppViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()
private val _movieSearchData = MutableStateFlow<List<MovieItem>>(arrayListOf())
val movieSearchData get() = _movieSearchData.asStateFlow()
private val _tvSeriesSearchData = MutableStateFlow<List<TvItem>>(arrayListOf())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ui.movie.artist_detail
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.artist.ArtistDetail
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class ArtistDetailViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()
private val _artistDetailResponse =
MutableStateFlow<ArtistDetail?>(null)
val artistDetailResponse get() = _artistDetailResponse.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.viewModelScope
import data.model.MovieItem
import data.model.artist.Artist
import data.model.movie_detail.MovieDetail
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -14,7 +14,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class MovieDetailViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()

private val _movieDetail = MutableStateFlow<MovieDetail?>(null)
val movieDetail get() = _movieDetail.asStateFlow()
Expand All @@ -30,69 +30,64 @@ class MovieDetailViewModel : ViewModel() {

fun movieDetail(movieId: Int) {
viewModelScope.launch {
viewModelScope.launch {
repo.movieDetail(movieId).onEach {
when (it) {
is UiState.Loading -> {
_isLoading.value = true
}
repo.movieDetail(movieId).onEach {
when (it) {
is UiState.Loading -> {
_isLoading.value = true
}

is UiState.Success -> {
_movieDetail.value = it.data
_isLoading.value = false
}
is UiState.Success -> {
_movieDetail.value = it.data
_isLoading.value = false
}

is UiState.Error -> {
_isLoading.value = false
}
is UiState.Error -> {
_isLoading.value = false
}
}.launchIn(viewModelScope)
}
}
}.launchIn(viewModelScope)
}
}

fun recommendedMovie(movieId: Int) {
viewModelScope.launch {
viewModelScope.launch {
repo.recommendedMovie(movieId).onEach {
when (it) {
is UiState.Loading -> {
_isLoading.value = true
}
repo.recommendedMovie(movieId).onEach {
when (it) {
is UiState.Loading -> {
_isLoading.value = true
}

is UiState.Success -> {
_recommendedMovie.value = it.data
_isLoading.value = false
}
is UiState.Success -> {
_recommendedMovie.value = it.data
_isLoading.value = false
}

is UiState.Error -> {
_isLoading.value = false
}
is UiState.Error -> {
_isLoading.value = false
}
}.launchIn(viewModelScope)
}
}
}.launchIn(viewModelScope)
}
}

fun movieCredit(movieId: Int) {
viewModelScope.launch {
viewModelScope.launch {
repo.movieCredit(movieId).onEach {
when (it) {
is UiState.Loading -> {
_isLoading.value = true
}
repo.movieCredit(movieId).onEach {
when (it) {
is UiState.Loading -> {
_isLoading.value = true
}

is UiState.Success -> {
_movieCredit.value = it.data
_isLoading.value = false
}
is UiState.Success -> {
_movieCredit.value = it.data
_isLoading.value = false
}

is UiState.Error -> {
_isLoading.value = false
}
is UiState.Error -> {
_isLoading.value = false
}
}.launchIn(viewModelScope)
}
}
}.launchIn(viewModelScope)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ui.movie.now_playing
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.MovieItem
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class NowPlayingViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()
private val _nowPlayingResponse = MutableStateFlow<List<MovieItem>>(arrayListOf())
val nowPlayingResponse get() = _nowPlayingResponse.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ui.movie.popular
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.MovieItem
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class PopularMovieViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()
private val _popularMovieResponse = MutableStateFlow<List<MovieItem>>(arrayListOf())
val popularMovieResponse get() = _popularMovieResponse.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ui.movie.top_rated
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.MovieItem
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class TopRatedViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()

private val _topRatedMovieResponse = MutableStateFlow<List<MovieItem>>(arrayListOf())
val topRatedMovieResponse get() = _topRatedMovieResponse.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ui.movie.upcoming
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.MovieItem
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class UpcomingMovieViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()
private val _upComingMovieResponse = MutableStateFlow<List<MovieItem>>(arrayListOf())
val upComingMovieResponse get() = _upComingMovieResponse.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ui.tv_series.airing_today
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import data.model.TvItem
import data.repository.MovieRepository
import data.repository.Repository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
import utils.network.UiState

class AiringTodayTvSeriesViewModel : ViewModel() {
private val repo = MovieRepository()
private val repo = Repository()
private val _airingTodayTvSeriesResponse = MutableStateFlow<List<TvItem>>(arrayListOf())
val airingTodayTvSeriesResponse get() = _airingTodayTvSeriesResponse.asStateFlow()

Expand Down
Loading

0 comments on commit 7792f5f

Please sign in to comment.