-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add FaqService * Add FAQ * Update package versions * Add localization for Read more * Show only accepted faqs * Use question mark icon for no faqs available
- Loading branch information
Showing
15 changed files
with
458 additions
and
11 deletions.
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
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,25 @@ | ||
// | ||
// PathViewModels.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 24.10.24. | ||
// | ||
|
||
import Common | ||
import Foundation | ||
import SharedModels | ||
|
||
@Observable | ||
final class FaqPathViewModel { | ||
let path: FaqPath | ||
var faq: DataState<FaqDTO> | ||
|
||
init(path: FaqPath) { | ||
self.path = path | ||
self.faq = path.faq.map(DataState.done) ?? .loading | ||
} | ||
|
||
func loadFaq() async { | ||
faq = await FaqServiceFactory.shared.getFaq(with: path.id, for: path.courseId ?? 0) | ||
} | ||
} |
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,30 @@ | ||
// | ||
// PathViews.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 24.10.24. | ||
// | ||
|
||
import DesignLibrary | ||
import SwiftUI | ||
|
||
public struct FaqPathView: View { | ||
@State private var viewModel: FaqPathViewModel | ||
|
||
public init(path: FaqPath) { | ||
self._viewModel = State(initialValue: FaqPathViewModel(path: path)) | ||
} | ||
|
||
public var body: some View { | ||
DataStateView(data: $viewModel.faq) { | ||
await viewModel.loadFaq() | ||
} content: { faq in | ||
FaqDetailView(faq: faq, namespace: viewModel.path.namespace) | ||
} | ||
.task { | ||
if case .loading = viewModel.faq { | ||
await viewModel.loadFaq() | ||
} | ||
} | ||
} | ||
} |
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,38 @@ | ||
// | ||
// Paths.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 24.10.24. | ||
// | ||
|
||
import SharedModels | ||
import SwiftUI | ||
|
||
public struct FaqPath: Hashable { | ||
public static func == (lhs: FaqPath, rhs: FaqPath) -> Bool { | ||
lhs.hashValue == rhs.hashValue | ||
} | ||
|
||
public let id: Int64 | ||
public let courseId: Int? | ||
public let faq: FaqDTO? | ||
public let namespace: Namespace.ID? | ||
|
||
public init(faq: FaqDTO, namespace: Namespace.ID?) { | ||
self.faq = faq | ||
self.id = faq.id | ||
self.namespace = namespace | ||
self.courseId = nil | ||
} | ||
|
||
public init(id: Int64, courseId: Int, namespace: Namespace.ID? = nil) { | ||
self.id = id | ||
self.courseId = courseId | ||
self.faq = nil | ||
self.namespace = namespace | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
ArtemisKit/Sources/Faq/Resources/en.lproj/Localizable.strings
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,3 @@ | ||
"faqs" = "FAQs"; | ||
"noFaqs" = "No FAQs"; | ||
"readMore" = "Read more"; |
18 changes: 18 additions & 0 deletions
18
ArtemisKit/Sources/Faq/Services/FaqService/FaqService.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,18 @@ | ||
// | ||
// FaqService.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 24.10.24. | ||
// | ||
|
||
import Common | ||
import SharedModels | ||
|
||
protocol FaqService { | ||
func getFaqs(for courseId: Int) async -> DataState<[FaqDTO]> | ||
func getFaq(with faqId: Int64, for courseId: Int) async -> DataState<FaqDTO> | ||
} | ||
|
||
enum FaqServiceFactory { | ||
static let shared: FaqService = FaqServiceImpl() | ||
} |
66 changes: 66 additions & 0 deletions
66
ArtemisKit/Sources/Faq/Services/FaqService/FaqServiceImpl.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,66 @@ | ||
// | ||
// FaqServiceImpl.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 24.10.24. | ||
// | ||
|
||
import APIClient | ||
import Common | ||
import SharedModels | ||
|
||
struct FaqServiceImpl: FaqService { | ||
|
||
let client = APIClient() | ||
|
||
struct GetFaqsRequest: APIRequest { | ||
typealias Response = [FaqDTO] | ||
|
||
let courseId: Int | ||
|
||
var method: HTTPMethod { | ||
return .get | ||
} | ||
|
||
var resourceName: String { | ||
return "api/courses/\(courseId)/faqs" | ||
} | ||
} | ||
|
||
func getFaqs(for courseId: Int) async -> DataState<[FaqDTO]> { | ||
let result = await client.sendRequest(GetFaqsRequest(courseId: courseId)) | ||
|
||
switch result { | ||
case .success((let response, _)): | ||
return .done(response: response) | ||
case .failure(let error): | ||
return .failure(error: UserFacingError(error: error)) | ||
} | ||
} | ||
|
||
struct GetFaqRequest: APIRequest { | ||
typealias Response = FaqDTO | ||
|
||
let courseId: Int | ||
let faqId: Int64 | ||
|
||
var method: HTTPMethod { | ||
return .get | ||
} | ||
|
||
var resourceName: String { | ||
return "api/courses/\(courseId)/faqs/\(faqId)" | ||
} | ||
} | ||
|
||
func getFaq(with faqId: Int64, for courseId: Int) async -> DataState<FaqDTO> { | ||
let result = await client.sendRequest(GetFaqRequest(courseId: courseId, faqId: faqId)) | ||
|
||
switch result { | ||
case .success((let response, _)): | ||
return .done(response: response) | ||
case .failure(let error): | ||
return .failure(error: UserFacingError(error: error)) | ||
} | ||
} | ||
} |
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,46 @@ | ||
// | ||
// FaqViewModel.swift | ||
// ArtemisKit | ||
// | ||
// Created by Anian Schleyer on 24.10.24. | ||
// | ||
|
||
import Common | ||
import Foundation | ||
import SharedModels | ||
|
||
@Observable | ||
class FaqViewModel { | ||
let course: Course | ||
|
||
private let faqService = FaqServiceFactory.shared | ||
var faqs: DataState<[FaqDTO]> = .loading | ||
|
||
var searchText = "" | ||
|
||
init(course: Course) { | ||
self.course = course | ||
} | ||
|
||
func loadFaq() async { | ||
let allFaqs = await faqService.getFaqs(for: course.id) | ||
switch allFaqs { | ||
case .loading: | ||
faqs = .loading | ||
case .failure(let error): | ||
faqs = .failure(error: error) | ||
case .done(let response): | ||
faqs = .done(response: response.filter { $0.faqState == .accepted }) | ||
} | ||
} | ||
} | ||
|
||
// MARK: FAQ+Search | ||
extension FaqViewModel { | ||
var searchResults: [FaqDTO] { | ||
faqs.value?.filter { | ||
$0.questionTitle.localizedStandardContains(searchText) || | ||
$0.questionAnswer.localizedStandardContains(searchText) | ||
} ?? [] | ||
} | ||
} |
Oops, something went wrong.