Added logout system. Fixed bug when logging in (when network error). Code cleanup.

This commit is contained in:
ChronosX88 2019-05-23 15:23:03 +04:00
parent bc5583104f
commit 398adfeece
19 changed files with 123 additions and 52 deletions

View File

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="org.greenrobot.eventbus.Subscribe" />
</list>
</component>
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />

View File

@ -92,6 +92,7 @@ public class XMPPConnection implements ConnectionListener {
.setKeystoreType(null)
.setSecurityMode(ConnectionConfiguration.SecurityMode.required)
.setCompressionEnabled(true)
.setConnectTimeout(7000)
.build();
connection = new XMPPTCPConnection(conf);
@ -104,6 +105,8 @@ public class XMPPConnection implements ConnectionListener {
connection.login(credentials.username, credentials.password);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (NullPointerException e) {
throw new IOException();
}
ChatManager.getInstanceFor(connection).addIncomingListener(networkHandler);

View File

@ -72,10 +72,10 @@ public class XMPPConnectionService extends Service {
isThreadAlive = false;
threadHandler.post(() -> {
if(connection != null) {
connection.disconnect();
connection = null;
thread.interrupt();
thread = null;
connection.disconnect();
connection = null;
}
});
}

View File

@ -41,11 +41,15 @@ interface CoreContracts {
interface IMainLogicContract {
fun startService()
fun logout()
}
interface IMainPresenterContract {
fun initConnection()
fun startChatWithPeer(username: String)
fun logoutFromAccount()
fun onStart()
fun onStop()
}
interface IMainViewContract {

View File

@ -82,6 +82,12 @@ public class AppHelper extends MultiDexApplication {
AppHelper.setJid(currentLoginCredentials.username + "@" + currentLoginCredentials.jabberHost);
}
public static void resetLoginCredentials() {
currentLoginCredentials = new LoginCredentials();
preferences.edit().remove("jid").apply();
preferences.edit().remove("pass").apply();
}
private static void initTrueTime() {
new Thread(() -> {
boolean isTrueTimeIsOn = false;
@ -106,11 +112,11 @@ public class AppHelper extends MultiDexApplication {
return mainUIThreadHandler;
}
public static ServiceConnection getServiceConnection() {
return serviceConnection;
}
public static void setServiceConnection(ServiceConnection serviceConnection) {
AppHelper.serviceConnection = serviceConnection;
}
public static ServiceConnection getServiceConnection() {
return serviceConnection;
}
}

View File

@ -58,4 +58,9 @@ public class LocalDBWrapper {
public static void updateMessage(MessageEntity messageEntity) {
dbInstance.messageDao().updateMessage(messageEntity);
}
public static void clearDatabase() {
dbInstance.messageDao().clearMessages();
dbInstance.chatDao().clearChats();
}
}

View File

@ -1,22 +0,0 @@
/*
* Copyright (C) 2019 ChronosX88
*
* 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 <https://www.gnu.org/licenses/>.
*/
package io.github.chronosx88.influence.helpers;
public class ObservableActions {
public static final int NEW_CHAT_CREATED = 0x0;
}

View File

@ -9,6 +9,7 @@ import android.os.IBinder;
import io.github.chronosx88.influence.XMPPConnectionService;
import io.github.chronosx88.influence.contracts.CoreContracts;
import io.github.chronosx88.influence.helpers.AppHelper;
import io.github.chronosx88.influence.helpers.LocalDBWrapper;
public class MainLogic implements CoreContracts.IMainLogicContract {
private static final String LOG_TAG = MainLogic.class.getName();
@ -34,6 +35,15 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
AppHelper.setXmppConnection(null);
}
};
AppHelper.setServiceConnection(connection);
context.bindService(new Intent(context, XMPPConnectionService.class), connection,Context.BIND_AUTO_CREATE);
}
@Override
public void logout() {
LocalDBWrapper.clearDatabase();
AppHelper.resetLoginCredentials();
context.unbindService(AppHelper.getServiceConnection());
context.stopService(new Intent(context, XMPPConnectionService.class));
}
}

View File

@ -25,4 +25,7 @@ public interface ChatDao {
@Update
void updateChat(ChatEntity chat);
@Query("DELETE FROM chats")
void clearChats();
}

View File

@ -28,4 +28,7 @@ public interface MessageDao {
@Update
void updateMessage(MessageEntity message);
@Query("DELETE FROM messages")
void clearMessages();
}

View File

@ -1,3 +1,20 @@
/*
* Copyright (C) 2019 ChronosX88
*
* 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 <https://www.gnu.org/licenses/>.
*/
package io.github.chronosx88.influence.presenters;
import android.content.Intent;

View File

@ -15,10 +15,6 @@ import org.greenrobot.eventbus.ThreadMode
class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract {
private val logic: CoreContracts.IMainLogicContract = MainLogic()
init {
EventBus.getDefault().register(this)
}
override fun initConnection() {
logic.startService()
}
@ -38,4 +34,16 @@ class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreCon
}
}
}
override fun logoutFromAccount() {
logic.logout()
}
override fun onStart() {
EventBus.getDefault().register(this)
}
override fun onStop() {
EventBus.getDefault().unregister(this)
}
}

View File

@ -166,11 +166,13 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL
case AuthenticationStatusEvent.INCORRECT_LOGIN_OR_PASSWORD: {
loadingScreen(false);
passwordInputLayout.setError("Invalid JID/Password");
AppHelper.resetLoginCredentials();
break;
}
case AuthenticationStatusEvent.NETWORK_ERROR: {
loadingScreen(false);
jidInputLayout.setError("Network error");
AppHelper.resetLoginCredentials();
break;
}
}

View File

@ -1,6 +1,7 @@
package io.github.chronosx88.influence.views;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
@ -87,6 +88,21 @@ public class MainActivity extends AppCompatActivity implements CoreContracts.IMa
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.menu_logout_item: {
presenter.logoutFromAccount();
startActivity(new Intent(this, LoginActivity.class));
finish();
break;
}
}
return true;
}
@Override
public void showSnackbar(@NotNull String message) {
runOnUiThread(() -> Snackbar.make(getRootView(), message, Snackbar.LENGTH_LONG).show());
@ -115,4 +131,16 @@ public class MainActivity extends AppCompatActivity implements CoreContracts.IMa
return rootView;
}
@Override
protected void onStart() {
super.onStart();
presenter.onStart();
}
@Override
protected void onStop() {
super.onStop();
presenter.onStop();
}
}

View File

@ -26,7 +26,8 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/jid_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:paddingTop="5dp">
<EditText
android:id="@+id/login_jid"
@ -42,7 +43,8 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:paddingTop="10dp">
<EditText
android:id="@+id/login_password"
@ -63,7 +65,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="SIGN IN"
android:textStyle="bold" />
android:text="@string/sign_in_button"/>
</LinearLayout>

View File

@ -16,5 +16,11 @@
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_logout_item"
app:showAsAction="never"
android:title="@string/logout">
</item>
</menu>

View File

@ -1,14 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Influence</string>
<string name="username_saved">Ваше имя пользователя успешно сохранено!</string>
<string name="username_isnt_saved">К сожалению, это имя пользователя занято.</string>
<string name="ok">OK</string>
<string name="cancel">Отмена</string>
<string name="username_settings">Ваше имя пользователя</string>
<string name="username_hint">Имя пользователя</string>
<string name="reconnect_network">Переподключиться к сети</string>
<string name="node_already_running">Узел уже запущен</string>
<string name="input_companion_username">Введите имя пользователя собеседника</string>
<string name="input_companion_username">Введите JID собеседника</string>
<string name="hint_enter_a_message">Введите сообщение...</string>
<string name="logout">Выйти из аккаунта</string>
<string name="sign_in_button">Войти</string>
</resources>

View File

@ -1,13 +1,9 @@
<resources>
<string name="app_name">Influence</string>
<string name="username_saved">Your username saved successfully!</string>
<string name="username_isnt_saved">Sorry, this username is busy.</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="username_settings">Your username</string>
<string name="username_hint">Username</string>
<string name="reconnect_network">Reconnect to the network</string>
<string name="node_already_running">Node already running</string>
<string name="input_companion_username">Input interlocutor\'s username</string>
<string name="input_companion_username">Input interlocutor\'s JID</string>
<string name="hint_enter_a_message">Enter message...</string>
<string name="logout">Log out from account</string>
<string name="sign_in_button">Sign In</string>
</resources>

View File

@ -12,7 +12,7 @@
<item name="alertDialogStyle">@style/CustomAlertStyle</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textStyle">normal</item>