-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Enable flexible number of colors for themes #3482
base: master
Are you sure you want to change the base?
Changes from 15 commits
f77edcf
b0392c7
c8f0b38
8563458
0888fe2
bf7e006
aeafe7c
1885a5c
ef26201
2c34989
6ec7da5
52b4a53
b97aa26
7ec797f
cbb938b
a4304f4
927e7c9
a7ac61e
0699b0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,67 @@ | ||
[ | ||
{ | ||
"id": "ashes", | ||
"name": " Ashes" | ||
"name": " Ashes", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "chalk", | ||
"name": "Chalk" | ||
"name": "Chalk", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "eighties", | ||
"name": "Eighties" | ||
"name": "Eighties", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "google", | ||
"name": "Google" | ||
"name": "Google", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "mocha", | ||
"name": "Mocha" | ||
"name": "Mocha", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "monokai", | ||
"name": "Monokai" | ||
"name": "Monokai", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "ocean", | ||
"name": "Ocean" | ||
"name": "Ocean", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "oceanic-next", | ||
"name": "Oceanic Next" | ||
"name": "Oceanic Next", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "paraiso", | ||
"name": "Paraiso" | ||
"name": "Paraiso", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "railscasts", | ||
"name": "Railscasts" | ||
"name": "Railscasts", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "tequila", | ||
"name": "Tequila", | ||
"numOfColors": 10 | ||
}, | ||
{ | ||
"id": "tomorrow", | ||
"name": "Tomorrow" | ||
"name": "Tomorrow", | ||
"numOfColors": 8 | ||
}, | ||
{ | ||
"id": "twilight", | ||
"name": "Twilight" | ||
"name": "Twilight", | ||
"numOfColors": 8 | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import { createMigrate } from 'redux-persist'; | |
import { PersistConfig } from 'storage/persistReducer'; | ||
import { ModuleCode } from 'types/modules'; | ||
import { ModuleLessonConfig, SemTimetableConfig } from 'types/timetables'; | ||
import { ColorMapping, TimetablesState } from 'types/reducers'; | ||
import { ColorMapping, SemesterColorMap, TimetablesState } from 'types/reducers'; | ||
|
||
import config from 'config'; | ||
import { | ||
|
@@ -14,6 +14,7 @@ import { | |
HIDE_LESSON_IN_TIMETABLE, | ||
REMOVE_MODULE, | ||
SELECT_MODULE_COLOR, | ||
REASSIGN_ALL_MODULES_COLOR, | ||
SET_LESSON_CONFIG, | ||
SET_TIMETABLE, | ||
SHOW_LESSON_IN_TIMETABLE, | ||
|
@@ -131,7 +132,7 @@ function semColors(state: ColorMapping = defaultSemColorMap, action: Actions): C | |
case ADD_MODULE: | ||
return { | ||
...state, | ||
[moduleCode]: getNewColor(values(state)), | ||
[moduleCode]: getNewColor(values(state), action.payload.numOfColors), | ||
}; | ||
|
||
case REMOVE_MODULE: | ||
|
@@ -148,6 +149,19 @@ function semColors(state: ColorMapping = defaultSemColorMap, action: Actions): C | |
} | ||
} | ||
|
||
function recolor(state: SemesterColorMap, numOfColors: number): SemesterColorMap { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor thought: This is a fine approach, but I wonder if it'd be better to remove this function and leave color indices as is. Instead, we could modulo the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I used an action to reassign the colors is that there are too many places where we have to modulo the colorIndex when rendering colors, which might make code changes hard to keep track of. |
||
const newSemesterColorMap: SemesterColorMap = {}; | ||
joeng03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object.keys(state).forEach((semester) => { | ||
const colorMapping = state[semester]; | ||
joeng03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const newColorMapping: ColorMapping = {}; | ||
Object.keys(colorMapping).forEach((moduleCode) => { | ||
newColorMapping[moduleCode] = colorMapping[moduleCode] % numOfColors; | ||
joeng03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
newSemesterColorMap[semester] = newColorMapping; | ||
}); | ||
return newSemesterColorMap; | ||
} | ||
|
||
// Map of semester to list of hidden modules | ||
const defaultHiddenState: ModuleCode[] = []; | ||
function semHiddenModules(state = defaultHiddenState, action: Actions) { | ||
|
@@ -182,7 +196,6 @@ function timetables( | |
if (!action.payload) { | ||
return state; | ||
} | ||
|
||
joeng03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch (action.type) { | ||
case SET_TIMETABLE: { | ||
const { semester, timetable, colors } = action.payload; | ||
|
@@ -219,6 +232,11 @@ function timetables( | |
hidden: { [semester]: hidden }, | ||
}; | ||
} | ||
case REASSIGN_ALL_MODULES_COLOR: | ||
return { | ||
...state, | ||
colors: recolor(state.colors, action.payload.numOfColors), | ||
}; | ||
|
||
default: | ||
return state; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this affects local storage schema, we should be more careful here. In the long run, I don't think we'll want to store
numOfColors
inThemeState
as it only contains user customizable data (i.e. current theme ID, should show title, orientation of timetable).Since
numOfColors
is a static property of a theme, instead of storing it in Redux, we should store it in a predefined readonly map like the one that you've defined inthemes.json
. This has the added benefit of you no longer having to passnumOfColors
around everywhere as you can just read it from the map.That said, if/when we add user-defined themes, they should be stored in Redux, but likely in a different place instead of
ThemeState
.