cleanup and fix sorting
This commit is contained in:
+30
-32
@@ -139,22 +139,15 @@ impl ChannelsState {
|
|||||||
|
|
||||||
pub fn update_channel_parents(&mut self) {
|
pub fn update_channel_parents(&mut self) {
|
||||||
// Zero out existing children
|
// Zero out existing children
|
||||||
for (id, state) in self.channels.iter_mut() {
|
for state in self.channels.values_mut() {
|
||||||
state.children = OrderSet::new();
|
state.children.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut channels_updated: HashSet<ChannelId> = HashSet::new();
|
let mut to_sort: Vec<(ChannelId, Option<ChannelId>, i32, String)> = Vec::new();
|
||||||
channels_updated.insert(0); // insert root channel
|
|
||||||
|
|
||||||
let mut channels_to_parse: Vec<(ChannelId, ChannelId, i32)> = Vec::new();
|
|
||||||
for (id, state) in self.channels.iter() {
|
for (id, state) in self.channels.iter() {
|
||||||
// Skip root channel
|
// Hnadle channels with no parent (the root channel)
|
||||||
if *id == 0 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore channels with no parent
|
|
||||||
let Some(parent_id) = state.parent else {
|
let Some(parent_id) = state.parent else {
|
||||||
|
to_sort.push((*id, None, 0, state.name.clone()));
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -164,37 +157,42 @@ impl ChannelsState {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
channels_to_parse.push((*id, parent_id, state.position));
|
to_sort.push((*id, Some(parent_id), state.position, state.name.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut channel_positions = HashMap::new();
|
let pos_name: HashMap<ChannelId, (i32, String)> = self
|
||||||
for (id, state) in self.channels.iter() {
|
.channels
|
||||||
channel_positions.insert(*id, state.position);
|
.iter()
|
||||||
}
|
.map(|(&id, state)| (id, (state.position, state.name.clone())))
|
||||||
|
.collect();
|
||||||
|
|
||||||
while channels_updated.len() < channels_to_parse.len() {
|
let mut updated: HashSet<ChannelId> = HashSet::new();
|
||||||
for (id, parent_id, position) in &channels_to_parse {
|
|
||||||
if channels_updated.contains(&id) {
|
while updated.len() < to_sort.len() {
|
||||||
continue;
|
for &(id, ref parent_id, position, ref name) in &to_sort {
|
||||||
}
|
let Some(parent_id) = parent_id else {
|
||||||
|
updated.insert(id);
|
||||||
// Skip channels whose parent we haven't seen yet
|
continue;
|
||||||
if !channels_updated.contains(&parent_id) {
|
};
|
||||||
|
|
||||||
|
if updated.contains(&id) || !updated.contains(&parent_id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unwrap should never fail here since we pre filter
|
||||||
let parent = self.channels.get_mut(&parent_id).unwrap();
|
let parent = self.channels.get_mut(&parent_id).unwrap();
|
||||||
|
|
||||||
let mut after_index = 0;
|
let mut insert_index = parent.children.len();
|
||||||
for (index, child_id) in parent.children.iter().enumerate() {
|
for (i, &child) in parent.children.iter().enumerate() {
|
||||||
after_index = index;
|
let (p, ref n) = pos_name[&child];
|
||||||
if channel_positions[child_id] > *position {
|
if (position == p && name < n) || p > position {
|
||||||
continue;
|
insert_index = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parent.children.insert_before(after_index, *id);
|
|
||||||
|
|
||||||
channels_updated.insert(*id);
|
parent.children.insert_before(insert_index, id);
|
||||||
|
updated.insert(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user