Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active February 14, 2020 10:04
Show Gist options
  • Save Himura2la/3016b078319429aa36b0be0923b6a76b to your computer and use it in GitHub Desktop.
Save Himura2la/3016b078319429aa36b0be0923b6a76b to your computer and use it in GitHub Desktop.
Embeds a flat list into a 2-level block structure. Useful for responsive UIs when you need to switch between three layout variants.
void TwoLevelsFullyDefined() {
const int itemsCount = 12;
const int level1BlocksInList = 2;
const int itemsInLevel1Block = 6;
const int level2BlocksInLevel1Block = 2;
const int itemsInLevel2Block = 3;
Console.WriteLine("<list>");
for(int currentLevel1Block = 0; currentLevel1Block < level1BlocksInList; currentLevel1Block++) {
Console.WriteLine($" <level1-block{currentLevel1Block}>");
int firstLevel2Block = currentLevel1Block * level2BlocksInLevel1Block;
int lastLevel2Block = (currentLevel1Block + 1) * level2BlocksInLevel1Block - 1;
int lastItemInLevel1Block = Math.Min((currentLevel1Block + 1) * itemsInLevel1Block - 1, itemsCount);
for(int currentLevel2Block = firstLevel2Block; currentLevel2Block <= lastLevel2Block; currentLevel2Block++) {
Console.WriteLine($" <level2-block{currentLevel2Block}>");
int firstItemInLevel2Block = currentLevel2Block * itemsInLevel2Block;
int lastItemInLevel2Block = Math.Min((currentLevel2Block + 1) * itemsInLevel2Block - 1, lastItemInLevel1Block);
for(int i = firstItemInLevel2Block; i <= lastItemInLevel2Block; i++) {
Console.WriteLine($" <item-{i}>");
}
Console.WriteLine($" </level2-block{currentLevel2Block}>");
}
Console.WriteLine($" </level1-block{currentLevel1Block}>");
}
Console.WriteLine($"</list>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment