-
Notifications
You must be signed in to change notification settings - Fork 0
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
Boxoffice mvi #13
Open
ChoiAnYong
wants to merge
7
commits into
main
Choose a base branch
from
boxoffice-MVI
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Boxoffice mvi #13
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
37e65f3
feat: NavigationStore ์ฐ๊ฒฐ
ChoiAnYong f37cf41
feat: BoxOfficeServiceProtocol ๊ตฌํ
ChoiAnYong 79b4caf
feat: BoxOfficeDomain ๊ตฌํ
ChoiAnYong 498e85d
feat: BoxOfficeStore ๊ตฌํ
ChoiAnYong 84fa1cb
feat: BoxOfficeView ๊ตฌํ
ChoiAnYong 539f7d4
feat: Navigation ๊ตฌํ
ChoiAnYong c00f87c
feat: MovieDetailView ๊ตฌํ
ChoiAnYong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// NavigationDomain.swift | ||
// BoxOffice | ||
// | ||
// Created by ์ต์์ฉ on 1/12/25. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NavigationIntent { | ||
case push(ViewType) | ||
case pop | ||
} | ||
|
||
enum ViewType: Hashable { | ||
case movieDetail(String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// NavigationStore.swift | ||
// BoxOffice | ||
// | ||
// Created by ์ต์์ฉ on 1/12/25. | ||
// | ||
|
||
import SwiftUI | ||
|
||
final class NavigationStore: ObservableObject { | ||
@Published var path: [ViewType] = [] | ||
|
||
@ViewBuilder | ||
func bulid(_ view: ViewType) -> some View { | ||
switch view { | ||
case .movieDetail(let movieId): | ||
MovieDetailView(movieId: movieId) | ||
} | ||
} | ||
|
||
func dispatch(_ intent: NavigationIntent) { | ||
switch intent { | ||
case .push(let view): | ||
path.append(view) | ||
case .pop: | ||
path.removeLast() | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
BoxOffice/BoxOffice/Source/Features/BoxOffice/BoxOfficeDomain.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// BoxOfficeDomain.swift | ||
// BoxOffice | ||
// | ||
// Created by ์ต์์ฉ on 1/8/25. | ||
// | ||
|
||
import Foundation | ||
|
||
struct BoxOfficeState { | ||
var boxOffice: [DetailBoxOffice] = [] | ||
var movie: MovieInfo? = nil | ||
var date: String = "" | ||
} | ||
|
||
|
||
enum BoxOfficeIntent { | ||
case onAppear | ||
case changeDate(plus: Int) | ||
case fetchBoxOffice | ||
case fetchMovieInfo(movieId: String) | ||
} |
94 changes: 94 additions & 0 deletions
94
BoxOffice/BoxOffice/Source/Features/BoxOffice/BoxOfficeStore.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// BoxOfficeStore.swift | ||
// BoxOffice | ||
// | ||
// Created by ์ต์์ฉ on 1/5/25. | ||
// | ||
|
||
import Foundation | ||
|
||
final class BoxOfficeStore: ObservableObject { | ||
@Published private(set) var state: BoxOfficeState | ||
|
||
private let boxOfficeService: BoxOfficeService | ||
|
||
init( | ||
state: BoxOfficeState = BoxOfficeState(), | ||
boxOfficeService: BoxOfficeService = BoxOfficeService() | ||
) { | ||
self.state = state | ||
self.boxOfficeService = boxOfficeService | ||
} | ||
|
||
func dispatch(_ intent: BoxOfficeIntent) { | ||
switch intent { | ||
case .onAppear: | ||
Task { | ||
do { | ||
await currentDate() | ||
try await fetchBoxOffice() | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
case .changeDate(let plus): | ||
Task { | ||
do { | ||
await calulateDate(plus) | ||
try await fetchBoxOffice() | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
case .fetchBoxOffice: | ||
Task { | ||
do { | ||
try await fetchBoxOffice() | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
case .fetchMovieInfo(let movieId): | ||
Task { | ||
do { | ||
try await fetchMovieInfo(movieId) | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
//MARK: - Functions | ||
|
||
extension BoxOfficeStore { | ||
@MainActor | ||
private func currentDate() { | ||
let date = Date() | ||
let df = DateFormatter() | ||
df.dateFormat = "yyyy-MM-dd" | ||
state.date = df.string(from: date) | ||
} | ||
|
||
@MainActor | ||
private func fetchBoxOffice() async throws { | ||
let date: String = state.date.split(separator: "-").map { String($0) }.joined() | ||
state.boxOffice = try await boxOfficeService.fetchBoxOffice(date: date).boxOfficeResult.dailyBoxOfficeList | ||
} | ||
|
||
@MainActor | ||
private func calulateDate(_ plus: Int) { | ||
let df = DateFormatter() | ||
df.dateFormat = "yyyy-MM-dd" | ||
guard let currentDate = df.date(from: state.date) else { return } | ||
|
||
let resultDate = Calendar.current.date(byAdding: .day, value: plus, to: currentDate) | ||
state.date = df.string(from: resultDate ?? Date()) | ||
} | ||
|
||
@MainActor | ||
private func fetchMovieInfo(_ movieId: String) async throws { | ||
state.movie = try await boxOfficeService.fetchMovieDetail(movieId: movieId).movieInfoResult.movieInfo | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,28 +8,33 @@ | |
import SwiftUI | ||
|
||
struct BoxOfficeView: View { | ||
@StateObject var viewModel: BoxOfficeViewModel | ||
@StateObject var store: BoxOfficeStore | ||
@EnvironmentObject private var navigationStore: NavigationStore | ||
|
||
var body: some View { | ||
NavigationStack { | ||
NavigationStack(path: $navigationStore.path) { | ||
ScrollView(showsIndicators: false) { | ||
TitleView(viewModel: viewModel) | ||
BoxOfficeListView(viewModel: viewModel) | ||
TitleView(store: store) | ||
BoxOfficeListView(store: store) | ||
} | ||
.navigationDestination(item: $viewModel.movie) { movie in | ||
MovieDetailView(movie: movie) | ||
.navigationDestination(for: ViewType.self) { view in | ||
navigationStore.bulid(view) | ||
.environmentObject(store) | ||
} | ||
} | ||
.onAppear { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ |
||
store.dispatch(.onAppear) | ||
} | ||
} | ||
} | ||
|
||
//MARK: - TitleView | ||
|
||
private struct TitleView: View { | ||
@ObservedObject private var viewModel: BoxOfficeViewModel | ||
@ObservedObject private var store: BoxOfficeStore | ||
|
||
fileprivate init(viewModel: BoxOfficeViewModel) { | ||
self.viewModel = viewModel | ||
fileprivate init(store: BoxOfficeStore) { | ||
self.store = store | ||
} | ||
|
||
fileprivate var body: some View { | ||
|
@@ -40,17 +45,17 @@ private struct TitleView: View { | |
|
||
HStack { | ||
Button { | ||
viewModel.calulateDate(-1) | ||
store.dispatch(.changeDate(plus: -1)) | ||
} label: { | ||
Image(systemName: "control") | ||
.rotationEffect(Angle(degrees: -90)) | ||
} | ||
|
||
Text("\(viewModel.date)") | ||
Text("\(store.state.date)") | ||
.font(.headline) | ||
|
||
Button { | ||
viewModel.calulateDate(1) | ||
store.dispatch(.changeDate(plus: 1)) | ||
} label: { | ||
Image(systemName: "control") | ||
.rotationEffect(Angle(degrees: 90)) | ||
|
@@ -64,25 +69,24 @@ private struct TitleView: View { | |
//MARK: - BoxOfficeListView | ||
|
||
private struct BoxOfficeListView: View { | ||
@ObservedObject private var viewModel: BoxOfficeViewModel | ||
@ObservedObject private var store: BoxOfficeStore | ||
@EnvironmentObject private var navigationStore: NavigationStore | ||
|
||
fileprivate init(viewModel: BoxOfficeViewModel) { | ||
self.viewModel = viewModel | ||
fileprivate init(store: BoxOfficeStore) { | ||
self.store = store | ||
} | ||
|
||
fileprivate var body: some View { | ||
Group { | ||
if viewModel.boxOffice.isEmpty { | ||
if store.state.boxOffice.isEmpty { | ||
Text("์ง๊ณ๋์ง ์์ ๋ ์ง์ ๋๋ค!") | ||
.font(.headline) | ||
.padding(.top, 300) | ||
} else { | ||
ForEach(viewModel.boxOffice, id: \.self) { boxOffice in | ||
ForEach(store.state.boxOffice, id: \.self) { boxOffice in | ||
BoxOfficeCell(boxOffice: boxOffice) | ||
.onTapGesture { | ||
Task { | ||
try await viewModel.fetchMovieInfo(boxOffice.movieCd) | ||
} | ||
navigationStore.dispatch(.push(.movieDetail(boxOffice.movieCd))) | ||
} | ||
} | ||
} | ||
|
@@ -131,5 +135,5 @@ private struct BoxOfficeCell:View { | |
} | ||
|
||
#Preview { | ||
BoxOfficeView(viewModel: BoxOfficeViewModel()) | ||
BoxOfficeView(store: BoxOfficeStore()) | ||
} |
54 changes: 0 additions & 54 deletions
54
BoxOffice/BoxOffice/Source/Features/BoxOffice/BoxOfficeViewModel.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฐ๋ณต๋๋ MainActor์ ์ฌ์ฉ์ด ์ข๋ ๊ฐ์ ํ ๋ถ๋ถ์ด ์์๊ฒ๊ฐ์๋ฐ,,,, ์์ ์ ์ด๋ป๊ฒ ํด์ผํ๋์ง๊ฐ ์๋ณด์ด๋ค์ ํ
์ถํ์ ๊ฐ์ด ๊ณ ๋ฏผํด๋ณด์ฃ !