Code cleanup

This commit is contained in:
Hillel Coren 2019-08-23 16:44:46 +03:00
parent c9f6b0460a
commit 6a87c3da75
7 changed files with 16 additions and 15 deletions

View File

@ -120,7 +120,7 @@ abstract class DocumentEntity extends Object
: '${round(size / 1000, 0).toInt()} KB';
String previewUrl(String baseUrl) =>
formatApiUrlMachine(
formatApiUrl(
baseUrl != null && baseUrl.startsWith('http') ? baseUrl : kAppUrl) +
'/documents/$id';

View File

@ -29,7 +29,7 @@ class AuthRepository {
'one_time_password': oneTimePassword,
};
url = formatApiUrlMachine(url) + '/login';
url = formatApiUrl(url) + '/login';
return sendRequest(url: url, data: credentials);
}
@ -42,7 +42,7 @@ class AuthRepository {
'token': token,
'provider': 'google',
};
url = formatApiUrlMachine(url) + '/oauth_login';
url = formatApiUrl(url) + '/oauth_login';
return sendRequest(url: url, data: credentials);
}
@ -53,7 +53,7 @@ class AuthRepository {
'token_name': 'invoice-ninja-$platform-app',
};
url = formatApiUrlMachine(url) + '/refresh';
url = formatApiUrl(url) + '/refresh';
return sendRequest(url: url, data: credentials, token: token);
}

View File

@ -266,7 +266,8 @@ abstract class AppState implements Built<AppState, AppStateBuilder> {
@override
String toString() {
return 'Is Testing: ${uiState.isTesting}';
return 'URL: ${authState.url}';
//return 'Is Testing: ${uiState.isTesting}';
//return 'Route: ${uiState.currentRoute}, Previous: ${uiState.previousRoute}, Layout: ${uiState.layout}, Menu: ${uiState.isMenuVisible}, History: ${uiState.isHistoryVisible}';
}
}

View File

@ -30,8 +30,8 @@ void _saveAuthLocal({String email, String url, String secret}) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(kSharedPrefEmail, email ?? '');
if (formatApiUrlReadable(url) != kAppUrl) {
prefs.setString(kSharedPrefUrl, formatApiUrlMachine(url));
if (cleanApiUrl(url).isNotEmpty && cleanApiUrl(url) != kAppUrl) {
prefs.setString(kSharedPrefUrl, formatApiUrl(url));
prefs.setString(kSharedPrefSecret, secret);
}
}
@ -39,7 +39,7 @@ void _saveAuthLocal({String email, String url, String secret}) async {
void _loadAuthLocal(Store<AppState> store) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String email = prefs.getString(kSharedPrefEmail) ?? '';
final String url = formatApiUrlMachine(prefs.getString(kSharedPrefUrl) ?? '');
final String url = formatApiUrl(prefs.getString(kSharedPrefUrl) ?? '');
final String secret = prefs.getString(kSharedPrefSecret) ?? '';
store.dispatch(UserLoginLoaded(email, url, secret));
@ -153,7 +153,7 @@ Middleware<AppState> _createRefreshRequest(AuthRepository repository) {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String url =
formatApiUrlMachine(prefs.getString(kSharedPrefUrl) ?? Config.TEST_URL);
formatApiUrl(prefs.getString(kSharedPrefUrl) ?? Config.TEST_URL);
final String token = prefs.getString(getCompanyTokenKey());
repository

View File

@ -28,7 +28,7 @@ AuthState userLoginRequestReducer(
AuthState authState, UserLoginRequest action) {
return authState.rebuild((b) => b
..error = null
..url = formatApiUrlMachine(action.url)
..url = formatApiUrl(action.url)
..secret = action.secret
..email = action.email
..password = action.password);
@ -38,7 +38,7 @@ AuthState oauthLoginRequestReducer(
AuthState authState, OAuthLoginRequest action) {
return authState.rebuild((b) => b
..error = null
..url = formatApiUrlMachine(action.url)
..url = formatApiUrl(action.url)
..secret = action.secret);
}

View File

@ -41,10 +41,10 @@ class _LoginState extends State<LoginView> {
final state = widget.viewModel.authState;
_emailController.text = state.email;
_passwordController.text = state.password;
_urlController.text = formatApiUrlReadable(state.url);
_urlController.text = cleanApiUrl(state.url);
_secretController.text = state.secret;
if (state.url.isNotEmpty) {
if (cleanApiUrl(state.url).isNotEmpty) {
_isSelfHosted = true;
}

View File

@ -277,9 +277,9 @@ String formatDate(String value, BuildContext context,
}
}
String formatApiUrlMachine(String url) => formatApiUrlReadable(url) + '/api/v1';
String formatApiUrl(String url) => cleanApiUrl(url) + '/api/v1';
String formatApiUrlReadable(String url) => url
String cleanApiUrl(String url) => url
.trim()
.replaceFirst(RegExp(r'/api/v1'), '')
.replaceFirst(RegExp(r'/$'), '');