Fixed user presence system (now user availability/unavailability depends on the life cycle of the MainActivity

This commit is contained in:
ChronosX88 2019-05-25 09:02:59 +04:00
parent 8b9c07de89
commit 0c99ac4db1
5 changed files with 41 additions and 8 deletions

View File

@ -125,7 +125,6 @@ public class XMPPConnection implements ConnectionListener {
if(mamManager.isSupported()) { if(mamManager.isSupported()) {
MamManager.getInstanceFor(connection).enableMamForAllMessages(); MamManager.getInstanceFor(connection).enableMamForAllMessages();
} }
connection.sendStanza(new Presence(Presence.Type.available));
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -224,4 +223,18 @@ public class XMPPConnection implements ConnectionListener {
public Presence getUserPresence(BareJid jid) { public Presence getUserPresence(BareJid jid) {
return roster.getPresence(jid); return roster.getPresence(jid);
} }
public void sendUserPresence(Presence presence) {
if(connection != null) {
if(isConnectionAlive()) {
try {
connection.sendStanza(presence);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} }

View File

@ -75,13 +75,6 @@ public class XMPPConnectionService extends Service {
if(connection != null) { if(connection != null) {
thread.interrupt(); thread.interrupt();
thread = null; thread = null;
try {
connection.getConnection().sendStanza(new Presence(Presence.Type.unavailable));
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
connection.disconnect(); connection.disconnect();
connection = null; connection = null;
} }

View File

@ -66,6 +66,7 @@ interface CoreContracts {
fun logoutFromAccount() fun logoutFromAccount()
fun onStart() fun onStart()
fun onStop() fun onStop()
fun onDestroy()
} }
interface IMainViewContract { interface IMainViewContract {

View File

@ -25,9 +25,13 @@ import io.github.chronosx88.influence.logic.MainLogic
import io.github.chronosx88.influence.models.appEvents.AuthenticationStatusEvent import io.github.chronosx88.influence.models.appEvents.AuthenticationStatusEvent
import io.github.chronosx88.influence.models.appEvents.NewChatEvent import io.github.chronosx88.influence.models.appEvents.NewChatEvent
import io.github.chronosx88.influence.views.LoginActivity import io.github.chronosx88.influence.views.LoginActivity
import java9.util.concurrent.CompletableFuture
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode import org.greenrobot.eventbus.ThreadMode
import org.jivesoftware.smack.packet.Presence
class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract { class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract {
private val logic: CoreContracts.IMainLogicContract = MainLogic() private val logic: CoreContracts.IMainLogicContract = MainLogic()
@ -54,6 +58,13 @@ class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreCon
AppHelper.getContext().startActivity(intent) AppHelper.getContext().startActivity(intent)
view.finishActivity() view.finishActivity()
} }
AuthenticationStatusEvent.CONNECT_AND_LOGIN_SUCCESSFUL -> {
GlobalScope.launch {
if(AppHelper.getXmppConnection().isConnectionAlive)
AppHelper.getXmppConnection().sendUserPresence(Presence(Presence.Type.available))
}
}
} }
} }
@ -68,4 +79,13 @@ class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreCon
override fun onStop() { override fun onStop() {
EventBus.getDefault().unregister(this) EventBus.getDefault().unregister(this)
} }
override fun onDestroy() {
GlobalScope.launch {
if(AppHelper.getXmppConnection() != null) {
if(AppHelper.getXmppConnection().isConnectionAlive)
AppHelper.getXmppConnection().sendUserPresence(Presence(Presence.Type.unavailable))
}
}
}
} }

View File

@ -164,4 +164,10 @@ public class MainActivity extends AppCompatActivity implements CoreContracts.IMa
public void finishActivity() { public void finishActivity() {
finish(); finish();
} }
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestroy();
}
} }