-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProfileView.java
More file actions
118 lines (100 loc) · 5.02 KB
/
ProfileView.java
File metadata and controls
118 lines (100 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.cleancode.journal.view;
import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.select.Select;
import com.vaadin.flow.component.textfield.PasswordField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import org.cleancode.journal.component.progressbar.PercentProgressBarComponent;
import org.cleancode.journal.component.speeddial.AddSpeedDial;
import org.cleancode.journal.domain.Profile;
import org.cleancode.journal.domain.Score;
import org.cleancode.journal.domain.grade.GradeColor;
import org.cleancode.journal.service.IAchievementService;
import org.cleancode.journal.service.IGradeService;
import org.cleancode.journal.service.INameService;
@Route(layout = MainView.class)
@PageTitle("Clean Code - Profile")
public class ProfileView extends Composite<VerticalLayout> {
private final INameService nameService;
public ProfileView(INameService nameService, Profile profile, IGradeService gradeService, IAchievementService achievementService) {
this.nameService = nameService;
var content = getContent();
content.add(createExperienceBar(profile));
content.add(createScoreBars(profile));
content.add(createNameField(profile));
content.add(createPasswordField());
content.add(createGradeSelect(profile));
content.add(new AddSpeedDial(profile, gradeService, achievementService));
}
private HorizontalLayout createScoreBars(Profile profile) {
Score score = profile.getScore();
int max = score.getMax();
PercentProgressBarComponent talent = createScoreBar("TAL", max, score.getTalent());
PercentProgressBarComponent strength = createScoreBar("STR", max, score.getStrength());
PercentProgressBarComponent intellect = createScoreBar("INT", max, score.getIntellect());
PercentProgressBarComponent charisma = createScoreBar("CHA", max, score.getCharisma());
HorizontalLayout horizontalLayout = new HorizontalLayout(talent, strength, intellect, charisma);
horizontalLayout.setWidthFull();
return horizontalLayout;
}
private PercentProgressBarComponent createScoreBar(String label, int value, int max) {
PercentProgressBarComponent scoreBar = new PercentProgressBarComponent();
scoreBar.setLabel(label);
scoreBar.setMaxValue(max);
scoreBar.setValue(value);
return scoreBar;
}
private PercentProgressBarComponent createExperienceBar(Profile profile) {
PercentProgressBarComponent experience = new PercentProgressBarComponent();
experience.setLabel("XP");
experience.setShowDescription(true);
experience.setMaxValue(getNextLevelExperience(profile.getLevel()));
experience.setValue(profile.getScore().getExperience());
return experience;
}
private int getNextLevelExperience(int level) {
return (int) ((level * 200) * 1.2);
}
private HorizontalLayout createNameField(Profile profile) {
HorizontalLayout nameSelection = new HorizontalLayout();
nameSelection.setSpacing(false);
nameSelection.setAlignItems(Alignment.BASELINE);
TextField userName = new TextField(profile.getName());
userName.setValue(profile.getName());
userName.addValueChangeListener(e -> profile.setName(e.getValue()));
userName.setLabel(getTranslation("user.name"));
nameSelection.add(userName);
Button newName = new Button();
newName.addClickListener((e) -> generateNewUserName(userName));
newName.setIcon(new Icon(VaadinIcon.REFRESH));
nameSelection.add(newName);
return nameSelection;
}
private PasswordField createPasswordField() {
PasswordField passwordField = new PasswordField();
passwordField.setLabel(getTranslation("user.password"));
return passwordField;
}
private Select<GradeColor> createGradeSelect(Profile profile) {
Select<GradeColor> modeSelect = new Select<>();
modeSelect.setItems(GradeColor.values());
modeSelect.setLabel(getTranslation("profile.current-grade"));
modeSelect.setValue(profile.getCurrentGrade());
modeSelect.setItemLabelGenerator(color -> getTranslation(color.getMessageKey()));
modeSelect.addValueChangeListener(event -> profile.setCurrentGrade(event.getValue()));
return modeSelect;
}
public void generateNewUserName(TextField name) {
String randomName = nameService.getRandomName();
name.setValue(randomName);
Notification.show(getTranslation("profile.action.changed-username", randomName));
}
}