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()); showCertificateDialog(event.getX509Certificate(), event.getMagicTrustManager(), event.getSslErrorHandler());
} }
;
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();

View file

@ -20,6 +20,8 @@
package com.nextcloud.talk.controllers; package com.nextcloud.talk.controllers;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; 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.controllers.base.BaseController;
import com.nextcloud.talk.persistence.entities.UserEntity; import com.nextcloud.talk.persistence.entities.UserEntity;
import com.nextcloud.talk.utils.ColorUtils; 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.database.user.UserUtils;
import com.nextcloud.talk.utils.glide.GlideApp; import com.nextcloud.talk.utils.glide.GlideApp;
import com.nextcloud.talk.utils.preferences.AppPreferences; import com.nextcloud.talk.utils.preferences.AppPreferences;
import com.nextcloud.talk.utils.preferences.MagicUserInputModule; import com.nextcloud.talk.utils.preferences.MagicUserInputModule;
import com.yarolegovich.mp.MaterialChoicePreference; import com.yarolegovich.mp.MaterialChoicePreference;
import com.yarolegovich.mp.MaterialEditTextPreference; import com.yarolegovich.mp.MaterialEditTextPreference;
import com.yarolegovich.mp.MaterialPreferenceCategory;
import com.yarolegovich.mp.MaterialPreferenceScreen; import com.yarolegovich.mp.MaterialPreferenceScreen;
import com.yarolegovich.mp.MaterialStandardPreference; import com.yarolegovich.mp.MaterialStandardPreference;
import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -105,6 +111,15 @@ public class SettingsController extends BaseController {
@BindView(R.id.settings_add_account) @BindView(R.id.settings_add_account)
MaterialStandardPreference addAccountButton; MaterialStandardPreference addAccountButton;
@BindView(R.id.message_view)
MaterialPreferenceCategory messageView;
@BindView(R.id.message_text)
TextView messageText;
@Inject
EventBus eventBus;
@Inject @Inject
AppPreferences appPreferences; AppPreferences appPreferences;
@ -215,8 +230,8 @@ public class SettingsController extends BaseController {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
getParentController().getRouter().pushController(RouterTransaction.with( getParentController().getRouter().pushController(RouterTransaction.with(
new WebViewLoginController(userEntity.getBaseUrl(), new WebViewLoginController(userEntity.getBaseUrl(), true))
true)).pushChangeHandler(new VerticalChangeHandler()) .pushChangeHandler(new VerticalChangeHandler())
.popChangeHandler(new VerticalChangeHandler())); .popChangeHandler(new VerticalChangeHandler()));
} }
}); });
@ -229,6 +244,39 @@ public class SettingsController extends BaseController {
.popChangeHandler(new VerticalChangeHandler())); .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 @Override

View file

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

View file

@ -28,7 +28,6 @@ import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry; import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule; import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader; 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.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule; import com.bumptech.glide.module.AppGlideModule;
import com.nextcloud.talk.application.NextcloudTalkApplication; import com.nextcloud.talk.application.NextcloudTalkApplication;
@ -43,9 +42,6 @@ import okhttp3.OkHttpClient;
@AutoInjector(NextcloudTalkApplication.class) @AutoInjector(NextcloudTalkApplication.class)
@GlideModule @GlideModule
public class CachingGlideModule extends AppGlideModule { public class CachingGlideModule extends AppGlideModule {
// 256 MB
private static final int IMAGE_CACHE_SIZE = 256 * 1024 * 1024;
@Inject @Inject
OkHttpClient okHttpClient; OkHttpClient okHttpClient;
@ -57,6 +53,5 @@ public class CachingGlideModule extends AppGlideModule {
@Override @Override
public void applyOptions(Context context, GlideBuilder builder) { 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_width="match_parent"
android:layout_height="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 <com.yarolegovich.mp.MaterialPreferenceCategory
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -56,30 +70,30 @@
android:id="@+id/settings_switch" android:id="@+id/settings_switch"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
apc:mp_title="@string/nc_settings_switch_account"
android:layout_below="@id/display_name_text" 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 <com.yarolegovich.mp.MaterialStandardPreference
android:id="@+id/settings_reauthorize" android:id="@+id/settings_reauthorize"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" 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 <com.yarolegovich.mp.MaterialStandardPreference
android:id="@+id/settings_remove_account" android:id="@+id/settings_remove_account"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" 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 <com.yarolegovich.mp.MaterialStandardPreference
android:id="@+id/settings_add_account" android:id="@+id/settings_add_account"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" 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> </RelativeLayout>

View file

@ -47,6 +47,8 @@
<string name="nc_settings_reauthorize">Reauthorize</string> <string name="nc_settings_reauthorize">Reauthorize</string>
<string name="nc_settings_remove_account">Remove account</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_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_no_proxy">No proxy</string>
<string name="nc_username">Username</string> <string name="nc_username">Username</string>