Some work on settings

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-11-06 23:43:11 +01:00
parent ae40b213f7
commit 643672a312
8 changed files with 167 additions and 48 deletions

View file

@ -169,8 +169,6 @@ public final class MainActivity extends AppCompatActivity implements ActionBarPr
showCertificateDialog(event.getX509Certificate(), event.getMagicTrustManager(), event.getSslErrorHandler());
}
;
@Override
public void onStart() {
super.onStart();

View file

@ -20,6 +20,8 @@
package com.nextcloud.talk.controllers;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
@ -40,17 +42,21 @@ import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.controllers.base.BaseController;
import com.nextcloud.talk.persistence.entities.UserEntity;
import com.nextcloud.talk.utils.ColorUtils;
import com.nextcloud.talk.utils.SettingsMessageHolder;
import com.nextcloud.talk.utils.database.user.UserUtils;
import com.nextcloud.talk.utils.glide.GlideApp;
import com.nextcloud.talk.utils.preferences.AppPreferences;
import com.nextcloud.talk.utils.preferences.MagicUserInputModule;
import com.yarolegovich.mp.MaterialChoicePreference;
import com.yarolegovich.mp.MaterialEditTextPreference;
import com.yarolegovich.mp.MaterialPreferenceCategory;
import com.yarolegovich.mp.MaterialPreferenceScreen;
import com.yarolegovich.mp.MaterialStandardPreference;
import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -105,6 +111,15 @@ public class SettingsController extends BaseController {
@BindView(R.id.settings_add_account)
MaterialStandardPreference addAccountButton;
@BindView(R.id.message_view)
MaterialPreferenceCategory messageView;
@BindView(R.id.message_text)
TextView messageText;
@Inject
EventBus eventBus;
@Inject
AppPreferences appPreferences;
@ -215,8 +230,8 @@ public class SettingsController extends BaseController {
@Override
public void onClick(View view) {
getParentController().getRouter().pushController(RouterTransaction.with(
new WebViewLoginController(userEntity.getBaseUrl(),
true)).pushChangeHandler(new VerticalChangeHandler())
new WebViewLoginController(userEntity.getBaseUrl(), true))
.pushChangeHandler(new VerticalChangeHandler())
.popChangeHandler(new VerticalChangeHandler()));
}
});
@ -229,6 +244,39 @@ public class SettingsController extends BaseController {
.popChangeHandler(new VerticalChangeHandler()));
}
});
if (SettingsMessageHolder.getInstance().getMessageType() != null) {
switch (SettingsMessageHolder.getInstance().getMessageType()) {
case ACCOUNT_UPDATED_NOT_ADDED:
messageText.setText(getResources().getString(R.string.nc_settings_account_updated));
messageView.setVisibility(View.VISIBLE);
break;
case WRONG_ACCOUNT:
messageText.setText(getResources().getString(R.string.nc_settings_wrong_account));
messageView.setVisibility(View.VISIBLE);
break;
default:
messageView.setVisibility(View.GONE);
break;
}
SettingsMessageHolder.getInstance().setMessageType(null);
messageView.animate()
.translationY(0)
.alpha(0.0f)
.setDuration(2000)
.setStartDelay(5000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
messageView.setVisibility(View.GONE);
}
});
} else {
messageView.setVisibility(View.GONE);
}
}
@Override

View file

@ -45,6 +45,7 @@ import com.nextcloud.talk.controllers.base.BaseController;
import com.nextcloud.talk.events.CertificateEvent;
import com.nextcloud.talk.models.LoginData;
import com.nextcloud.talk.persistence.entities.UserEntity;
import com.nextcloud.talk.utils.SettingsMessageHolder;
import com.nextcloud.talk.utils.bundle.BundleBuilder;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils;
@ -81,6 +82,8 @@ public class WebViewLoginController extends BaseController {
ReactiveEntityStore<Persistable> dataStore;
@Inject
MagicTrustManager magicTrustManager;
@Inject
EventBus eventBus;
@BindView(R.id.webview)
WebView webView;
@ -160,7 +163,10 @@ public class WebViewLoginController extends BaseController {
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
webView.setVisibility(View.VISIBLE);
if (webView != null) {
webView.setVisibility(View.VISIBLE);
}
basePageLoaded = true;
}
@ -182,7 +188,7 @@ public class WebViewLoginController extends BaseController {
magicTrustManager.checkServerTrusted(new X509Certificate[]{cert}, "generic");
handler.proceed();
} catch (CertificateException exception) {
EventBus.getDefault().post(new CertificateEvent(cert, magicTrustManager, handler));
eventBus.post(new CertificateEvent(cert, magicTrustManager, handler));
}
}
} catch (Exception exception) {
@ -215,38 +221,40 @@ public class WebViewLoginController extends BaseController {
UserEntity currentUser = userUtils.getCurrentUser();
String displayName = null;
String pushConfiguration = null;
SettingsMessageHolder.SettingsMessageType settingsMessageType = null;
if (currentUser != null && isPasswordUpdate &&
!currentUser.getUsername().equals(loginData.getUsername())) {
SettingsMessageHolder.getInstance().setMessageType(
SettingsMessageHolder.SettingsMessageType.WRONG_ACCOUNT);
getRouter().popToRoot();
} else {
if (currentUser != null) {
displayName = currentUser.getDisplayName();
pushConfiguration = currentUser.getPushConfigurationState();
}
if (!isPasswordUpdate && userUtils.getIfUserWithUsernameAndServer(loginData.getUsername(), baseUrl)) {
settingsMessageType = SettingsMessageHolder.SettingsMessageType.ACCOUNT_UPDATED_NOT_ADDED;
}
// We use the URL user entered because one provided by the server is NOT reliable
userQueryDisposable = userUtils.createOrUpdateUser(loginData.getUsername(), loginData.getToken(),
baseUrl, displayName, pushConfiguration, true).
subscribe(userEntity -> {
if (!isPasswordUpdate) {
BundleBuilder bundleBuilder = new BundleBuilder(new Bundle());
bundleBuilder.putString(BundleKeys.KEY_USERNAME, userEntity.getUsername());
bundleBuilder.putString(BundleKeys.KEY_TOKEN, userEntity.getToken());
bundleBuilder.putString(BundleKeys.KEY_BASE_URL, userEntity.getBaseUrl());
getRouter().pushController(RouterTransaction.with(new AccountVerificationController
(bundleBuilder.build())).pushChangeHandler(new HorizontalChangeHandler())
.popChangeHandler(new HorizontalChangeHandler()));
} else {
if (getRouter().hasRootController()) {
getRouter().popToRoot();
} else {
getRouter().setRoot(RouterTransaction.with(
new BottomNavigationController(R.menu.menu_navigation)
).pushChangeHandler(new HorizontalChangeHandler())
// We use the URL user entered because one provided by the server is NOT reliable
SettingsMessageHolder.SettingsMessageType finalSettingsMessageType = settingsMessageType;
userQueryDisposable = userUtils.createOrUpdateUser(loginData.getUsername(), loginData.getToken(),
baseUrl, null, null, true).
subscribe(userEntity -> {
if (!isPasswordUpdate && !userUtils.anyUserExists()) {
BundleBuilder bundleBuilder = new BundleBuilder(new Bundle());
bundleBuilder.putString(BundleKeys.KEY_USERNAME, userEntity.getUsername());
bundleBuilder.putString(BundleKeys.KEY_TOKEN, userEntity.getToken());
bundleBuilder.putString(BundleKeys.KEY_BASE_URL, userEntity.getBaseUrl());
getRouter().pushController(RouterTransaction.with(new AccountVerificationController
(bundleBuilder.build())).pushChangeHandler(new HorizontalChangeHandler())
.popChangeHandler(new HorizontalChangeHandler()));
} else {
if (finalSettingsMessageType != null) {
SettingsMessageHolder.getInstance().setMessageType(finalSettingsMessageType);
}
getRouter().popToRoot();
}
}
}, throwable -> dispose(),
this::dispose);
}, throwable -> dispose(),
this::dispose);
}
}
}

View file

@ -0,0 +1,47 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils;
import android.support.annotation.Nullable;
public class SettingsMessageHolder {
public enum SettingsMessageType {
WRONG_ACCOUNT, ACCOUNT_UPDATED_NOT_ADDED
}
private SettingsMessageType settingsMessageType;
private static final SettingsMessageHolder holder = new SettingsMessageHolder();
public static SettingsMessageHolder getInstance() {
return holder;
}
public SettingsMessageType getMessageType() {
return settingsMessageType;
}
public void setMessageType(@Nullable SettingsMessageType settingsMessageType) {
this.settingsMessageType = settingsMessageType;
}
}

View file

@ -86,6 +86,13 @@ public class UserUtils {
}
}
public boolean getIfUserWithUsernameAndServer(String username, String server) {
Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.USERNAME.eq(username)
.and(UserEntity.BASE_URL.eq(server.toLowerCase())))
.limit(1).get();
return findUserQueryResult.firstOrNull() != null;
}
public Observable<UserEntity> createOrUpdateUser(String username, String token, String serverUrl,
@Nullable String displayName,
@Nullable String pushConfigurationState,
@ -97,7 +104,7 @@ public class UserUtils {
if (user == null) {
user = new UserEntity();
user.setBaseUrl(serverUrl);
user.setBaseUrl(serverUrl.toLowerCase());
user.setUsername(username);
user.setToken(token);

View file

@ -28,7 +28,6 @@ import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.nextcloud.talk.application.NextcloudTalkApplication;
@ -43,9 +42,6 @@ import okhttp3.OkHttpClient;
@AutoInjector(NextcloudTalkApplication.class)
@GlideModule
public class CachingGlideModule extends AppGlideModule {
// 256 MB
private static final int IMAGE_CACHE_SIZE = 256 * 1024 * 1024;
@Inject
OkHttpClient okHttpClient;
@ -57,6 +53,5 @@ public class CachingGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, IMAGE_CACHE_SIZE));
}
}

View file

@ -25,6 +25,20 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yarolegovich.mp.MaterialPreferenceCategory
android:id="@+id/message_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<TextView
android:id="@+id/message_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/darkRed"/>
</com.yarolegovich.mp.MaterialPreferenceCategory>
<com.yarolegovich.mp.MaterialPreferenceCategory
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -56,30 +70,30 @@
android:id="@+id/settings_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
apc:mp_title="@string/nc_settings_switch_account"
android:layout_below="@id/display_name_text"
android:tag="switchAccountButton"/>
android:tag="switchAccountButton"
apc:mp_title="@string/nc_settings_switch_account"/>
<com.yarolegovich.mp.MaterialStandardPreference
android:id="@+id/settings_reauthorize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
apc:mp_title="@string/nc_settings_reauthorize"
android:layout_below="@id/settings_switch"/>
android:layout_below="@id/settings_switch"
apc:mp_title="@string/nc_settings_reauthorize"/>
<com.yarolegovich.mp.MaterialStandardPreference
android:id="@+id/settings_remove_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
apc:mp_title="@string/nc_settings_remove_account"
android:layout_below="@id/settings_reauthorize"/>
android:layout_below="@id/settings_reauthorize"
apc:mp_title="@string/nc_settings_remove_account"/>
<com.yarolegovich.mp.MaterialStandardPreference
android:id="@+id/settings_add_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
apc:mp_title="@string/nc_settings_add_account"
android:layout_below="@id/settings_remove_account"/>
android:layout_below="@id/settings_remove_account"
apc:mp_title="@string/nc_settings_add_account"/>
</RelativeLayout>

View file

@ -47,6 +47,8 @@
<string name="nc_settings_reauthorize">Reauthorize</string>
<string name="nc_settings_remove_account">Remove account</string>
<string name="nc_settings_add_account">Add a new account</string>
<string name="nc_settings_wrong_account">Only current account can be reauthorized</string>
<string name="nc_settings_account_updated">We updated your existing account instead of adding a new one since it already exists</string>
<string name="nc_no_proxy">No proxy</string>
<string name="nc_username">Username</string>