Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def wordpress_ui
end

def wordpress_kit
pod 'WordPressKit', '~> 2.1.0'
#pod 'WordPressKit', :git => 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', :commit => '3886f1d'
pod 'WordPressKit', '~> 2.1.1-beta.2'
#pod 'WordPressKit', :git => 'https://github.com/wordpress-mobile/WordPressKit-iOS.git', :commit => '9a41727'
#pod 'WordPressKit', :path => '~/Developer/a8c/WordPressKit-iOS'
end

Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ PODS:
- WordPressKit (~> 2.0-beta)
- WordPressShared (~> 1.4)
- WordPressUI (~> 1.0)
- WordPressKit (2.1.0):
- WordPressKit (2.1.1-beta.2):
- Alamofire (~> 4.7.3)
- CocoaLumberjack (= 3.4.2)
- NSObject-SafeExpectations (= 0.0.3)
Expand Down Expand Up @@ -249,7 +249,7 @@ DEPENDENCIES:
- SVProgressHUD (= 2.2.5)
- WordPress-Editor-iOS (= 1.4.3)
- WordPressAuthenticator (~> 1.1.9)
- WordPressKit (~> 2.1.0)
- WordPressKit (~> 2.1.1-beta.2)
- WordPressShared (= 1.7.1)
- WordPressUI (from `https://github.com/wordpress-mobile/WordPressUI-iOS.git`, tag `1.2.0`)
- WPMediaPicker (= 1.3.2)
Expand Down Expand Up @@ -387,14 +387,14 @@ SPEC CHECKSUMS:
WordPress-Aztec-iOS: 0062ba96c3ea77186590c74b761427c461f89a06
WordPress-Editor-iOS: cfaa780c3ee64ea781fe6f52ca1561667a3f3707
WordPressAuthenticator: 9559bc45373f1b6c2f58d664d34f564bcf73111f
WordPressKit: 1296ce8f8b85423ddf0d993deac918e75c5b6896
WordPressKit: 857a220f743f7c901204d8a9faf14d857e96370c
WordPressShared: 2018257df3fe1de423f31a3ba50ac8d6bd24941c
WordPressUI: 44fe43a9c5c504dfd534286e39e1ce6ebcd69ff5
WPMediaPicker: e50edd8f30f5d87288840941ef3ff9cd11860937
wpxmlrpc: 6ba55c773cfa27083ae4a2173e69b19f46da98e2
yoga: f37b1edbd68be803f1dc4d57d40d8a5b277d8e2c
ZendeskSDK: 44ee00338dd718495f0364369420ae11b389c878

PODFILE CHECKSUM: 2c1688ad9f328bb556211b9739772c7300173c1f
PODFILE CHECKSUM: 43c5c45f6d7ee78c09c2cf6f41eb5724d335fd17

COCOAPODS: 1.5.3
101 changes: 47 additions & 54 deletions WordPress/Classes/Stores/StatsInsightsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum InsightAction: Action {
case receivedPublicize(_ publicizeStats: StatsPublicizeInsight?)
case receivedCommentsInsight(_ commentsInsight: StatsCommentsInsight?)
case receivedTodaysStats(_ todaysStats: StatsTodayInsight?)
case receivedPostingActivity(_ postingActivity: StatsStreak?)
case receivedPostingActivity(_ postingActivity: StatsPostingStreakInsight?)
case receivedTagsAndCategories(_ tagsAndCategories: StatsTagsAndCategoriesInsight?)
case refreshInsights()
}
Expand Down Expand Up @@ -46,7 +46,7 @@ struct InsightStoreState {
var todaysStats: StatsTodayInsight?
var fetchingTodaysStats = false

var postingActivity: StatsStreak?
var postingActivity: StatsPostingStreakInsight?
var fetchingPostingActivity = false

var topTagsAndCategories: StatsTagsAndCategoriesInsight?
Expand Down Expand Up @@ -182,27 +182,13 @@ private extension StatsInsightsStore {
self.actionDispatcher.dispatch(InsightAction.receivedTagsAndCategories(tagsAndCategoriesInsight))
}

SiteStatsInformation.statsService()?.retrieveInsightsStats(
allTimeStatsCompletionHandler: nil,
insightsCompletionHandler: nil,
todaySummaryCompletionHandler: nil,
latestPostSummaryCompletionHandler: nil,
commentsAuthorCompletionHandler: nil,
commentsPostsCompletionHandler: nil,
tagsCategoriesCompletionHandler: nil,
followersDotComCompletionHandler: nil,
followersEmailCompletionHandler: nil,
publicizeCompletionHandler: nil,
streakCompletionHandler: { (statsStreak, error) in
api.getInsight { (streak: StatsPostingStreakInsight?, error) in
if error != nil {
DDLogInfo("Error fetching stats streak: \(String(describing: error?.localizedDescription))")
DDLogInfo("Error fetching tags and categories insight: \(String(describing: error?.localizedDescription))")
}
self.actionDispatcher.dispatch(InsightAction.receivedPostingActivity(statsStreak))
}, progressBlock: { (numberOfFinishedOperations, totalNumberOfOperations) in

}, andOverallCompletionHandler: {

})
self.actionDispatcher.dispatch(InsightAction.receivedPostingActivity(streak))
}
}

func refreshInsights() {
Expand Down Expand Up @@ -270,7 +256,7 @@ private extension StatsInsightsStore {
}
}

func receivedPostingActivity(_ postingActivity: StatsStreak?) {
func receivedPostingActivity(_ postingActivity: StatsPostingStreakInsight?) {
transaction { state in
state.postingActivity = postingActivity
state.fetchingPostingActivity = false
Expand Down Expand Up @@ -343,57 +329,64 @@ extension StatsInsightsStore {
return state.topTagsAndCategories
}

func getPostingActivity() -> StatsPostingStreakInsight? {
return state.postingActivity
}
/// Summarizes the daily posting count for the month in the given date.
/// Returns an array containing every day of the month and associated post count.
///
func getMonthlyPostingActivityFor(date: Date) -> [PostingActivityDayData] {

var monthData = [PostingActivityDayData]()
let dateComponents = Calendar.current.dateComponents([.year, .month], from: date.normalizedDate())
func getMonthlyPostingActivityFor(date: Date) -> [PostingStreakEvent] {

// Add every day in the month to the array, seeding with 0 counts.
guard let dayRange = Calendar.current.range(of: .day, in: .month, for: date) else {
return monthData
guard
let postingEvents = state.postingActivity?.postingEvents,
postingEvents.count > 0
else {
return []
}

dayRange.forEach { day in
let components = DateComponents(year: dateComponents.year, month: dateComponents.month, day: day)
guard let date = Calendar.current.date(from: components) else {
return
}
let calendar = Calendar.autoupdatingCurrent
let components = calendar.dateComponents([.month, .year], from: date)

monthData.append(PostingActivityDayData(date: date, count: 0))
guard
let month = components.month,
let year = components.year
else {
return []
}

// If there is no posting activity at all, return.
guard let allPostingActivity = state.postingActivity?.items else {
return monthData
}
// This gives a range of how many days there are in a given month...
let rangeOfMonth = calendar.range(of: .day, in: .month, for: date) ?? 0..<0

// If the posting occurred in the requested month, increment the count for that day.
allPostingActivity.forEach { postingActivity in
let postDate = postingActivity.date.normalizedDate()
if let dayIndex = monthData.index(where: { $0.date == postDate }) {
monthData[dayIndex].count += 1
let mappedMonth = rangeOfMonth
// then we create a `Date` representing each of those days
.compactMap {
return calendar.date(from: DateComponents(year: year, month: month, day: $0))
}
// and pick out a relevant `PostingStreakEvent` from data we have or return
// an empty one.
.map { (date: Date) -> PostingStreakEvent in
if let postingEvent = postingEvents.first(where: { event in return event.date == date }) {
return postingEvent
}
return PostingStreakEvent(date: date, postCount: 0)
}

return monthData
}
return mappedMonth

func getYearlyPostingActivityFrom(date: Date) -> [[PostingActivityDayData]] {
var monthsData = [[PostingActivityDayData]]()
}

// Get last 12 months, in ascending order.
for month in (0...11).reversed() {
if let monthDate = Calendar.current.date(byAdding: .month, value: -month, to: Date()) {
monthsData.append(getMonthlyPostingActivityFor(date: monthDate))
func getYearlyPostingActivityFrom(date: Date) -> [[PostingStreakEvent]] {
// We operate on a "reversed" range since we want least-recent months first.
return (0...11).reversed().compactMap {
guard
let monthDate = Calendar.current.date(byAdding: .month, value: -$0, to: date)
else {
return nil
}
}

return monthsData
return getMonthlyPostingActivityFor(date: monthDate)
}
}

var isFetching: Bool {
return
state.fetchingLastPostInsight ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PostingActivityCell: UITableViewCell, NibLoadable {
addLegend()
}

func configure(withData monthsData: [[PostingActivityDayData]], andDelegate delegate: SiteStatsInsightsDelegate) {
func configure(withData monthsData: [[PostingStreakEvent]], andDelegate delegate: SiteStatsInsightsDelegate) {
siteStatsInsightsDelegate = delegate
addMonths(monthsData: monthsData)
}
Expand All @@ -49,7 +49,7 @@ private extension PostingActivityCell {
legendView.addSubview(legend)
}

func addMonths(monthsData: [[PostingActivityDayData]]) {
func addMonths(monthsData: [[PostingStreakEvent]]) {
for monthData in monthsData {
let monthView = PostingActivityMonth.loadFromNib()
monthView.configure(monthData: monthData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PostingActivityCollectionViewCell: UICollectionViewCell {

// MARK: - Configure

func configure(withData monthData: [PostingActivityDayData], postingActivityDayDelegate: PostingActivityDayDelegate? = nil) {
func configure(withData monthData: [PostingStreakEvent], postingActivityDayDelegate: PostingActivityDayDelegate? = nil) {
let monthView = PostingActivityMonth.loadFromNib()
monthView.configure(monthData: monthData, postingActivityDayDelegate: postingActivityDayDelegate)
monthView.frame.size = frame.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ protocol PostingActivityDayDelegate: class {
func daySelected(_ day: PostingActivityDay)
}

/// Convenience struct to contain information about a single day displayed in Posting Activity.
///
struct PostingActivityDayData {
var date: Date
var count: Int
}

class PostingActivityDay: UIView, NibLoadable {

// MARK: - Properties
Expand All @@ -20,11 +13,11 @@ class PostingActivityDay: UIView, NibLoadable {

private var visible = true
private var active = true
private(set) var dayData: PostingActivityDayData?
private(set) var dayData: PostingStreakEvent?

// MARK: - Configure

func configure(dayData: PostingActivityDayData? = nil, delegate: PostingActivityDayDelegate? = nil) {
func configure(dayData: PostingStreakEvent? = nil, delegate: PostingActivityDayDelegate? = nil) {
self.dayData = dayData
visible = dayData != nil
active = delegate != nil
Expand All @@ -51,7 +44,7 @@ private extension PostingActivityDay {
return .clear
}

return PostingActivityLegend.colorForCount(dayData.count)
return PostingActivityLegend.colorForCount(dayData.postCount)
}

@IBAction func dayButtonPressed(_ sender: UIButton) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PostingActivityMonth: UIView, NibLoadable {
@IBOutlet weak var viewWidthConstraint: NSLayoutConstraint!

private var month: Date?
private var monthData: [PostingActivityDayData]?
private var monthData: [PostingStreakEvent]?
private weak var postingActivityDayDelegate: PostingActivityDayDelegate?

// 14 = day width (12) + column margin (2).
Expand All @@ -18,7 +18,7 @@ class PostingActivityMonth: UIView, NibLoadable {

// MARK: - Configure

func configure(monthData: [PostingActivityDayData], postingActivityDayDelegate: PostingActivityDayDelegate? = nil) {
func configure(monthData: [PostingStreakEvent], postingActivityDayDelegate: PostingActivityDayDelegate? = nil) {
self.monthData = monthData
self.postingActivityDayDelegate = postingActivityDayDelegate
getMonth()
Expand Down Expand Up @@ -97,7 +97,7 @@ private extension PostingActivityMonth {
toggleLastStackView(lastStackViewUsed: weekIndex - 1)
}

func addDayToStackView(stackView: UIStackView, dayData: PostingActivityDayData? = nil) {
func addDayToStackView(stackView: UIStackView, dayData: PostingStreakEvent? = nil) {
let dayView = PostingActivityDay.loadFromNib()
dayView.configure(dayData: dayData, delegate: postingActivityDayDelegate)
stackView.addArrangedSubview(dayView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PostingActivityViewController: UIViewController, StoryboardLoadable {
@IBOutlet weak var postCountLabel: UILabel!
@IBOutlet weak var legendView: UIView!

var yearData = [[PostingActivityDayData]]()
var yearData = [[PostingStreakEvent]]()

private var selectedDay: PostingActivityDay?
private typealias Style = WPStyleGuide.Stats
Expand Down Expand Up @@ -82,7 +82,7 @@ extension PostingActivityViewController: PostingActivityDayDelegate {

dayDataView.isHidden = false
dateLabel.text = formattedDate(dayData.date)
postCountLabel.text = formattedPostCount(dayData.count)
postCountLabel.text = formattedPostCount(dayData.postCount)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private extension SiteStatsInsightsViewModel {
}

func createPostingActivityRow() -> PostingActivityRow {
var monthsData = [[PostingActivityDayData]]()
var monthsData = [[PostingStreakEvent]]()

if let twoMonthsAgo = Calendar.current.date(byAdding: .month, value: -2, to: Date()) {
monthsData.append(store.getMonthlyPostingActivityFor(date: twoMonthsAgo))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct PostingActivityRow: ImmuTableRow {
return ImmuTableCell.nib(CellType.defaultNib, CellType.self)
}()

let monthsData: [[PostingActivityDayData]]
let monthsData: [[PostingStreakEvent]]
let siteStatsInsightsDelegate: SiteStatsInsightsDelegate
let action: ImmuTableAction? = nil

Expand Down