@@ -72,18 +72,6 @@ class GalleryViewController: UIViewController {
7272 return outofdateView
7373 } ( )
7474
75- private lazy var noGoalsLabel : BSLabel = {
76- let noGoalsLabel = BSLabel ( )
77- noGoalsLabel. accessibilityIdentifier = " noGoalsLabel "
78- noGoalsLabel. text = " You have no Beeminder goals! \n \n You'll need to create one before this app will be any use. "
79- noGoalsLabel. textAlignment = . center
80- noGoalsLabel. numberOfLines = 0
81- noGoalsLabel. isHidden = true
82- // When shown this label should fill all remaining space so it is centered on the screen.
83- noGoalsLabel. setContentHuggingPriority ( UILayoutPriority ( UILayoutPriority . defaultLow. rawValue - 10 ) , for: . vertical)
84- return noGoalsLabel
85- } ( )
86-
8775 private lazy var outofdateLabel : BSLabel = {
8876 let outofdateLabel = BSLabel ( )
8977 outofdateLabel. accessibilityIdentifier = " outofdateLabel "
@@ -222,8 +210,6 @@ class GalleryViewController: UIViewController {
222210 return refreshControl
223211 } ( )
224212
225- self . stackView. addArrangedSubview ( self . noGoalsLabel)
226-
227213 self . updateGoals ( )
228214 self . fetchGoals ( )
229215
@@ -394,16 +380,10 @@ class GalleryViewController: UIViewController {
394380 self . setupHealthKit ( )
395381 self . collectionView. refreshControl? . endRefreshing ( )
396382 MBProgressHUD . hide ( for: self . view, animated: true )
383+ self . collectionView. backgroundView = backgroundViewConsideringContents
397384 self . updateDeadbeatVisibility ( )
398385 self . lastUpdated = Date ( )
399386 self . updateLastUpdatedLabel ( )
400- if self . filteredGoals. isEmpty {
401- self . noGoalsLabel. isHidden = false
402- self . collectionContainer. isHidden = true
403- } else {
404- self . noGoalsLabel. isHidden = true
405- self . collectionContainer. isHidden = false
406- }
407387 let searchItem = UIBarButtonItem ( barButtonSystemItem: . search, target: self , action: #selector( self . searchButtonPressed) )
408388 self . navigationItem. leftBarButtonItem = searchItem
409389 }
@@ -558,3 +538,63 @@ private extension GalleryViewController {
558538 }
559539 }
560540}
541+
542+
543+ // view for the background of the collection view based on its contents
544+ private extension GalleryViewController {
545+ var backgroundViewConsideringContents : UIView ? {
546+ let allGoals = currentUserManager. user ( context: viewContext) ? . goals
547+
548+ return switch ( filteredGoals. isEmpty, allGoals? . isEmpty) {
549+ case ( true , false ) :
550+ self . makeViewForEmptyCollection ( when: . noGoalsMatchingFilter)
551+ case ( true , true ) :
552+ self . makeViewForEmptyCollection ( when: . noActiveGoals)
553+ default :
554+ nil
555+ }
556+ }
557+
558+ enum NoGoalReason : CaseIterable {
559+ case noActiveGoals
560+ case noGoalsMatchingFilter
561+
562+ var message : String {
563+ switch self {
564+ case . noActiveGoals:
565+ """
566+ You have no Beeminder goals!
567+
568+ You'll need to create one before this app will be any use.
569+ """
570+ case . noGoalsMatchingFilter:
571+ """
572+ You have Beeminder goals!
573+
574+ None match the current filter.
575+ """
576+ }
577+ }
578+ }
579+
580+ func makeViewForEmptyCollection( when reason: NoGoalReason ) -> UIView {
581+ let container = UIView ( )
582+
583+ let viewCorrespondingToReason : UIView = {
584+ let label = BSLabel ( )
585+ label. text = reason. message
586+ label. textAlignment = . center
587+ label. numberOfLines = 0
588+ return label
589+ } ( )
590+ container. addSubview ( viewCorrespondingToReason)
591+ container. addSubview ( UIView ( ) )
592+
593+ viewCorrespondingToReason. snp. makeConstraints { make in
594+ make. top. lessThanOrEqualTo ( 100 )
595+ make. horizontalEdges. equalTo ( container)
596+ }
597+
598+ return container
599+ }
600+ }
0 commit comments