Hide empty section in Compositional Layout Collection View with DiffableDataSource not working - collectionview

I have three sections of data on screen: one for Five Star Items, one for Visits, and one for Other Fun Places items. They are all of the same type: funPlaces. For this example, I'm creating three arrays of funPlaces and showing them in three sections.
What I'd like to do is not generate a section on-screen at all if there is no data available for that section. So if there is no Visits data, I only want to see the sections for Five Star Items and Other Fun Places items. If there are no Five Star Items, I only want to see the sections for Visits and Other Fun Places items, etc.
However...
What's happening is that if there are no Visits items for example, rather than the Visits section disappearing as I would expect, instead the data from otherFunPlaces gets shown in the Visits section on-screen and the entire Other Fun Places section disappears. Yes, the Visits data is gone, but the section is still there and its data has been replaced with Other Fun Places data on-screen.
If I have an empty Five Star Items array, the Five Star Items section is still shown but the data has been replaced with Visits data on-screen. And the Other Fun Places data shifts up a section to the Visits Section--it's the Other Fun Places section that actually disappears on-screen.
The only case that works is if I have an empty Other Fun Items array--the section disappears as expected, and the data in the two sections above is also in its proper place on-screen.
What am I missing to make this work?
func applySnapshot(animatingDifferences: Bool = true) {
let funPlaces = fetchedResultsController.fetchedObjects ?? []
if funPlaces.count > 6 {
// we only generated enough sample data objects in viewDidLoad for this test
// we're going to make sure there are no fiveStarItems in the array for this test
// let fiveStarItems = Array<FunPlace>(funPlaces[0...1])
let fiveStarItems: [FunPlace] = [] // hide fiveStarItems
// we're going to keep data in visitsItems and otherItems
let visitsItems = Array<FunPlace>(funPlaces[2...3])
let otherFunPlacesItems = Array<FunPlace>(funPlaces[4...5])
var snapshot = NSDiffableDataSourceSnapshot<Sections, FunPlace>()
if !fiveStarItems.isEmpty {
snapshot.appendSections([Sections.fiveStar])
snapshot.appendItems(fiveStarItems, toSection: .fiveStar)
}
if !visitsItems.isEmpty {
snapshot.appendSections([Sections.visits])
snapshot.appendItems(visitsItems, toSection: .visits)
}
if !otherFunPlacesItems.isEmpty {
snapshot.appendSections([Sections.otherFunPlaces])
snapshot.appendItems(otherFunPlacesItems, toSection: .otherFunPlaces)
}
dataSource?.apply(snapshot, animatingDifferences: true)
}
}
Thanks.

When creating the layout make sure you do it based on the section identifier, not the section number. This way, any section with no data will not be displayed:
let layout = UICollectionViewCompositionalLayout(sectionProvider: { sectionNumber, env in
let sectionIdentifier = self.dataSource.snapshot().sectionIdentifiers[sectionNumber]
if sectionIdentifier == .fiveStar {
return FiveStarSectionLayout....
} else if (sectionIdentifier == .visits) {
return VisitsSectionLayout....
} else if sectionIdentifier == .otherFunPlaces) {
return OtherFunPlacesSectionLayout...
} else {
return nil
}
})
self.collectionView.setCollectionViewLayout(layout, animated: true)

I figured it out.
The problem was indexing the sections to lay out. I had to keep track of available sections separately and only lay out the ones available.

Related

keep footer last in list when dragging new item into the list from another

I have Vue Draggable working like a Kanban board with multiple columns, and I have made each column at least the height of the viewport so that each item can easily be dragged into the column next to it (for cases where one column is much longer than the next, for example).
I also have a button in the footer slot to add new cards to the column. This works well in that it is generally always at the bottom of the list, is not draggable, etc.
The issue arises when I drag an item from another list in below the footer (but still within the height of the draggable element). When I do this, the footer does not stay below the new item, which looks odd.
Once I drop the element, it snaps into place and the footer is one again at the bottom - it is only when the new card is being moved that it appears below the footer.
Is there any way to make sure that even during the move event and a new card being added to a list that the footer stays as the last element?
This issue seems to be captured in this comment on Github issues - https://github.com/SortableJS/Vue.Draggable/issues/673#issuecomment-554149705 - but no solution is provided in that thread.
Any help greatly appreciated.
So for anyone having the same issue I solved this by doing the following:
Hooking into the onMove event using the :move prop to run a function each time an item is moved... e.g. :move="fixFooter"
Creating a function that checks if the moved item would be the last in the new list (thus by default it would sit below the footer without this fix), and if so on the $nextTick append the footer to the Draggable container so it gets moved below the item coming in to the list... (just learnt that .appendChild will move an existing element to the end)
It then checks if the from/to list is the same to account for the item being dragged back into the list it's coming from during the same drag event (otherwise you get the same undesirable footer behaviour as before if you drag an item out of a list and back below the footer without dropping it).
fixFooter(e) {
if(e.relatedContext.list.length === e.draggedContext.futureIndex) {
var newListDom = e.relatedContext.component.$el;
var newListFooter = e.relatedContext.component.$slots.footer[0].elm;
this.$nextTick(() => {
newListDom.appendChild(newListFooter);
});
} else if(e.from === e.to && (e.from.children.length - 1) === e.draggedContext.futureIndex) {
var currentListDom = e.from;
var currentListFooter = e.from.lastChild;
this.$nextTick(() => {
currentListDom.appendChild(currentListFooter);
});
}
}
Seems like for vue draggable next this is enough:
fixFooter(e) {
if (e.relatedContext.list.length === e.draggedContext.futureIndex) {
var newListDom = e.to
var newListFooter = e.to.lastChild
this.$nextTick(() => {
newListDom.appendChild(newListFooter);
});
}
}

Select UI Element by filtering properties in coded ui

I have a web application. And I am using coded ui to write automated tests to test the application.
I have a dropdown with a text box. Which on entering values in the textbox, the values in the dropdown gets filtered based on the text entered.
If I type inside textbox like 'Admin', I will get below options like this:
And I need to capture the two options displayed.
But using IE Developer tool (F12), I am not able to capture the filtered options, because the options that are displayed do not have any unique property (like this below). And the options that are NOT displayed have a class="hidden" property
Any way to capture the elements that are displayed by applying some kind of filter like 'Select ui elements whose class != hidden'
Thanks in advance!!
HI please try below code will it works for you or not.By traversing all those controls that have class ="hidden"
WpfWindow mainWindow = new WpfWindow();
mainWindow.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "hidden");
UITestControlCollection collection = mainWindow.FindMatchingControls();
foreach (UITestControl links in collection)
{
HtmlHyperlink mylink = (HtmlHyperlink)links;
Console.WriteLine(mylink.InnerText);
}
I'm not sure there is a way to do it by search properties, but there are other approaches.
One way would be to brute force difference the collections. Find all the list items, then find the hidden ones and do a difference.
HtmlControl listControl = /* find the UL somehow */
HtmlControl listItemsSearch = new HtmlControl(listControl);
listItemsSearch.SearchProperties.Add(HtmlControl.PropertyNames.TagName, "li");
HtmlControl hiddenListItemsSearch = new HtmlControl(listControl);
hiddenListItemsSearch.SearchProperties.Add(HtmlControl.PropertyNames.TagName, "li");
hiddenListItemsSearch.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "hidden");
var listItems = listItemsSearch.FindMatchingControls().Except(hiddenListItemsSearch.FindMatchingControls());
You will only be able to iterate this collection one time so if you need to iterate multiple times, create a function that returns this search.
var listItemsFunc = () => listItemsSearch.FindMatchingControls().Except(hiddenListItemsSearch.FindMatchingControls());
foreach(var listItem in listItemsFunc()){
// iterate 1
}
foreach(var listItem in listItemsFunc()){
// iterate 2
}
The other way I would consider doing it would be to filter based on the controls which have a clickable point and take up space on the screen (ie, not hidden).
listItemsSearch.FindMatchingControls().Where(x => {
try { x.GetClickablePoint(); return x.Width > 0 && x.Height > 0; } catch { return false; }
});

Search Bar return Button

I have a searchBar in a tableview, searching for results in a .csv file (coredata). The list is huge so the user has to scroll up many times to reach the search bar after the first search OR select the "A" letter in the Indexbar. Is there a way to add a button in the NavigationBar to show the searchBar when the user wants to get back to the beginning of the list? Thanks in advance.
searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//assume a single section after a search
return (searchController.active) ? 1 : sectionTitles.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active {
return searchResults.count
} else {
// Return the number of rows in the section.
let wordKey = sectionTitles[section]
if let items = cockpitDict[wordKey] {
return items.count
}
return 0
}
}
I don't know how to send the user direct to a search bar, but there are always other ways to do things, what you can do is: Reload the storyboard and then it will show again the search bar in the initial states. Take a look on this interesting post: How do I perform an auto-segue in Xcode 6 using Swift? maybe you can go to another VC and return immediately with an simple animation so the user will not notice the tricker. :)
Yes, you can add a UIBarButtonItem to your navigation bar where the action you do will scroll the table back to the top.
Just replace the code from tableView.tableHeaderView = searchController.searchBar to navigationItem.titleView = searchController.searchBar which shows the searchBar on the NavigationBar.
But when you select the searchBar then it goes upward and might be not visible on the screen, so you can take the UISearchBar instead of UISearchController. For more information please look into these thread.
This is not exactly an answer to your question, but still a solution to your problem of quickly returning to the top of your table view. You could overwrite the table view's scrollsToTop: -property to return YES. By doing so, you will enable the user to jump to the top of the table by simply tapping the status bar. This is standard behavior in many stock apps, such as Contacts, Mail, Safari, and Photos.
Beware that only one scroll view / table view / collection view on the screen may return YES in order to achieve this behavior. In the case of multiple scroll views, you can alternatively implement the UIScrollViewDelegate -protocol's
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
callback and dynamically check, if the given scroll view should respond to the tap.
Although this is the Objective-C -way, I guess you should be able to transfer the concept over to Swift.
This approach has two advantages:
You will not clutter your navigation bar. Apples iOS Human Interface Guidelines explicitly suggest to
Avoid crowding a navigation bar with additional controls, even if it looks like there’s enough space. In general, a navigation bar should contain no more than the view’s current title, the back button, and one control that manages the view’s contents.
You will be consistent with the OS' standard behavior.

Styling NSOutlineView Rows

I have a Document based Core Data app with an NSTreeController supplying the content to a view based NSOutlineView. I am "styling" (setting text colour, background colour etc.) the rows based on persistent "transformable" NSColor and NSFont attributes in my data model which the end use can modify. When a new row is popped up, it displays things with the colours/fonts set in the data model. Here is the delegate/datasource code that sets the row background colour:
- (void) outlineView:(NSOutlineView *)outlineView
didAddRowView:(NSTableRowView *)rowView
forRow:(NSInteger)row
{
// Get the relevant nodeType which contains the attributes
QVItem *aNode = [[outlineView itemAtRow:row] representedObject];
if (aNode.backColor)
{
rowView.backgroundColor = aNode.backColor;
}
}
However when the style attributes change I want the associated visible rows to be redrawn with the new style values. Each time a "style" attribute is changed, I am using NSNotificationCenter to send a notification to the Outline view delegate, with the model object whose row needs to be redrawn with the changed style. This is the code in the delegate that receives the notification.
-(void) styleHasChanged: (NSNotification *)aNotification
{
NSTreeNode *aTreeNode = [myTreeController treeNodeForModelObject:aNotification.object];
[myOutlineView reloadItem:aTreeNode];
}
My assumption here is that I can navigate the tree controller to find the tree node which is representing my model object and then ask the outline view to redraw the row for that tree node. This is the "additions" code in the tree controller which walks the tree to find the object - not super efficient, but I don't think there is another way.
#implementation NSTreeController (QVAdditions)
- (NSTreeNode *)treeNodeForModelObject:(id)aModelObject
{
return [self treeNodeForModelObject:aModelObject inNodes:[[self arrangedObjects] childNodes]];
}
- (NSTreeNode *)treeNodeForModelObject:(id)aModelObject inNodes:(NSArray*)nodes
{
for(NSTreeNode* node in nodes)
{
if([node representedObject] == aModelObject)
return node;
if([[node childNodes] count])
{
NSTreeNode * treeNode = [self treeNodeForModelObject:aModelObject inNodes:[node childNodes]];
return treeNode;
}
}
return nil;
}
So sometimes this works and the row redraws, and sometimes it doesn't. The delegate method "styleHasChanged:" is always called, and the tree controller always returns a corresponding tree node (Actually of a subclass of NSTreeNode). But more often than not the outline view does not recognise the tree node, and the row is not redrawn. Its like the tree controller has given back a different tree node object to the one it gave the outline view in the past. But weirdly sometimes it does work and the right row is redrawn with the new background colour. If I collapse the row out of view and pop it open again, it is redrawn correctly.
Anyone any idea why it works sometimes and not other times?
It would be nice to be able to bind the colour/font attributes to the row and columns in some way, so that the outline view did this styling automatically with KVO, but I don't think that is possible - is it?
You spend hours/days trying to work out what you've done wrong; You write the question out; Post it; Sleep on it; and think how stupid can you be.
So I asked the NSTableRowView to redraw itself, but I had not set the new background colour. So here is the new improved (and works) version of styleHasChanged:
-(void) styleHasChanged: (NSNotification *)aNotification
{
QVItem *modelItem = aNotification.object;
NSTreeNode *aTreeNode = [myTreeController treeNodeForModelObject:modelItem];
NSInteger rowIndex = [myOutlineView rowForItem:aTreeNode];
if !(rowIndex == -1)
{
NSTableRowView *rowViewToBeUpdated = [myOutlineView rowViewAtRow:rowIndex makeIfNecessary:YES];
rowViewToBeUpdated.backgroundColor = modelItem.backColor;
}
}
Duh!

How to store changes to a page in tornado.no/cms?

In a controller for a page in tornado-cms, I do the following:
def res = Service.tornado.articles([ articleCategoryPath: "boker/ny"]);
Service.tornado.loadFullArticles(res);
res.sort { a,b ->
b.props.year <=> a.props.year
}
tornado.small_articles = res;
Or, shorter:
tornado.small_articles = Service.tornado.articles([
articleCategoryPath: "boker/ny",
full: true ])
.sort { a, b -> b.props.year <=> a.props.year };
This fills the content box small_articles with the all the articles from a specific folder "boker/ny" reversely sorted by the article prop year.
It works fine, but is it possible to save the changes made to the content box tornado.small_articles so that the resulting list of articles is also visible from the GUI? Something like Service.tornado.saveChangesToPageContentBox(page, content_box_nr, tornado.small_articles);?
Start by removing the articles currently bound to the small_articles box like this:
Integer containerId = <id-of-small-articles-container>;
Service.tornado.getPageBindings(page.id).each { binding ->
if (binding.containerId == containerId)
Service.tornado.deletePageBinding(binding);
}
Then add new bindings for the articles you have collected:
tornado.small_articles.each {
Service.tornado.addPageBinding(new ArticlePageContainer(page.id, it.id, containerId));
}
You should not do this on every request to the given page, but rather update the list when the content changes or by some other, less frequent criteria.

Resources