1

Beginner programmer here.

I am trying to show two different color dots if there is a negative AND a positive transaction for that date.

Here is my code. It only shows one dot.

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
        
        let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
        
        let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
        
        for _ in incomeTransaction {
            return 1
        }
        for _ in expenseTransaction {
            return 1
        }
        return 0
    }

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
        
        let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
        
        let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
        
        for _ in incomeTransaction {
            return [UIColor(rgb: Constants.green)]
        }
        for _ in expenseTransaction {
            return [UIColor(rgb: Constants.red)]
        }
        return nil
    }

single dot

This code shows two dots, but they are the same color:

    func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
        
        let incomeTransaction = realm.objects(Transaction.self).filter(positiveTransactionPredicate(date: date))
        
        let expenseTransaction = realm.objects(Transaction.self).filter(negativeTransactionPredicate(date: date))
            
        for _ in incomeTransaction {
            return 2
        }
        for _ in expenseTransaction {
            return 2
        }
        
        return 0
    }

two dots

How do you tell the FSCalendar delegate methods to show two different color dots on a date?

Like This:

two different dots

Thanks.

BriScoLeg
  • 105
  • 1
  • 9
  • It's not clear what you asking or what the actual issue is. Are you asking how to display two dots in general or how to read data from Realm or peform a calculation or what exactly? Can you update and clarify the question and tell us specifically what the issue is? – Jay Jul 22 '20 at 17:34
  • @jay I've updated the question. I just want some example code that shows two different color dots for a date. I should be able to figure out everything if I can just see how to do that. Thanks. – BriScoLeg Jul 22 '20 at 18:43

1 Answers1

0

It doesn't appear that numberOfEventsForDate:(NSDate *)date; is correctly implemented. You're returning 1 which results in 1 dot

Returning 1 will show 1 dot, 0 will show 0 dots and 2 will display two dots. From the example project

/**
 * Asks the dataSource the number of event dots for a specific date.
 *
 * @see
 *   - (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventColorForDate:(NSDate *)date;
 *   - (NSArray *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventColorsForDate:(NSDate *)date;
 */
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date;

I would suggest taking a look at the examples on the github repo for FSCalendar.

The objc implementation is this

//return 2 for 2 events
cell.numberOfEvents = [self.dataSourceProxy calendar:self numberOfEventsForDate:date]; 

If you always want two dots, regardless of the actual number of events, just return 2

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
   return 2
}

Download the Example.swift project and see the FSCalendarSwiftExample.xcodeproj

Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thank you for your collaboration. I understand how to create two dots. I even provided an example code how to do that in my original question. My question is not how to create two dots, it is how to create two DIFFERENT COLOR dots. – BriScoLeg Jul 24 '20 at 18:05
  • I think you misunderstood - probably because what I said was unclear. The delegate method `calendar(_ calendar:appearance: FSCalendarAppearance:eventDefaultColorsFor` is called for each date. If the date passed in is a date there's an event return the color you want it do be. i.e. if there's an event on the 1st and 2nd, check to see if the passed in date is in dateArray1 and return `return [UIColor.blue]`. If the date is in dateArray2, `return [UIColor.green]` Along with that the dataSource method `alendar(_ calendar: numberOfEventsFor` indicates the dot count – Jay Jul 24 '20 at 18:26