-
Notifications
You must be signed in to change notification settings - Fork 122
[Horizon] Download file learning object #3116
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
Merged
Ahmed-Naguib93
merged 9 commits into
feature/horizon
from
feature/horizon-CLX-384-File-Learning-Object
Feb 11, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
52b89b6
feat: Add download file
Ahmed-Naguib93 c5b1f4a
feat: add download file
Ahmed-Naguib93 33ccee0
Merge branch 'feature/horizon' into feature/horizon-CLX-384-File-Lear…
Ahmed-Naguib93 fcf8797
Reorder files
Ahmed-Naguib93 49898e8
Order files
Ahmed-Naguib93 04a7a08
Update For CI
Ahmed-Naguib93 4bbf236
Addressed code reviews
Ahmed-Naguib93 184f1eb
Addressed code review
Ahmed-Naguib93 9d85269
Addressed code review
Ahmed-Naguib93 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -67,6 +67,9 @@ | |
}, | ||
"Confusing" : { | ||
|
||
}, | ||
"Download File" : { | ||
|
||
}, | ||
"Edit" : { | ||
|
||
|
80 changes: 80 additions & 0 deletions
80
Horizon/Horizon/Sources/Features/ModuleItemSequence/Domain/DownloadFileInteractor.swift
This file contains hidden or 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,80 @@ | ||
// | ||
// This file is part of Canvas. | ||
// Copyright (C) 2025-present Instructure, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
|
||
import Foundation | ||
import Core | ||
import Combine | ||
|
||
protocol DownloadFileInteractor { | ||
func download() -> AnyPublisher<URL, Error> | ||
} | ||
|
||
final class DownloadFileInteractorLive: DownloadFileInteractor { | ||
// MARK: - Dependencies | ||
|
||
private let courseID: String | ||
private let fileID: String | ||
private let fileManager: FileManager | ||
|
||
// MARK: - Init | ||
|
||
init( | ||
courseID: String, | ||
fileID: String, | ||
fileManager: FileManager = .default | ||
) { | ||
self.courseID = courseID | ||
self.fileID = fileID | ||
self.fileManager = fileManager | ||
} | ||
|
||
func download() -> AnyPublisher<URL, Error> { | ||
ReactiveStore( | ||
useCase: GetFile(context: .course(courseID), fileID: fileID) | ||
) | ||
.getEntities(ignoreCache: true) | ||
.flatMap { [weak self] files -> AnyPublisher<URL, Error> in | ||
guard let self, | ||
let file = files.first, | ||
let url = file.url | ||
else { | ||
return Empty(completeImmediately: true) | ||
.setFailureType(to: Error.self) | ||
.eraseToAnyPublisher() | ||
} | ||
let localURL = URL.Directories.documents.appendingPathComponent(file.filename) | ||
|
||
if self.fileManager.fileExists(atPath: localURL.path) { | ||
Ahmed-Naguib93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Just(localURL) | ||
.setFailureType(to: Error.self) | ||
.eraseToAnyPublisher() | ||
} else { | ||
return DownloadTaskPublisher(parameters: | ||
DownloadTaskParameters( | ||
remoteURL: url, | ||
localURL: localURL | ||
) | ||
) | ||
.collect() // Wait until the download is finished. | ||
.mapToValue(localURL) | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
} |
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
...on/Sources/Features/ModuleItemSequence/Domain/Preview/DownloadFileInteractorPreview.swift
This file contains hidden or 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,30 @@ | ||
// | ||
// This file is part of Canvas. | ||
// Copyright (C) 2025-present Instructure, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
|
||
#if DEBUG | ||
import Foundation | ||
import Combine | ||
|
||
class DownloadFileInteractorPreview: DownloadFileInteractor { | ||
func download() -> AnyPublisher<URL, Error> { | ||
Just(URL(string: "https://github.com/instructure/canvas-ios")!) | ||
.setFailureType(to: Error.self) | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
#endif |
58 changes: 58 additions & 0 deletions
58
...on/Horizon/Sources/Features/ModuleItemSequence/View/FileDetails/FileDetailsAssembly.swift
This file contains hidden or 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,58 @@ | ||
// | ||
// This file is part of Canvas. | ||
// Copyright (C) 2025-present Instructure, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
|
||
import Foundation | ||
import Core | ||
import SwiftUI | ||
|
||
struct FileDetailsAssembly { | ||
static func makeView( | ||
courseID: String, | ||
fileID: String, | ||
context: Context, | ||
fileName: String, | ||
isShowHeader: Binding<Bool> | ||
) -> FileDetailsView { | ||
let interactor = DownloadFileInteractorLive(courseID: courseID, fileID: fileID) | ||
let router = AppEnvironment.shared.router | ||
let viewModel = FileDetailsViewModel(interactor: interactor, router: router) | ||
return FileDetailsView( | ||
viewModel: viewModel, | ||
context: context, | ||
fileID: fileID, | ||
fileName: fileName, | ||
isShowHeader: isShowHeader | ||
) | ||
} | ||
|
||
#if DEBUG | ||
static func makePreview() -> FileDetailsView { | ||
let interactor = DownloadFileInteractorPreview() | ||
let router = AppEnvironment.shared.router | ||
let viewModel = FileDetailsViewModel(interactor: interactor, router: router) | ||
let view = FileDetailsView( | ||
viewModel: viewModel, | ||
context: nil, | ||
fileID: "23", | ||
fileName: "AI for Everyone.pdf", | ||
isShowHeader: .constant(true) | ||
) | ||
return view | ||
} | ||
#endif | ||
} |
78 changes: 78 additions & 0 deletions
78
.../Horizon/Sources/Features/ModuleItemSequence/View/FileDetails/Views/FileDetailsView.swift
This file contains hidden or 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,78 @@ | ||
// | ||
// This file is part of Canvas. | ||
// Copyright (C) 2025-present Instructure, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
// | ||
|
||
import SwiftUI | ||
import Core | ||
import HorizonUI | ||
|
||
struct FileDetailsView: View { | ||
// MARK: - Private Properties | ||
|
||
@State private var didFinishRenderingPreview: Bool = false | ||
@Environment(\.viewController) private var viewController | ||
|
||
// MARK: - Dependencies | ||
|
||
@State private var viewModel: FileDetailsViewModel | ||
private let context: Context? | ||
private let fileID: String | ||
private let fileName: String | ||
@Binding var isShowHeader: Bool | ||
|
||
init( | ||
viewModel: FileDetailsViewModel, | ||
context: Context?, | ||
fileID: String, | ||
fileName: String, | ||
isShowHeader: Binding<Bool> | ||
) { | ||
self.context = context | ||
self.fileID = fileID | ||
self.fileName = fileName | ||
self._isShowHeader = isShowHeader | ||
self.viewModel = viewModel | ||
} | ||
|
||
var body: some View { | ||
VStack { | ||
if isShowHeader { | ||
FileDownloadStatusView(status: viewModel.viewState, fileName: fileName) { | ||
viewModel.downloadFile(viewController: viewController) | ||
} onTapCancel: { | ||
viewModel.cancelDownload() | ||
} | ||
.padding(.vertical, .huiSpaces.primitives.small) | ||
.padding(.horizontal, .huiSpaces.primitives.medium) | ||
.hidden(!didFinishRenderingPreview) | ||
} | ||
FileDetailsViewRepresentable( | ||
isScrollTopReached: $isShowHeader, | ||
isFinishLoading: $didFinishRenderingPreview, | ||
context: context, | ||
fileID: fileID | ||
) | ||
} | ||
.animation(.smooth, value: viewModel.viewState) | ||
.animation(.smooth, value: [isShowHeader, didFinishRenderingPreview]) | ||
} | ||
} | ||
#if DEBUG | ||
#Preview { | ||
FileDetailsAssembly.makePreview() | ||
} | ||
#endif |
Oops, something went wrong.
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.
Interesting, I hadn't noticed this when I did the LTI and external URL stories.