-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing some programming controls (#130)
* Checkpoint * Restrict hours control * Implement FF and rewind schedule
- Loading branch information
1 parent
fe3e307
commit e8998c8
Showing
7 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const OneDayMillis = 1000 * 60 * 60 * 24; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { CondensedChannelProgram, isFlexProgram } from '@tunarr/types'; | ||
import dayjs from 'dayjs'; | ||
import duration from 'dayjs/plugin/duration'; | ||
import { forEach, inRange, reject } from 'lodash-es'; | ||
import { OneDayMillis } from '../../helpers/constants.ts'; | ||
import { createFlexProgram } from '../../helpers/util.ts'; | ||
import useStore from '../../store/index.ts'; | ||
import { | ||
updateCurrentChannel, | ||
setCurrentLineup, | ||
} from '../../store/channelEditor/actions.ts'; | ||
|
||
dayjs.extend(duration); | ||
|
||
export const restrictHours = ( | ||
programs: CondensedChannelProgram[], | ||
from: number, | ||
to: number, | ||
) => { | ||
if (!inRange(from, 0, 24) || !inRange(to, 0, 24) || to <= from) { | ||
return { newStartTime: null, newPrograms: programs }; | ||
} | ||
|
||
const newStartTime = dayjs().hour(from).minute(0).second(0).millisecond(0); | ||
const startTimeOffset = dayjs.duration(from, 'hours').asMilliseconds(); | ||
const endTimeOffset = dayjs.duration(to, 'hours').asMilliseconds(); | ||
const maxDuration = endTimeOffset - startTimeOffset; | ||
// Remove all flex and programs that will never fit in the restricted hours slot | ||
const workingPrograms = reject( | ||
programs, | ||
(p) => isFlexProgram(p) || p.duration > maxDuration, | ||
); | ||
|
||
let currOffset = 0; // Offset from the channel start time | ||
const newPrograms: CondensedChannelProgram[] = []; | ||
|
||
forEach(workingPrograms, (program) => { | ||
const timeLeft = maxDuration - currOffset; | ||
if (program.duration > timeLeft) { | ||
// Put the program back and try tomorrow. | ||
// workingPrograms.unshift(program); | ||
// Flex until the following day's start time | ||
newPrograms.push( | ||
createFlexProgram(timeLeft + OneDayMillis - maxDuration), | ||
); | ||
currOffset = 0; | ||
} | ||
|
||
newPrograms.push(program); | ||
currOffset += program.duration; | ||
}); | ||
|
||
return { newStartTime, newPrograms }; | ||
}; | ||
|
||
export const useRestrictHours = () => { | ||
const programs = useStore((s) => s.channelEditor.programList); | ||
|
||
return (from: number, to: number) => { | ||
const { newStartTime, newPrograms } = restrictHours(programs, from, to); | ||
|
||
if (newStartTime) { | ||
updateCurrentChannel({ startTime: newStartTime.unix() * 1000 }); | ||
} | ||
setCurrentLineup(newPrograms, true); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { setChannelStartTime } from '../../store/channelEditor/actions.ts'; | ||
import useStore from '../../store/index.ts'; | ||
import { materializedProgramListSelector } from '../../store/selectors.ts'; | ||
|
||
const useSlide = () => { | ||
const channel = useStore(({ channelEditor }) => channelEditor.currentEntity); | ||
const programs = useStore(materializedProgramListSelector); | ||
return (amount: number) => { | ||
if (channel && amount !== 0 && programs.length > 0) { | ||
setChannelStartTime(channel.startTime + amount); | ||
} | ||
}; | ||
}; | ||
|
||
export const useFastForwardSchedule = () => { | ||
const slide = useSlide(); | ||
return (amount: number) => slide(amount < 0 ? amount : -amount); | ||
}; | ||
|
||
export const useRewindSchedule = () => { | ||
const slide = useSlide(); | ||
return (amount: number) => slide(Math.abs(amount)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters