Added a progress tracker to the bug report view (#513)

* created a progress tracker class and passed it in the user notification to be observed by the progress view

* improved the publishing by dispatching it on RunLoop.main

* bug report struct created and progress tracker class moved into the Other folder

* some swiftlint adjustments

* fixed tests

* fixed another test BugReportServiceTests

* changelog 495 - change

* added a mock preview

* fixing some linting suggestions

* no need to use KVO, achieve the same result using a publisher

* some refactors to address PR comments

* some code improvements

* fixed the issue that prevented the avatar of the room to be displayed in the mocks, and updated the tests

* Revert "fixed the issue that prevented the avatar of the room to be displayed in the mocks, and updated the tests"

This reverts commit 113d6091d91a3aac1f9a59ff6c5e07610ed59859.
This commit is contained in:
Mauro
2023-02-08 21:52:31 +01:00
committed by GitHub
parent 4d87701ae9
commit e5522e7753
12 changed files with 168 additions and 71 deletions
@@ -0,0 +1,39 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Combine
import Foundation
protocol ProgressListener {
var progressSubject: CurrentValueSubject<Double, Never> { get }
}
protocol ProgressPublisher {
var publisher: AnyPublisher<Double, Never> { get }
}
final class ProgressTracker: ProgressListener, ProgressPublisher {
let progressSubject: CurrentValueSubject<Double, Never>
var publisher: AnyPublisher<Double, Never> {
progressSubject
.eraseToAnyPublisher()
}
init(initialValue: Double = 0.0) {
progressSubject = .init(initialValue)
}
}
@@ -22,9 +22,18 @@ enum UserNotificationType {
}
struct UserNotification: Equatable, Identifiable {
static func == (lhs: UserNotification, rhs: UserNotification) -> Bool {
lhs.id == rhs.id &&
lhs.type == rhs.type &&
lhs.title == rhs.title &&
lhs.iconName == rhs.iconName &&
lhs.persistent == rhs.persistent
}
var id: String = UUID().uuidString
var type = UserNotificationType.toast
var title: String
var iconName: String?
var persistent = false
var progressPublisher: ProgressPublisher?
}
@@ -14,16 +14,22 @@
// limitations under the License.
//
import Combine
import SwiftUI
struct UserNotificationModalView: View {
let notification: UserNotification
@State private var progressFraction: Double?
var body: some View {
ZStack {
VStack(spacing: 12.0) {
ProgressView()
if let progressFraction = progressFraction {
ProgressView(value: progressFraction)
} else {
ProgressView()
}
HStack {
if let iconName = notification.iconName {
Image(systemName: iconName)
@@ -34,10 +40,13 @@ struct UserNotificationModalView: View {
}
}
.padding()
.frame(minWidth: 150.0)
.frame(minWidth: 150.0, maxWidth: 250.0)
.background(Color.element.quinaryContent)
.clipShape(RoundedCornerShape(radius: 12.0, corners: .allCorners))
.shadow(color: .black.opacity(0.1), radius: 10.0, y: 4.0)
.onReceive(notification.progressPublisher?.publisher ?? Empty().eraseToAnyPublisher()) { progress in
progressFraction = progress
}
.transition(.opacity)
}
.id(notification.id)
@@ -45,21 +54,22 @@ struct UserNotificationModalView: View {
.background(.black.opacity(0.1))
.ignoresSafeArea()
}
private var toastTransition: AnyTransition {
AnyTransition
.asymmetric(insertion: .move(edge: .top),
removal: .move(edge: .bottom))
.combined(with: .opacity)
}
}
struct UserNotificationModalView_Previews: PreviewProvider {
static var previews: some View {
VStack {
Group {
UserNotificationModalView(notification: UserNotification(type: .modal,
title: "Successfully logged in",
iconName: "checkmark"))
iconName: "checkmark")
)
.previewDisplayName("Spinner")
UserNotificationModalView(notification: UserNotification(type: .modal,
title: "Successfully logged in",
iconName: "checkmark",
progressPublisher: ProgressTracker(initialValue: 0.5))
)
.previewDisplayName("Progress Bar")
}
}
}
@@ -51,8 +51,8 @@ final class BugReportCoordinator: CoordinatorProtocol {
switch result {
case .cancel:
self.completion?(.cancel)
case .submitStarted:
self.startLoading()
case let .submitStarted(progressTracker):
self.startLoading(progressPublisher: progressTracker)
case .submitFinished:
self.stopLoading()
self.completion?(.finish)
@@ -75,11 +75,14 @@ final class BugReportCoordinator: CoordinatorProtocol {
static let loadingIndicatorIdentifier = "BugReportLoading"
private func startLoading(label: String = ElementL10n.loading) {
parameters.userNotificationController?.submitNotification(UserNotification(id: Self.loadingIndicatorIdentifier,
type: .modal,
title: label,
persistent: true))
private func startLoading(label: String = ElementL10n.loading, progressPublisher: ProgressPublisher) {
parameters.userNotificationController?.submitNotification(
UserNotification(id: Self.loadingIndicatorIdentifier,
type: .modal,
title: label,
persistent: true,
progressPublisher: progressPublisher)
)
}
private func stopLoading() {
@@ -19,7 +19,7 @@ import UIKit
enum BugReportViewModelAction {
case cancel
case submitStarted
case submitStarted(progressTracker: ProgressTracker)
case submitFinished
case submitFailed(error: Error)
}
@@ -49,13 +49,16 @@ class BugReportViewModel: BugReportViewModelType, BugReportViewModelProtocol {
// MARK: Private
private func submitBugReport() async {
callback?(.submitStarted)
let progressTracker = ProgressTracker()
callback?(.submitStarted(progressTracker: progressTracker))
do {
let result = try await bugReportService.submitBugReport(text: context.reportText,
includeLogs: context.sendingLogsEnabled,
includeCrashLog: true,
githubLabels: [],
files: [])
let bugReport = BugReport(text: context.reportText,
includeLogs: context.sendingLogsEnabled,
includeCrashLog: true,
githubLabels: [],
files: [])
let result = try await bugReportService.submitBugReport(bugReport,
progressListener: progressTracker)
MXLog.info("SubmitBugReport succeeded, result: \(result.reportUrl)")
callback?(.submitFinished)
} catch {
@@ -14,17 +14,20 @@
// limitations under the License.
//
import Combine
import Foundation
import GZIP
import Sentry
import UIKit
class BugReportService: BugReportServiceProtocol {
class BugReportService: NSObject, BugReportServiceProtocol {
private let baseURL: URL
private let sentryURL: URL
private let applicationId: String
private let session: URLSession
private var lastCrashEventId: String?
private let progressSubject = PassthroughSubject<Double, Never>()
private var cancellables = Set<AnyCancellable>()
init(withBaseURL baseURL: URL,
sentryURL: URL,
@@ -34,6 +37,7 @@ class BugReportService: BugReportServiceProtocol {
self.sentryURL = sentryURL
self.applicationId = applicationId
self.session = session
super.init()
// enable SentrySDK
SentrySDK.start { options in
@@ -74,18 +78,15 @@ class BugReportService: BugReportServiceProtocol {
SentrySDK.crash()
}
func submitBugReport(text: String,
includeLogs: Bool,
includeCrashLog: Bool,
githubLabels: [String],
files: [URL]) async throws -> SubmitBugReportResponse {
var params = [MultipartFormData(key: "text", type: .text(value: text))]
func submitBugReport(_ bugReport: BugReport,
progressListener: ProgressListener?) async throws -> SubmitBugReportResponse {
var params = [MultipartFormData(key: "text", type: .text(value: bugReport.text))]
params.append(contentsOf: defaultParams)
for label in githubLabels {
for label in bugReport.githubLabels {
params.append(MultipartFormData(key: "label", type: .text(value: label)))
}
let zippedFiles = try await zipFiles(includeLogs: includeLogs,
includeCrashLog: includeCrashLog)
let zippedFiles = try await zipFiles(includeLogs: bugReport.includeLogs,
includeCrashLog: bugReport.includeCrashLog)
// log or compressed-log
if !zippedFiles.isEmpty {
for url in zippedFiles {
@@ -97,24 +98,14 @@ class BugReportService: BugReportServiceProtocol {
params.append(MultipartFormData(key: "crash_report", type: .text(value: "<https://sentry.tools.element.io/organizations/element/issues/?project=44&query=\(crashEventId)>")))
}
for url in files {
for url in bugReport.files {
params.append(MultipartFormData(key: "file", type: .file(url: url)))
}
let boundary = "Boundary-\(UUID().uuidString)"
var body = Data()
for param in params {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition:form-data; name=\"\(param.key)\"")
switch param.type {
case .text(let value):
body.appendString(string: "\r\n\r\n\(value)\r\n")
case .file(let url):
body.appendString(string: "; filename=\"\(url.lastPathComponent)\"\r\n")
body.appendString(string: "Content-Type: \"content-type header\"\r\n\r\n")
body.append(try Data(contentsOf: url))
body.appendString(string: "\r\n")
}
try body.appendParam(param, boundary: boundary)
}
body.appendString(string: "--\(boundary)--\r\n")
@@ -124,7 +115,16 @@ class BugReportService: BugReportServiceProtocol {
request.httpMethod = "POST"
request.httpBody = body as Data
let (data, _) = try await session.data(for: request)
let data: Data
if let progressListener {
progressSubject
.receive(on: DispatchQueue.main)
.assign(to: \.value, on: progressListener.progressSubject)
.store(in: &cancellables)
(data, _) = try await session.data(for: request, delegate: self)
} else {
(data, _) = try await session.data(for: request)
}
// Parse the JSON data
let decoder = JSONDecoder()
@@ -223,6 +223,20 @@ private extension Data {
append(data)
}
}
mutating func appendParam(_ param: MultipartFormData, boundary: String) throws {
appendString(string: "--\(boundary)\r\n")
appendString(string: "Content-Disposition:form-data; name=\"\(param.key)\"")
switch param.type {
case .text(let value):
appendString(string: "\r\n\r\n\(value)\r\n")
case .file(let url):
appendString(string: "; filename=\"\(url.lastPathComponent)\"\r\n")
appendString(string: "Content-Type: \"content-type header\"\r\n\r\n")
append(try Data(contentsOf: url))
appendString(string: "\r\n")
}
}
}
private struct MultipartFormData {
@@ -234,3 +248,13 @@ private enum MultipartFormDataType {
case text(value: String)
case file(url: URL)
}
extension BugReportService: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, didCreateTask task: URLSessionTask) {
task.progress.publisher(for: \.fractionCompleted)
.sink { [weak self] value in
self?.progressSubject.send(value)
}
.store(in: &cancellables)
}
}
@@ -17,6 +17,14 @@
import Foundation
import UIKit
struct BugReport {
let text: String
let includeLogs: Bool
let includeCrashLog: Bool
let githubLabels: [String]
let files: [URL]
}
struct SubmitBugReportResponse: Decodable {
var reportUrl: String
}
@@ -26,9 +34,6 @@ protocol BugReportServiceProtocol {
func crash()
func submitBugReport(text: String,
includeLogs: Bool,
includeCrashLog: Bool,
githubLabels: [String],
files: [URL]) async throws -> SubmitBugReportResponse
func submitBugReport(_ bugReport: BugReport,
progressListener: ProgressListener?) async throws -> SubmitBugReportResponse
}
@@ -18,11 +18,8 @@ import Foundation
import UIKit
class MockBugReportService: BugReportServiceProtocol {
func submitBugReport(text: String,
includeLogs: Bool,
includeCrashLog: Bool,
githubLabels: [String],
files: [URL]) async throws -> SubmitBugReportResponse {
func submitBugReport(_ bugReport: BugReport,
progressListener: ProgressListener?) async throws -> SubmitBugReportResponse {
SubmitBugReportResponse(reportUrl: "https://www.example/com/123")
}