Skip to content

Commit

Permalink
Fetch Groups and Users JSON Data
Browse files Browse the repository at this point in the history
  • Loading branch information
aregvarda committed Dec 6, 2021
1 parent ce1d55c commit b3c969a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
31 changes: 25 additions & 6 deletions VKNewsFeed/Newsfeed/NewsfeedPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,46 @@ protocol NewsfeedPresentationLogic {

class NewsfeedPresenter: NewsfeedPresentationLogic {
weak var viewController: NewsfeedDisplayLogic?

let dateFormatter: DateFormatter = {
let dt = DateFormatter()
dt.locale = Locale(identifier: "ru_RU")
dt.dateFormat = "d MMM 'в' HH:mm"
return dt
}()

func presentData(response: Newsfeed.Model.Response.ResponseType) {

switch response {
case .presentNewsfeed(let feed):

let cells = feed.items.map { feedItem in
cellViewModel(from: feedItem)
cellViewModel(from: feedItem, profiles: feed.profiles, groups: feed.groups)
}
let feedViewModel = FeedViewModel.init(cells: cells)
viewController?.displayData(viewModel: Newsfeed.Model.ViewModel.ViewModelData.displayNewsfeed(feedViewModel: feedViewModel))
}
}
private func cellViewModel(from feedItem: FeedItem) -> FeedViewModel.Cell {
return FeedViewModel.Cell.init(iconUrlString: "",
name: "future name",
date: "future date",
private func cellViewModel(from feedItem: FeedItem, profiles: [Profile], groups: [Group]) -> FeedViewModel.Cell {

let profile = self.profile(for: feedItem.sourceId, profiles: profiles, groups: groups)
let date = Date(timeIntervalSince1970: feedItem.date)
let dateTitle = dateFormatter.string(from: date)

return FeedViewModel.Cell.init(iconUrlString: profile.photo,
name: profile.name,
date: dateTitle,
text: feedItem.text,
likes: String(feedItem.likes?.count ?? 0),
comments: String(feedItem.comments?.count ?? 0),
shares: String(feedItem.reposts?.count ?? 0),
views: String(feedItem.views?.count ?? 0))
}
private func profile(for sourceId: Int, profiles: [Profile], groups: [Group]) -> ProfileRepresentable {
let profilesOrGroups: [ProfileRepresentable] = sourceId >= 0 ? profiles : groups
let normalSourceId = sourceId >= 0 ? sourceId : -sourceId
let profileRepresentable = profilesOrGroups.first { myProfileRepresentable in
myProfileRepresentable.id == normalSourceId
}
return profileRepresentable!
}
}
27 changes: 27 additions & 0 deletions VKNewsFeed/Services/Models/FeedResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct FeedResponseWrapped: Decodable {

struct FeedResponse: Decodable {
var items: [FeedItem]
var profiles: [Profile]
var groups: [Group]
}

struct FeedItem: Decodable {
Expand All @@ -29,3 +31,28 @@ struct FeedItem: Decodable {
struct CountableItem: Decodable {
let count: Int
}

protocol ProfileRepresentable {
var id: Int { get }
var name: String { get }
var photo: String { get }
}

struct Profile: Decodable, ProfileRepresentable {

let id: Int
let firstName: String
let lastName: String
let photo100: String

var name: String { return firstName + " " + lastName }
var photo: String { return photo100 }
}

struct Group: Decodable, ProfileRepresentable {
let id: Int
let name: String
let photo100: String

var photo: String { return photo100 }
}

0 comments on commit b3c969a

Please sign in to comment.