Commit 69bb6a50 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Convert LoginPresenter and LoginFragment to Kotlin

parent 346e21c5
package chat.rocket.android.fragment.server_config;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.layouthelper.oauth.OAuthProviderInfo;
import chat.rocket.android.log.RCLog;
import chat.rocket.core.models.LoginServiceConfiguration;
import chat.rocket.persistence.realm.repositories.RealmLoginServiceConfigurationRepository;
import chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository;
/**
* Login screen.
*/
public class LoginFragment extends AbstractServerConfigFragment implements LoginContract.View {
private LoginContract.Presenter presenter;
private ConstraintLayout container;
private View waitingView;
private TextView txtUsername;
private TextView txtPasswd;
@Override
protected int getLayout() {
return R.layout.fragment_login;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = new LoginPresenter(
new RealmLoginServiceConfigurationRepository(hostname),
new RealmPublicSettingRepository(hostname),
new MethodCallHelper(getContext(), hostname)
);
}
@Override
protected void onSetupView() {
container = rootView.findViewById(R.id.container);
Button btnEmail = rootView.findViewById(R.id.btn_login_with_email);
Button btnUserRegistration = rootView.findViewById(R.id.btn_user_registration);
txtUsername = rootView.findViewById(R.id.editor_username);
txtPasswd = rootView.findViewById(R.id.editor_passwd);
waitingView = rootView.findViewById(R.id.waiting);
btnEmail.setOnClickListener(view ->
presenter.login(txtUsername.getText().toString(), txtPasswd.getText().toString()));
btnUserRegistration.setOnClickListener(view ->
UserRegistrationDialogFragment.create(hostname, txtUsername.getText().toString(), txtPasswd.getText().toString())
.show(getFragmentManager(), "UserRegistrationDialogFragment"));
}
@Override
public void showLoader() {
container.setVisibility(View.GONE);
waitingView.setVisibility(View.VISIBLE);
}
@Override
public void hideLoader() {
waitingView.setVisibility(View.GONE);
container.setVisibility(View.VISIBLE);
}
@Override
public void showError(String message) {
Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT).show();
}
@Override
public void showLoginServices(List<LoginServiceConfiguration> loginServiceList) {
HashMap<String, View> viewMap = new HashMap<>();
HashMap<String, Boolean> supportedMap = new HashMap<>();
for (OAuthProviderInfo info : OAuthProviderInfo.LIST) {
viewMap.put(info.serviceName, rootView.findViewById(info.buttonId));
supportedMap.put(info.serviceName, false);
}
for (LoginServiceConfiguration authProvider : loginServiceList) {
for (OAuthProviderInfo info : OAuthProviderInfo.LIST) {
if (!supportedMap.get(info.serviceName)
&& info.serviceName.equals(authProvider.getService())) {
supportedMap.put(info.serviceName, true);
viewMap.get(info.serviceName).setOnClickListener(view -> {
Fragment fragment = null;
try {
fragment = info.fragmentClass.newInstance();
} catch (Exception exception) {
RCLog.w(exception, "failed to build new Fragment");
}
if (fragment != null) {
Bundle args = new Bundle();
args.putString("hostname", hostname);
fragment.setArguments(args);
showFragmentWithBackStack(fragment);
}
});
viewMap.get(info.serviceName).setVisibility(View.VISIBLE);
}
}
}
for (OAuthProviderInfo info : OAuthProviderInfo.LIST) {
if (!supportedMap.get(info.serviceName)) {
viewMap.get(info.serviceName).setVisibility(View.GONE);
}
}
}
@Override
public void showTwoStepAuth() {
showFragmentWithBackStack(TwoStepAuthFragment.create(
hostname, txtUsername.getText().toString(), txtPasswd.getText().toString()
));
}
@Override
public void onResume() {
super.onResume();
presenter.bindView(this);
}
@Override
public void onPause() {
presenter.release();
super.onPause();
}
}
package chat.rocket.android.fragment.server_config
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.view.View
import android.widget.Button
import android.widget.TextView
import java.util.HashMap
import chat.rocket.android.R
import chat.rocket.android.api.MethodCallHelper
import chat.rocket.android.layouthelper.oauth.OAuthProviderInfo
import chat.rocket.android.log.RCLog
import chat.rocket.core.models.LoginServiceConfiguration
import chat.rocket.persistence.realm.repositories.RealmLoginServiceConfigurationRepository
import chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository
/**
* Login screen.
*/
class LoginFragment : AbstractServerConfigFragment(), LoginContract.View {
private var presenter: LoginContract.Presenter? = null
private var container: ConstraintLayout? = null
private var waitingView: View? = null
private var txtUsername: TextView? = null
private var txtPasswd: TextView? = null
override fun getLayout(): Int {
return R.layout.fragment_login
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter = LoginPresenter(
RealmLoginServiceConfigurationRepository(hostname),
RealmPublicSettingRepository(hostname),
MethodCallHelper(context, hostname)
)
}
override fun onSetupView() {
container = rootView.findViewById(R.id.container)
val btnEmail = rootView.findViewById<Button>(R.id.btn_login_with_email)
val btnUserRegistration = rootView.findViewById<Button>(R.id.btn_user_registration)
txtUsername = rootView.findViewById(R.id.editor_username)
txtPasswd = rootView.findViewById(R.id.editor_passwd)
waitingView = rootView.findViewById(R.id.waiting)
btnEmail.setOnClickListener { view -> presenter!!.login(txtUsername!!.text.toString(), txtPasswd!!.text.toString()) }
btnUserRegistration.setOnClickListener { view ->
UserRegistrationDialogFragment.create(hostname, txtUsername!!.text.toString(), txtPasswd!!.text.toString())
.show(fragmentManager!!, "UserRegistrationDialogFragment")
}
}
override fun showLoader() {
container!!.visibility = View.GONE
waitingView!!.visibility = View.VISIBLE
}
override fun hideLoader() {
waitingView!!.visibility = View.GONE
container!!.visibility = View.VISIBLE
}
override fun showError(message: String) {
Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT).show()
}
override fun showLoginServices(loginServiceList: List<LoginServiceConfiguration>) {
val viewMap = HashMap<String, View>()
val supportedMap = HashMap<String, Boolean>()
for (info in OAuthProviderInfo.LIST) {
viewMap.put(info.serviceName, rootView.findViewById(info.buttonId))
supportedMap.put(info.serviceName, false)
}
for (authProvider in loginServiceList) {
for (info in OAuthProviderInfo.LIST) {
if (!supportedMap[info.serviceName] && info.serviceName == authProvider.service) {
supportedMap.put(info.serviceName, true)
viewMap[info.serviceName].setOnClickListener { view ->
var fragment: Fragment? = null
try {
fragment = info.fragmentClass.newInstance()
} catch (exception: Exception) {
RCLog.w(exception, "failed to build new Fragment")
}
if (fragment != null) {
val args = Bundle()
args.putString("hostname", hostname)
fragment.arguments = args
showFragmentWithBackStack(fragment)
}
}
viewMap[info.serviceName].setVisibility(View.VISIBLE)
}
}
}
for (info in OAuthProviderInfo.LIST) {
if (!supportedMap[info.serviceName]) {
viewMap[info.serviceName].setVisibility(View.GONE)
}
}
}
override fun showTwoStepAuth() {
showFragmentWithBackStack(TwoStepAuthFragment.create(
hostname, txtUsername!!.text.toString(), txtPasswd!!.text.toString()
))
}
override fun onResume() {
super.onResume()
presenter!!.bindView(this)
}
override fun onPause() {
presenter!!.release()
super.onPause()
}
}
package chat.rocket.android.fragment.server_config;
import android.support.annotation.NonNull;
import com.hadisatrio.optional.Optional;
import bolts.Task;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.api.TwoStepAuthException;
import chat.rocket.android.helper.Logger;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.PublicSettingsConstants;
import chat.rocket.core.models.PublicSetting;
import chat.rocket.core.repositories.LoginServiceConfigurationRepository;
import chat.rocket.core.repositories.PublicSettingRepository;
import io.reactivex.android.schedulers.AndroidSchedulers;
public class LoginPresenter extends BasePresenter<LoginContract.View>
implements LoginContract.Presenter {
private final LoginServiceConfigurationRepository loginServiceConfigurationRepository;
private final PublicSettingRepository publicSettingRepository;
private final MethodCallHelper methodCallHelper;
public LoginPresenter(LoginServiceConfigurationRepository loginServiceConfigurationRepository,
PublicSettingRepository publicSettingRepository,
MethodCallHelper methodCallHelper) {
this.loginServiceConfigurationRepository = loginServiceConfigurationRepository;
this.publicSettingRepository = publicSettingRepository;
this.methodCallHelper = methodCallHelper;
}
@Override
public void bindView(@NonNull LoginContract.View view) {
super.bindView(view);
getLoginServices();
}
@Override
public void login(String username, String password) {
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
return;
}
view.showLoader();
addSubscription(
publicSettingRepository.getById(PublicSettingsConstants.LDAP.ENABLE)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
publicSettingOptional -> doLogin(username, password, publicSettingOptional),
Logger.INSTANCE::report
)
);
}
private void getLoginServices() {
addSubscription(
loginServiceConfigurationRepository.getAll()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
loginServiceConfigurations -> view.showLoginServices(loginServiceConfigurations),
Logger.INSTANCE::report
)
);
}
private void doLogin(String username, String password, Optional<PublicSetting> optional) {
call(username, password, optional)
.continueWith(task -> {
if (task.isFaulted()) {
view.hideLoader();
final Exception error = task.getError();
if (error instanceof TwoStepAuthException) {
view.showTwoStepAuth();
} else {
view.showError(error.getMessage());
}
}
return null;
}, Task.UI_THREAD_EXECUTOR);
}
private Task<Void> call(String username, String password, Optional<PublicSetting> optional) {
if (optional.isPresent() && optional.get().getValueAsBoolean()) {
return methodCallHelper.loginWithLdap(username, password);
}
return methodCallHelper.loginWithEmail(username, password);
}
}
package chat.rocket.android.fragment.server_config
import com.hadisatrio.optional.Optional
import bolts.Task
import chat.rocket.android.BackgroundLooper
import chat.rocket.android.api.MethodCallHelper
import chat.rocket.android.api.TwoStepAuthException
import chat.rocket.android.helper.Logger
import chat.rocket.android.helper.TextUtils
import chat.rocket.android.shared.BasePresenter
import chat.rocket.core.PublicSettingsConstants
import chat.rocket.core.models.PublicSetting
import chat.rocket.core.repositories.LoginServiceConfigurationRepository
import chat.rocket.core.repositories.PublicSettingRepository
import io.reactivex.android.schedulers.AndroidSchedulers
class LoginPresenter(private val loginServiceConfigurationRepository: LoginServiceConfigurationRepository,
private val publicSettingRepository: PublicSettingRepository,
private val methodCallHelper: MethodCallHelper) : BasePresenter<LoginContract.View>(), LoginContract.Presenter {
override fun bindView(view: LoginContract.View) {
super.bindView(view)
getLoginServices()
}
override fun login(username: String, password: String) {
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
return
}
view.showLoader()
addSubscription(
publicSettingRepository.getById(PublicSettingsConstants.LDAP.ENABLE)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ publicSettingOptional -> doLogin(username, password, publicSettingOptional) },
Consumer<Throwable> { Logger.report(it) }
)
)
}
private fun getLoginServices() {
addSubscription(
loginServiceConfigurationRepository.all
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ loginServiceConfigurations -> view.showLoginServices(loginServiceConfigurations) },
Consumer<Throwable> { Logger.report(it) }
)
)
}
private fun doLogin(username: String, password: String, optional: Optional<PublicSetting>) {
call(username, password, optional)
.continueWith<Any>({ task ->
if (task.isFaulted()) {
view.hideLoader()
val error = task.getError()
if (error is TwoStepAuthException) {
view.showTwoStepAuth()
} else {
view.showError(error.message)
}
}
null
}, Task.UI_THREAD_EXECUTOR)
}
private fun call(username: String, password: String, optional: Optional<PublicSetting>): Task<Void> {
return if (optional.isPresent && optional.get().valueAsBoolean) {
methodCallHelper.loginWithLdap(username, password)
} else methodCallHelper.loginWithEmail(username, password)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment