Skip to content

Commit 6b6b93d

Browse files
authored
Merge pull request #104 from OlegEremenko991/feature/update-journals
Обновление дневников
2 parents 48ae98e + de99011 commit 6b6b93d

File tree

10 files changed

+56
-42
lines changed

10 files changed

+56
-42
lines changed

SwiftUI-WorkoutApp.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@
12161216
"$(inherited)",
12171217
"@executable_path/Frameworks",
12181218
);
1219-
MARKETING_VERSION = 3.2.2;
1219+
MARKETING_VERSION = 3.3.0;
12201220
PRODUCT_BUNDLE_IDENTIFIER = com.FGU.WorkOut;
12211221
PRODUCT_NAME = WorkoutApp;
12221222
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
@@ -1252,7 +1252,7 @@
12521252
"$(inherited)",
12531253
"@executable_path/Frameworks",
12541254
);
1255-
MARKETING_VERSION = 3.2.2;
1255+
MARKETING_VERSION = 3.3.0;
12561256
PRODUCT_BUNDLE_IDENTIFIER = com.FGU.WorkOut;
12571257
PRODUCT_NAME = WorkoutApp;
12581258
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/Journal/JournalAccess.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@ public enum JournalAccess: Int, CaseIterable, CustomStringConvertible {
2020
}
2121
}
2222

23-
#warning("Сервер возвращает ошибку 404, ждем от Антона уточнений по API")
2423
/// Проверяет возможность создания записи в дневнике
2524
/// - Parameters:
26-
/// - journalOwnerUserId: `id` владельца дневника
25+
/// - journalOwnerId: `id` владельца дневника
2726
/// - journalCommentAccess: Тип доступа для создания записей в дневнике.
2827
/// Настраивается владельцем дневника
2928
/// - mainUserId: `id` авторизованного пользователя в мобильном приложении
3029
/// - mainUserFriendsIds: Список идентификаторов друзей авторизованного пользователя
3130
/// - Returns: `true` - можно создать запись в дневнике, `false` - нельзя
3231
public static func canCreateEntry(
33-
journalOwnerUserId: Int,
32+
journalOwnerId: Int,
3433
journalCommentAccess: Self,
3534
mainUserId: Int?,
3635
mainUserFriendsIds: [Int]
3736
) -> Bool {
38-
let isOwner = journalOwnerUserId == mainUserId
39-
let isFriend = mainUserFriendsIds.contains(journalOwnerUserId)
37+
let isOwner = journalOwnerId == mainUserId
38+
let isFriend = mainUserFriendsIds.contains(journalOwnerId)
4039
switch journalCommentAccess {
4140
case .all:
4241
return mainUserId != nil

SwiftUI-WorkoutApp/SWModels/Sources/SWModels/TextEntryOption.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ public enum TextEntryOption {
44
/// Комментарий к мероприятию
55
case event(id: Int)
66
/// Запись в дневнике
7-
case journal(id: Int)
7+
///
8+
/// - `ownerId` - `id` владельца дневника
9+
/// - `journalId` - `id` дневника
10+
case journal(ownerId: Int, journalId: Int)
811
}

SwiftUI-WorkoutApp/Screens/Common/TextEntry/TextEntryView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct TextEntryView: View {
2222
switch mode {
2323
case let .editGround(info),
2424
let .editEvent(info),
25-
let .editJournalEntry(info):
25+
let .editJournalEntry(_, info):
2626
self.oldEntryText = info.oldEntry
2727
default: break
2828
}
@@ -44,10 +44,10 @@ extension TextEntryView {
4444
enum Mode {
4545
case newForGround(id: Int)
4646
case newForEvent(id: Int)
47-
case newForJournal(id: Int)
47+
case newForJournal(ownerId: Int, journalId: Int)
4848
case editGround(EditInfo)
4949
case editEvent(EditInfo)
50-
case editJournalEntry(EditInfo)
50+
case editJournalEntry(ownerId: Int, editInfo: EditInfo)
5151

5252
struct EditInfo {
5353
let parentObjectID, entryID: Int

SwiftUI-WorkoutApp/Screens/Common/TextEntry/TextEntryViewModel.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ final class TextEntryViewModel: ObservableObject {
1919
isSuccess = try await APIService(with: defaults).addNewEntry(
2020
to: .event(id: id), entryText: entryText
2121
)
22-
case let .newForJournal(id):
22+
case let .newForJournal(ownerId, journalId):
2323
isSuccess = try await APIService(with: defaults).addNewEntry(
24-
to: .journal(id: id), entryText: entryText
24+
to: .journal(ownerId: ownerId, journalId: journalId),
25+
entryText: entryText
2526
)
2627
default: break
2728
}
@@ -48,9 +49,9 @@ final class TextEntryViewModel: ObservableObject {
4849
entryID: info.entryID,
4950
newEntryText: entryText
5051
)
51-
case let .editJournalEntry(info):
52+
case let .editJournalEntry(ownerId, info):
5253
isSuccess = try await APIService(with: defaults).editEntry(
53-
for: .journal(id: info.parentObjectID),
54+
for: .journal(ownerId: ownerId, journalId: info.parentObjectID),
5455
entryID: info.entryID,
5556
newEntryText: entryText
5657
)

SwiftUI-WorkoutApp/Screens/Journals/EntriesList/JournalEntriesList.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ struct JournalEntriesList: View {
4040
deleteClbk: { initiateDeletion(for: item.id) }
4141
),
4242
isNetworkConnected: network.isConnected,
43-
mainUserID: defaults.mainUserInfo?.userID
43+
mainUserID: defaults.mainUserInfo?.userID,
44+
isJournalOwner: viewModel.userID == defaults.mainUserInfo?.userID
4445
)
4546
}
4647
}
@@ -51,7 +52,8 @@ struct JournalEntriesList: View {
5152
.sheet(item: $editEntry) {
5253
TextEntryView(
5354
mode: .editJournalEntry(
54-
.init(
55+
ownerId: viewModel.userID,
56+
editInfo: .init(
5557
parentObjectID: viewModel.currentJournal.id,
5658
entryID: $0.id,
5759
oldEntry: $0.formattedMessage
@@ -99,15 +101,23 @@ private extension JournalEntriesList {
99101

100102
@ViewBuilder
101103
var addEntryButtonIfNeeded: some View {
102-
let isMainUser = viewModel.userID == defaults.mainUserInfo?.userID
103-
if isMainUser {
104+
let canCreateEntry = JournalAccess.canCreateEntry(
105+
journalOwnerId: viewModel.userID,
106+
journalCommentAccess: viewModel.currentJournal.commentAccessType,
107+
mainUserId: defaults.mainUserInfo?.userID,
108+
mainUserFriendsIds: defaults.friendsIdsList
109+
)
110+
if canCreateEntry {
104111
Button(action: showNewEntry) {
105112
Image(systemName: Icons.Regular.plus.rawValue)
106113
}
107114
.disabled(viewModel.isLoading || !network.isConnected)
108115
.sheet(isPresented: $showEntrySheet) {
109116
TextEntryView(
110-
mode: .newForJournal(id: viewModel.currentJournal.id),
117+
mode: .newForJournal(
118+
ownerId: viewModel.userID,
119+
journalId: viewModel.currentJournal.id
120+
),
111121
refreshClbk: updateEntries
112122
)
113123
}

SwiftUI-WorkoutApp/Screens/Journals/EntriesList/JournalEntriesListViewModel.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ final class JournalEntriesListViewModel: ObservableObject {
4343
guard let entryID, !isLoading else { return }
4444
isLoading.toggle()
4545
do {
46-
if try await APIService(with: defaults).deleteEntry(from: .journal(id: currentJournal.id), entryID: entryID) {
46+
if try await APIService(with: defaults).deleteEntry(
47+
from: .journal(ownerId: userID, journalId: currentJournal.id),
48+
entryID: entryID
49+
) {
4750
list.removeAll(where: { $0.id == entryID })
4851
}
4952
} catch {

SwiftUI-WorkoutApp/Screens/Journals/JournalCell.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct JournalCell: View {
88
let mode: Mode
99
let isNetworkConnected: Bool
1010
let mainUserID: Int?
11+
let isJournalOwner: Bool
1112

1213
var body: some View {
1314
JournalRowView(
@@ -46,22 +47,24 @@ private extension JournalCell {
4647
case let .root(setupClbk, deleteClbk):
4748
if isEntryByMainUser {
4849
var array = [OptionButton]()
49-
array.append(OptionButton(.setup, action: setupClbk))
50-
array.append(OptionButton(.delete, action: deleteClbk))
50+
array.append(.init(.setup, action: setupClbk))
51+
array.append(.init(.delete, action: deleteClbk))
5152
return array
5253
} else {
5354
return []
5455
}
5556
case let .entry(editClbk, reportClbk, canDelete, deleteClbk):
5657
if isEntryByMainUser {
5758
var array = [OptionButton]()
58-
array.append(OptionButton(.edit, action: editClbk))
59+
array.append(.init(.edit, action: editClbk))
5960
if canDelete {
60-
array.append(OptionButton(.delete, action: deleteClbk))
61+
array.append(.init(.delete, action: deleteClbk))
6162
}
6263
return array
64+
} else if isJournalOwner {
65+
return canDelete ? [.init(.delete, action: deleteClbk)] : []
6366
} else {
64-
return [OptionButton(.report, action: reportClbk)]
67+
return [.init(.report, action: reportClbk)]
6568
}
6669
}
6770
}
@@ -74,7 +77,8 @@ struct JournalCell_Previews: PreviewProvider {
7477
model: .init(journalEntryResponse: .preview),
7578
mode: .root(setupClbk: {}, deleteClbk: {}),
7679
isNetworkConnected: true,
77-
mainUserID: nil
80+
mainUserID: nil,
81+
isJournalOwner: true
7882
)
7983
.previewLayout(.sizeThatFits)
8084
}

SwiftUI-WorkoutApp/Screens/Journals/JournalsList/JournalsListView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ private extension JournalsListView {
104104
deleteClbk: { initiateDeletion(for: journal.id) }
105105
),
106106
isNetworkConnected: network.isConnected,
107-
mainUserID: defaults.mainUserInfo?.userID
107+
mainUserID: defaults.mainUserInfo?.userID,
108+
isJournalOwner: journal.ownerID == defaults.mainUserInfo?.userID
108109
)
109110
}
110111
}

SwiftUI-WorkoutApp/Services/APIService.swift

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,8 @@ struct APIService {
290290
endpoint = .addCommentToSportsGround(groundID: id, comment: entryText)
291291
case let .event(id):
292292
endpoint = .addCommentToEvent(eventID: id, comment: entryText)
293-
case let .journal(id):
294-
guard let mainUserID = await defaults.mainUserInfo?.userID else { throw APIError.invalidUserID }
295-
endpoint = .saveJournalEntry(userID: mainUserID, journalID: id, message: entryText)
293+
case let .journal(ownerId, journalId):
294+
endpoint = .saveJournalEntry(userID: ownerId, journalID: journalId, message: entryText)
296295
}
297296
return try await makeStatus(for: endpoint.urlRequest(with: baseUrlString))
298297
}
@@ -318,13 +317,10 @@ struct APIService {
318317
commentID: entryID,
319318
newComment: newEntryText
320319
)
321-
case let .journal(id):
322-
guard let mainUserID = await defaults.mainUserInfo?.userID else {
323-
throw APIError.invalidUserID
324-
}
320+
case let .journal(ownerId, journalId):
325321
endpoint = .editEntry(
326-
userID: mainUserID,
327-
journalID: id,
322+
userID: ownerId,
323+
journalID: journalId,
328324
entryID: entryID,
329325
newEntryText: newEntryText
330326
)
@@ -344,11 +340,8 @@ struct APIService {
344340
endpoint = .deleteGroundComment(id, commentID: entryID)
345341
case let .event(id):
346342
endpoint = .deleteEventComment(id, commentID: entryID)
347-
case let .journal(id):
348-
guard let mainUserID = await defaults.mainUserInfo?.userID else {
349-
throw APIError.invalidUserID
350-
}
351-
endpoint = .deleteEntry(userID: mainUserID, journalID: id, entryID: entryID)
343+
case let .journal(ownerId, journalId):
344+
endpoint = .deleteEntry(userID: ownerId, journalID: journalId, entryID: entryID)
352345
}
353346
return try await makeStatus(for: endpoint.urlRequest(with: baseUrlString))
354347
}

0 commit comments

Comments
 (0)