Simplify client code that reads the user preferences

Use

  Gerrit.getUserPeferences()

instead of

  Gerrit.getUserAccount().getGeneralPreferences()

The first one returns the client side representation of the user
settings (AccountPreferencesInfo) while the second one returns the
entity class for persisting the user preferences in the database
(AccountGeneralPreferences).

Gerrit.getUserPreferences() never returns null. If the user is not
signed in the default preferences are returned. Hence we no longer
need to check with Gerrit.isSignedIn() whether the user is signed in
whenever we access the user preferences.

Change-Id: I6420c221f66299485d0cd913a55563b0cd2a9b11
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-23 10:46:03 +02:00
parent 6a56672b72
commit 042c394f80
13 changed files with 45 additions and 118 deletions

View File

@@ -59,7 +59,11 @@ public class AccountPreferencesInfo extends JavaScriptObject {
}
public final short changesPerPage() {
return get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE);
short changesPerPage =
get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE);
return 0 < changesPerPage
? changesPerPage
: AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
private final native short get(String n, int d)
/*-{ return this.hasOwnProperty(n) ? this[n] : d }-*/;

View File

@@ -625,10 +625,7 @@ public class Dispatcher {
}
private static boolean preferUnified() {
return Gerrit.isSignedIn()
&& DiffView.UNIFIED_DIFF.equals(Gerrit.getUserAccount()
.getGeneralPreferences()
.getDiffView());
return DiffView.UNIFIED_DIFF.equals(Gerrit.getUserPreferences().diffView());
}
private static void unified(final String token,

View File

@@ -274,9 +274,7 @@ public class Gerrit implements EntryPoint {
public static void setHeaderVisible(boolean visible) {
topMenu.setVisible(visible);
siteHeader.setVisible(visible && (myAccount != null
? myAccount.getGeneralPreferences().isShowSiteHeader()
: true));
siteHeader.setVisible(visible && getUserPreferences().showSiteHeader());
}
public static boolean isHeaderVisible() {

View File

@@ -22,7 +22,6 @@ import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
@@ -44,7 +43,7 @@ public class GroupListScreen extends Screen {
public GroupListScreen() {
setRequiresSignIn(true);
configurePageSize();
pageSize = Gerrit.getUserPreferences().changesPerPage();
}
public GroupListScreen(String params) {
@@ -65,17 +64,6 @@ public class GroupListScreen extends Screen {
}
}
private void configurePageSize() {
if (Gerrit.isSignedIn()) {
final AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
final short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
}
@Override
protected void onLoad() {
super.onLoad();

View File

@@ -38,7 +38,6 @@ import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.client.ui.NavigationTable;
import com.google.gerrit.client.ui.OnEditEnabler;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
@@ -92,18 +91,7 @@ public class ProjectBranchesScreen extends ProjectScreen {
public ProjectBranchesScreen(final Project.NameKey toShow) {
super(toShow);
configurePageSize();
}
private void configurePageSize() {
if (Gerrit.isSignedIn()) {
AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
pageSize = Gerrit.getUserPreferences().changesPerPage();
}
private void parseToken() {

View File

@@ -30,7 +30,6 @@ import com.google.gerrit.client.ui.ProjectSearchLink;
import com.google.gerrit.client.ui.ProjectsTable;
import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
@@ -57,10 +56,11 @@ public class ProjectListScreen extends Screen {
private Query query;
public ProjectListScreen() {
configurePageSize();
pageSize = Gerrit.getUserPreferences().changesPerPage();
}
public ProjectListScreen(String params) {
this();
for (String kvPair : params.split("[,;&]")) {
String[] kv = kvPair.split("=", 2);
if (kv.length != 2 || kv[0].isEmpty()) {
@@ -75,18 +75,6 @@ public class ProjectListScreen extends Screen {
start = Integer.parseInt(URL.decodeQueryString(kv[1]));
}
}
configurePageSize();
}
private void configurePageSize() {
if (Gerrit.isSignedIn()) {
final AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
final short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
}
@Override

View File

@@ -26,7 +26,6 @@ import com.google.gerrit.client.rpc.NativeMap;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gwt.core.client.JavaScriptObject;
@@ -238,9 +237,7 @@ class DownloadBox extends VerticalPanel {
}
private static String getUserPreference() {
if (Gerrit.isSignedIn()) {
DownloadScheme pref =
Gerrit.getUserAccount().getGeneralPreferences().getDownloadUrl();
DownloadScheme pref = Gerrit.getUserPreferences().downloadScheme();
if (pref != null) {
switch (pref) {
case ANON_GIT:
@@ -257,18 +254,15 @@ class DownloadBox extends VerticalPanel {
return null;
}
}
}
return null;
}
private void saveScheme() {
DownloadScheme scheme = getSelectedScheme();
AccountGeneralPreferences pref =
Gerrit.getUserAccount().getGeneralPreferences();
AccountPreferencesInfo prefs = Gerrit.getUserPreferences();
if (Gerrit.isSignedIn() && scheme != null
&& scheme != pref.getDownloadUrl()) {
pref.setDownloadUrl(scheme);
&& scheme != prefs.downloadScheme()) {
prefs.downloadScheme(scheme);
AccountPreferencesInfo in = AccountPreferencesInfo.create();
in.downloadScheme(scheme);
AccountApi.self().view("preferences")

View File

@@ -471,8 +471,8 @@ public class FileTable extends FlowPanel {
this.comments = comments;
this.drafts = drafts;
this.hasUser = Gerrit.isSignedIn();
this.showChangeSizeBars = !hasUser
|| Gerrit.getUserAccount().getGeneralPreferences().isSizeBarInChangeTable();
this.showChangeSizeBars =
Gerrit.getUserPreferences().sizeBarInChangeTable();
myTable.addStyleName(R.css().table());
}
@@ -648,8 +648,7 @@ public class FileTable extends FlowPanel {
if (Patch.COMMIT_MSG.equals(path)) {
sb.append(Util.C.commitMessage());
} else if (!hasUser || Gerrit.getUserAccount().getGeneralPreferences()
.isMuteCommonPathPrefixes()) {
} else if (Gerrit.getUserPreferences().muteCommonPathPrefixes()) {
int commonPrefixLen = commonPrefix(path);
if (commonPrefixLen > 0) {
sb.openSpan().setStyleName(R.css().commonPrefix())

View File

@@ -71,19 +71,17 @@ public class ChangeTable extends NavigationTable<ChangeInfo> {
private final List<Section> sections;
private int columns;
private boolean showLegacyId;
private final boolean showLegacyId;
private List<String> labelNames;
public ChangeTable() {
super(Util.C.changeItemHelp());
columns = BASE_COLUMNS;
labelNames = Collections.emptyList();
showLegacyId = Gerrit.getUserPreferences().legacycidInChangeTable();
if (Gerrit.isSignedIn()) {
keysAction.add(new StarKeyCommand(0, 's', Util.C.changeTableStar()));
showLegacyId = Gerrit.getUserAccount()
.getGeneralPreferences()
.isLegacycidInChangeTable();
}
sections = new ArrayList<>();
@@ -242,18 +240,14 @@ public class ChangeTable extends NavigationTable<ChangeInfo> {
table.setWidget(row, C_PROJECT, new ProjectLink(c.projectNameKey()));
table.setWidget(row, C_BRANCH, new BranchLink(c.projectNameKey(), c
.status(), c.branch(), c.topic()));
if (Gerrit.isSignedIn()
&& Gerrit.getUserAccount().getGeneralPreferences()
.isRelativeDateInChangeTable()) {
if (Gerrit.getUserPreferences().relativeDateInChangeTable()) {
table.setText(row, C_LAST_UPDATE, relativeFormat(c.updated()));
} else {
table.setText(row, C_LAST_UPDATE, shortFormat(c.updated()));
}
int col = C_SIZE;
if (Gerrit.isSignedIn()
&& !Gerrit.getUserAccount().getGeneralPreferences()
.isSizeBarInChangeTable()) {
if (!Gerrit.getUserPreferences().sizeBarInChangeTable()) {
table.setText(row, col,
Util.M.insertionsAndDeletions(c.insertions(), c.deletions()));
} else {
@@ -275,10 +269,8 @@ public class ChangeTable extends NavigationTable<ChangeInfo> {
String user;
String info;
ReviewCategoryStrategy reviewCategoryStrategy = Gerrit.isSignedIn()
? Gerrit.getUserAccount().getGeneralPreferences()
.getReviewCategoryStrategy()
: ReviewCategoryStrategy.NONE;
ReviewCategoryStrategy reviewCategoryStrategy =
Gerrit.getUserPreferences().reviewCategoryStrategy();
if (label.rejected() != null) {
user = label.rejected().name();
info = getReviewCategoryDisplayInfo(reviewCategoryStrategy,

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.ScreenLoadCallback;
import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
@@ -39,15 +38,7 @@ public abstract class PagedSingleListScreen extends Screen {
protected PagedSingleListScreen(String anchorToken, int start) {
anchorPrefix = anchorToken;
this.start = start;
if (Gerrit.isSignedIn()) {
final AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
final short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
pageSize = Gerrit.getUserPreferences().changesPerPage();
}
@Override

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.client.download;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.config.DownloadInfo.DownloadCommandInfo;
import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwtexpui.clippy.client.CopyableLabel;
@@ -41,14 +40,7 @@ public abstract class DownloadPanel extends FlowPanel {
private void setupWidgets() {
if (!urls.isEmpty()) {
final AccountGeneralPreferences pref;
if (Gerrit.isSignedIn()) {
pref = Gerrit.getUserAccount().getGeneralPreferences();
} else {
pref = new AccountGeneralPreferences();
pref.resetToDefaults();
}
urls.select(pref.getDownloadUrl());
urls.select(Gerrit.getUserPreferences().downloadScheme());
FlowPanel p = new FlowPanel();
p.setStyleName(Gerrit.RESOURCES.css().downloadLinkHeader());

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.account.AccountApi;
import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo;
import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gwt.aria.client.Roles;
import com.google.gwt.core.client.JavaScriptObject;
@@ -110,13 +109,10 @@ public class DownloadUrlLink extends Anchor implements ClickHandler {
select();
AccountGeneralPreferences pref =
Gerrit.getUserAccount().getGeneralPreferences();
AccountPreferencesInfo prefs = Gerrit.getUserPreferences();
if (Gerrit.isSignedIn() && scheme != null
&& scheme != pref.getDownloadUrl()) {
// If the user is signed-in, remember this choice for future panels.
//
pref.setDownloadUrl(scheme);
&& scheme != prefs.downloadScheme()) {
prefs.downloadScheme(scheme);
AccountPreferencesInfo in = AccountPreferencesInfo.create();
in.downloadScheme(scheme);
AccountApi.self().view("preferences")

View File

@@ -262,7 +262,7 @@ class PatchTable extends Composite {
private static boolean isUnifiedPatchLink(final Patch patch) {
return (patch.getPatchType().equals(PatchType.BINARY)
|| (Gerrit.isSignedIn()
&& Gerrit.getUserAccount().getGeneralPreferences().getDiffView()
&& Gerrit.getUserPreferences().diffView()
.equals(DiffView.UNIFIED_DIFF)));
}