Fix the problem where the activated field was not set correctly for a theme property (#1632)

Signed-off-by: John Niang <johnniang@fastmail.com>
pull/1635/head
John Niang 2022-01-16 13:30:09 +08:00 committed by GitHub
parent bd87d17329
commit 2249ef1103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 19 deletions

View File

@ -61,7 +61,9 @@ public class ThemeRepositoryImpl
@Override @Override
public String getActivatedThemeId() { public String getActivatedThemeId() {
return getActivatedThemeProperty().getId(); return this.optionRepository.findByKey(THEME.getValue())
.map(Option::getValue)
.orElse(DEFAULT_THEME_ID);
} }
@Override @Override
@ -72,13 +74,11 @@ public class ThemeRepositoryImpl
synchronized (this) { synchronized (this) {
if (this.currentTheme == null) { if (this.currentTheme == null) {
// get current theme id // get current theme id
String currentThemeId = this.optionRepository.findByKey(THEME.getValue()) String currentThemeId = getActivatedThemeId();
.map(Option::getValue)
.orElse(DEFAULT_THEME_ID);
// fetch current theme // fetch current theme
this.currentTheme = this.currentTheme =
this.fetchThemeByThemeId(currentThemeId).orElseGet(() -> { this.fetchThemePropertyByThemeId(currentThemeId).orElseGet(() -> {
if (!StringUtils.equalsIgnoreCase(currentThemeId, DEFAULT_THEME_ID)) { if (!StringUtils.equalsIgnoreCase(currentThemeId, DEFAULT_THEME_ID)) {
fallbackTheme.set(true); fallbackTheme.set(true);
return this.getThemeByThemeId(DEFAULT_THEME_ID); return this.getThemeByThemeId(DEFAULT_THEME_ID);
@ -98,7 +98,7 @@ public class ThemeRepositoryImpl
@Override @Override
public Optional<ThemeProperty> fetchThemePropertyByThemeId(String themeId) { public Optional<ThemeProperty> fetchThemePropertyByThemeId(String themeId) {
return ThemePropertyScanner.INSTANCE.scan(getThemeRootPath(), null) return listAll()
.stream() .stream()
.filter(property -> Objects.equals(themeId, property.getId())) .filter(property -> Objects.equals(themeId, property.getId()))
.findFirst(); .findFirst();
@ -127,7 +127,7 @@ public class ThemeRepositoryImpl
@Override @Override
public ThemeProperty attemptToAdd(ThemeProperty newProperty) { public ThemeProperty attemptToAdd(ThemeProperty newProperty) {
// 1. check existence // 1. check existence
final var alreadyExist = fetchThemeByThemeId(newProperty.getId()).isPresent(); final var alreadyExist = fetchThemePropertyByThemeId(newProperty.getId()).isPresent();
if (alreadyExist) { if (alreadyExist) {
throw new AlreadyExistsException("当前安装的主题已存在"); throw new AlreadyExistsException("当前安装的主题已存在");
} }
@ -206,15 +206,8 @@ public class ThemeRepositoryImpl
@NonNull @NonNull
protected ThemeProperty getThemeByThemeId(String themeId) { protected ThemeProperty getThemeByThemeId(String themeId) {
return fetchThemeByThemeId(themeId).orElseThrow( return fetchThemePropertyByThemeId(themeId).orElseThrow(
() -> new ThemeNotFoundException("Failed to find theme with id: " + themeId)); () -> new ThemeNotFoundException("Failed to find theme with id: " + themeId));
} }
@NonNull
protected Optional<ThemeProperty> fetchThemeByThemeId(String themeId) {
return ThemePropertyScanner.INSTANCE.scan(getThemeRootPath(), null)
.stream()
.filter(property -> Objects.equals(themeId, property.getId()))
.findFirst();
}
} }

View File

@ -60,13 +60,13 @@ class ThemeRepositoryImplTest {
given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty()); given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
doReturn(Optional.of(expectedTheme)).when(themeRepository) doReturn(Optional.of(expectedTheme)).when(themeRepository)
.fetchThemeByThemeId(HaloConst.DEFAULT_THEME_ID); .fetchThemePropertyByThemeId(HaloConst.DEFAULT_THEME_ID);
ThemeProperty resultTheme = themeRepository.getActivatedThemeProperty(); ThemeProperty resultTheme = themeRepository.getActivatedThemeProperty();
assertEquals(expectedTheme, resultTheme); assertEquals(expectedTheme, resultTheme);
verify(optionRepository, times(1)).findByKey(any()); verify(optionRepository, times(1)).findByKey(any());
verify(themeRepository, times(1)).fetchThemeByThemeId(any()); verify(themeRepository, times(1)).fetchThemePropertyByThemeId(any());
} }
@Test @Test
@ -77,7 +77,7 @@ class ThemeRepositoryImplTest {
given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty()); given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
doReturn(Optional.of(expectedTheme)).when(themeRepository) doReturn(Optional.of(expectedTheme)).when(themeRepository)
.fetchThemeByThemeId(HaloConst.DEFAULT_THEME_ID); .fetchThemePropertyByThemeId(HaloConst.DEFAULT_THEME_ID);
ExecutorService executorService = Executors.newFixedThreadPool(10); ExecutorService executorService = Executors.newFixedThreadPool(10);
// define tasks // define tasks
@ -96,7 +96,7 @@ class ThemeRepositoryImplTest {
}); });
verify(optionRepository, times(1)).findByKey(any()); verify(optionRepository, times(1)).findByKey(any());
verify(themeRepository, times(1)).fetchThemeByThemeId(any()); verify(themeRepository, times(1)).fetchThemePropertyByThemeId(any());
} }
} }