mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-23 03:54:53 +02:00
Temp
This commit is contained in:
parent
dd8f9cf6a3
commit
fbb0a0e978
|
@ -4,7 +4,6 @@ import org.mercury_im.messenger.MercuryImApplication;
|
|||
import org.mercury_im.messenger.di.module.AppModule;
|
||||
import org.mercury_im.messenger.handler.AvatarHandler;
|
||||
import org.mercury_im.messenger.handler.RoomPlainMessageHandler;
|
||||
import org.mercury_im.messenger.handler.RoomRosterHandler;
|
||||
import org.mercury_im.messenger.persistence.room.RoomModule;
|
||||
import org.mercury_im.messenger.service.XmppConnectionService;
|
||||
import org.mercury_im.messenger.ui.MainActivity;
|
||||
|
@ -22,8 +21,15 @@ import javax.inject.Singleton;
|
|||
|
||||
import dagger.Component;
|
||||
|
||||
/**
|
||||
* Main Application Component that binds together all the modules needed for the Android
|
||||
* application.
|
||||
*/
|
||||
@Singleton
|
||||
@Component(modules = {AppModule.class, RoomModule.class})
|
||||
@Component(modules = {
|
||||
AppModule.class,
|
||||
RoomModule.class
|
||||
})
|
||||
public interface AppComponent {
|
||||
|
||||
// Application
|
||||
|
@ -64,8 +70,6 @@ public interface AppComponent {
|
|||
|
||||
// Connectors
|
||||
|
||||
void inject(RoomRosterHandler roomRosterHandler);
|
||||
|
||||
void inject(RoomPlainMessageHandler messageHandler);
|
||||
|
||||
void inject(AvatarHandler avatarHandler);
|
||||
|
|
|
@ -3,13 +3,14 @@ package org.mercury_im.messenger.di.module;
|
|||
import android.app.Application;
|
||||
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.core.di.CenterModule;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
@Module(includes = CenterModule.class)
|
||||
public class AppModule {
|
||||
|
||||
private MercuryImApplication mApplication;
|
||||
|
|
|
@ -15,7 +15,7 @@ public class AvatarHandler implements AvatarListener {
|
|||
private final long accountId;
|
||||
|
||||
@Inject
|
||||
private AvatarRepository avatarRepository;
|
||||
AvatarRepository avatarRepository;
|
||||
|
||||
private UserAvatarManager avatarManager;
|
||||
|
||||
|
|
|
@ -1,131 +0,0 @@
|
|||
package org.mercury_im.messenger.handler;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.room.Transaction;
|
||||
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.roster.Roster;
|
||||
import org.jxmpp.jid.BareJid;
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.jxmpp.jid.FullJid;
|
||||
import org.jxmpp.jid.Jid;
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.persistence.room.dao.ContactAttributesDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.EntityDao;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomContactAttributes;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomEntityModel;
|
||||
import org.mercury_im.messenger.core.MercuryConnection;
|
||||
import org.mercury_im.messenger.core.RosterHandler;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class RoomRosterHandler implements RosterHandler {
|
||||
|
||||
@Inject
|
||||
ContactAttributesDao contactAttributesDao;
|
||||
|
||||
@Inject
|
||||
EntityDao entityDao;
|
||||
|
||||
private final long accountId;
|
||||
private final Roster roster;
|
||||
private final MercuryConnection connection;
|
||||
|
||||
public RoomRosterHandler(MercuryConnection connection) {
|
||||
this.connection = connection;
|
||||
this.accountId = connection.getAccountId();
|
||||
this.roster = connection.getRoster();
|
||||
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void entriesAdded(Collection<Jid> addresses) {
|
||||
for (Jid jid : addresses) {
|
||||
RoomContactAttributes contactModel = getContactModel(jid);
|
||||
contactModel.setNickname(roster.getEntry(jid.asBareJid()).getName());
|
||||
contactAttributesDao.insert(contactModel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entriesUpdated(Collection<Jid> addresses) {
|
||||
for (Jid jid : addresses) {
|
||||
RoomContactAttributes contactModel = getContactModel(jid);
|
||||
contactModel.setNickname(roster.getEntry(jid.asBareJid()).getName());
|
||||
contactAttributesDao.update(contactModel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void entriesDeleted(Collection<Jid> addresses) {
|
||||
for (Jid jid : addresses) {
|
||||
RoomContactAttributes contactModel = getContactModel(jid);
|
||||
contactAttributesDao.delete(contactModel);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceChanged(Presence presence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRosterLoaded(Roster roster) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRosterLoadingFailed(Exception exception) {
|
||||
|
||||
}
|
||||
|
||||
@Transaction
|
||||
private RoomContactAttributes getContactModel(Jid jid) {
|
||||
Log.d("Mercury", "Build entity " + jid + " " + accountId);
|
||||
EntityBareJid entityBareJid = jid.asEntityBareJidOrThrow();
|
||||
RoomEntityModel entityModel = entityDao.getEntityForSync(accountId, entityBareJid);
|
||||
if (entityModel == null) {
|
||||
entityModel = new RoomEntityModel();
|
||||
entityModel.setAccountId(accountId);
|
||||
entityModel.setJid(entityBareJid);
|
||||
entityModel.setId(entityDao.insert(entityModel));
|
||||
}
|
||||
|
||||
RoomContactAttributes contactModel = contactAttributesDao.syncGetContactForEntityId(entityModel.getId());
|
||||
if (contactModel == null) {
|
||||
contactModel = new RoomContactAttributes();
|
||||
contactModel.setAccountId(accountId);
|
||||
contactModel.setEntityId(entityModel.getId());
|
||||
}
|
||||
return contactModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceAvailable(FullJid address, Presence availablePresence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceUnavailable(FullJid address, Presence presence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceError(Jid address, Presence errorPresence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void presenceUnsubscribed(BareJid address, Presence unsubscribedPresence) {
|
||||
|
||||
}
|
||||
}
|
|
@ -5,26 +5,17 @@ import android.os.Bundle;
|
|||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.lifecycle.Observer;
|
||||
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.R;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.ContactAttributesRepository;
|
||||
import org.mercury_im.messenger.ui.login.AccountsActivity;
|
||||
import org.mercury_im.messenger.ui.login.LoginActivity;
|
||||
import org.mercury_im.messenger.ui.settings.SettingsActivity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.os.Bundle;
|
|||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
|
@ -26,8 +27,9 @@ import javax.inject.Inject;
|
|||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import io.reactivex.Maybe;
|
||||
|
||||
public class ChatActivity extends BindingActivity implements ChatInputFragment.OnChatInputActionListener {
|
||||
public class ChatActivity extends AppCompatActivity implements ChatInputFragment.OnChatInputActionListener {
|
||||
|
||||
public static final String EXTRA_JID = "JID";
|
||||
public static final String EXTRA_ACCOUNT = "ACCOUNT";
|
||||
|
@ -75,9 +77,10 @@ public class ChatActivity extends BindingActivity implements ChatInputFragment.O
|
|||
jid = JidCreate.entityBareFromOrThrowUnchecked(jidString);
|
||||
accountId = savedInstanceState.getLong(EXTRA_ACCOUNT);
|
||||
|
||||
LiveData<AccountModel> accountModel = accountRepository.getAccount(accountId);
|
||||
Maybe<AccountModel> accountModel = accountRepository.getAccount(accountId);
|
||||
chatViewModel = ViewModelProviders.of(this).get(ChatViewModel.class);
|
||||
|
||||
/*
|
||||
accountModel.observe(this, new Observer<AccountModel>() {
|
||||
@Override
|
||||
public void onChanged(AccountModel accountModel) {
|
||||
|
@ -97,6 +100,7 @@ public class ChatActivity extends BindingActivity implements ChatInputFragment.O
|
|||
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.stub_compose, ChatInputFragment.newInstance())
|
||||
.commitAllowingStateLoss();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ import android.widget.TextView;
|
|||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.R;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
||||
import org.mercury_im.messenger.ui.login.AccountsFragment.OnListFragmentInteractionListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -22,10 +21,10 @@ import de.hdodenhof.circleimageview.CircleImageView;
|
|||
|
||||
public class AccountsRecyclerViewAdapter extends RecyclerView.Adapter<AccountsRecyclerViewAdapter.ViewHolder> {
|
||||
|
||||
private final List<RoomAccountModel> mValues;
|
||||
private final List<AccountModel> mValues;
|
||||
private final OnListFragmentInteractionListener mListener;
|
||||
|
||||
public AccountsRecyclerViewAdapter(List<RoomAccountModel> items, OnListFragmentInteractionListener listener) {
|
||||
public AccountsRecyclerViewAdapter(List<AccountModel> items, OnListFragmentInteractionListener listener) {
|
||||
mValues = items != null ? items : new ArrayList<>();
|
||||
mListener = listener;
|
||||
}
|
||||
|
@ -56,7 +55,7 @@ public class AccountsRecyclerViewAdapter extends RecyclerView.Adapter<AccountsRe
|
|||
});
|
||||
}
|
||||
|
||||
public void setValues(List<RoomAccountModel> values) {
|
||||
public void setValues(List<AccountModel> values) {
|
||||
this.mValues.clear();
|
||||
if (values != null) {
|
||||
mValues.addAll(values);
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package org.mercury_im.messenger.ui.login;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.core.ConnectionCenter;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
||||
|
||||
|
@ -11,21 +14,39 @@ import java.util.List;
|
|||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class AccountsViewModel extends ViewModel {
|
||||
|
||||
@Inject
|
||||
AccountRepository repository;
|
||||
|
||||
private LiveData<List<RoomAccountModel>> accounts;
|
||||
@Inject
|
||||
ConnectionCenter connectionCenter;
|
||||
|
||||
private final MutableLiveData<List<AccountModel>> accounts = new MutableLiveData<>();
|
||||
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||
|
||||
@Inject
|
||||
public AccountsViewModel() {
|
||||
super();
|
||||
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
||||
accounts = repository.getAllAccounts();
|
||||
compositeDisposable.add(repository.getAllAccounts()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe((Consumer<List<AccountModel>>) accounts::setValue));
|
||||
}
|
||||
|
||||
public LiveData<List<RoomAccountModel>> getAccounts() {
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
compositeDisposable.clear();
|
||||
}
|
||||
|
||||
public LiveData<List<AccountModel>> getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,39 +1,38 @@
|
|||
package org.mercury_im.messenger.ui.login;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.core.MercuryConnection;
|
||||
import org.mercury_im.messenger.handler.RoomPlainMessageHandler;
|
||||
import org.mercury_im.messenger.handler.RoomRosterHandler;
|
||||
import org.mercury_im.messenger.core.ConnectionCenter;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
||||
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
||||
import org.mercury_im.messenger.service.XmppConnectionService;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.observers.DisposableSingleObserver;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
import static org.mercury_im.messenger.core.MercuryConnection.TAG;
|
||||
|
||||
|
||||
public class LoginViewModel extends ViewModel {
|
||||
|
||||
@Inject
|
||||
AccountRepository accountRepository;
|
||||
|
||||
private XmppConnectionService connectionService;
|
||||
@Inject
|
||||
ConnectionCenter connectionCenter;
|
||||
|
||||
private String jid;
|
||||
private String password;
|
||||
|
@ -41,12 +40,12 @@ public class LoginViewModel extends ViewModel {
|
|||
private MutableLiveData<JidError> jidError = new MutableLiveData<>();
|
||||
private MutableLiveData<PasswordError> passwordError = new MutableLiveData<>();
|
||||
|
||||
private MutableLiveData<RoomAccountModel> account = new MutableLiveData<>();
|
||||
private MutableLiveData<AccountModel> account = new MutableLiveData<>();
|
||||
|
||||
public LoginViewModel() {
|
||||
super();
|
||||
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
||||
init(new RoomAccountModel());
|
||||
init(accountRepository.newAccountModel());
|
||||
}
|
||||
|
||||
public enum JidError {
|
||||
|
@ -94,16 +93,16 @@ public class LoginViewModel extends ViewModel {
|
|||
return !password.isEmpty();
|
||||
}
|
||||
|
||||
public void init(@NonNull RoomAccountModel account) {
|
||||
public void init(@NonNull AccountModel account) {
|
||||
this.account.setValue(account);
|
||||
}
|
||||
|
||||
public MutableLiveData<RoomAccountModel> getAccount() {
|
||||
public MutableLiveData<AccountModel> getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void login() {
|
||||
RoomAccountModel account = getAccount().getValue();
|
||||
AccountModel account = getAccount().getValue();
|
||||
if (account != null && account.getJid() != null && !TextUtils.isEmpty(account.getPassword())) {
|
||||
accountRepository.insertAccount(account);
|
||||
}
|
||||
|
@ -132,49 +131,23 @@ public class LoginViewModel extends ViewModel {
|
|||
accountModel.setEnabled(true);
|
||||
accountModel.setJid(bareJid);
|
||||
accountModel.setPassword(password);
|
||||
long id = accountRepository.insertAccount(accountModel);
|
||||
accountModel.setId(id);
|
||||
Log.d(MercuryImApplication.TAG, "LoginActivity.loginDetailsEntered: Account " + id + " inserted.");
|
||||
attemptLogin(accountModel);
|
||||
Single<Long> id = accountRepository.insertAccount(accountModel);
|
||||
id.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new DisposableSingleObserver<Long>() {
|
||||
@Override
|
||||
public void onSuccess(Long aLong) {
|
||||
accountModel.setId(aLong);
|
||||
Log.d(MercuryImApplication.TAG, "LoginActivity.loginDetailsEntered: Account " + aLong + " inserted.");
|
||||
connectionCenter.createConnection(accountModel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e(TAG, "Could not insert new Account data.", e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void attemptLogin(AccountModel accountModel) {
|
||||
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
|
||||
.setHost(accountModel.getJid().getDomain().toString())
|
||||
.setXmppAddressAndPassword(accountModel.getJid(), accountModel.getPassword())
|
||||
.setConnectTimeout(2 * 60 * 1000)
|
||||
.build();
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
XMPPTCPConnection connection = new XMPPTCPConnection(configuration);
|
||||
connection.setUseStreamManagementResumption(true);
|
||||
connection.setUseStreamManagement(true);
|
||||
MercuryConnection mercuryConnection = new MercuryConnection(connection, accountModel.getId());
|
||||
connectionService.putConnection(accountModel.getId(), mercuryConnection);
|
||||
mercuryConnection.setRosterHandler(new RoomRosterHandler(mercuryConnection));
|
||||
mercuryConnection.setPlainMessageHandler(new RoomPlainMessageHandler(accountModel.getId()));
|
||||
|
||||
connection.connect().login();
|
||||
Log.d(MercuryImApplication.TAG, "Logged in for " + accountModel.getJid().toString());
|
||||
} catch (XmppStringprepException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SmackException e) {
|
||||
e.printStackTrace();
|
||||
} catch (XMPPException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public void setConnectionService(XmppConnectionService connectionService) {
|
||||
this.connectionService = connectionService;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel;
|
|||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.persistence.repository.ContactRepository;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomContactModel;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IContactRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -14,13 +15,12 @@ import javax.inject.Inject;
|
|||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class RosterViewModel extends ViewModel {
|
||||
|
||||
@Inject
|
||||
ContactRepository<RoomContactModel> contactRepository;
|
||||
ContactRepository contactRepository;
|
||||
|
||||
private final MutableLiveData<List<RoomContactModel>> rosterEntryList = new MutableLiveData<>();
|
||||
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||
|
@ -33,7 +33,7 @@ public class RosterViewModel extends ViewModel {
|
|||
compositeDisposable.add(contactRepository.getAllContacts()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(rosterEntryList::setValue));
|
||||
.subscribe(o -> rosterEntryList.setValue((List<RoomContactModel>) o)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
package org.mercury_im.messenger.core;
|
||||
|
||||
import org.jivesoftware.smack.AbstractXMPPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -23,8 +28,6 @@ public class ConnectionCenter {
|
|||
return INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public MercuryConnection getConnection(long accountId) {
|
||||
return connectionMap.get(accountId);
|
||||
}
|
||||
|
@ -32,4 +35,15 @@ public class ConnectionCenter {
|
|||
public void putConnection(MercuryConnection connection) {
|
||||
connectionMap.put(connection.getAccountId(), connection);
|
||||
}
|
||||
|
||||
public MercuryConnection createConnection(AccountModel accountModel) {
|
||||
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
|
||||
.setHost(accountModel.getJid().getDomain().toString())
|
||||
.setXmppAddressAndPassword(accountModel.getJid(), accountModel.getPassword())
|
||||
.setConnectTimeout(2 * 60 * 1000)
|
||||
.build();
|
||||
|
||||
AbstractXMPPConnection tcpConnection = new XMPPTCPConnection(configuration);
|
||||
return new MercuryConnection(tcpConnection, accountModel.getId());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ public class MercuryConnection implements ConnectionListener {
|
|||
|
||||
private static final Logger LOGGER = Logger.getLogger(MercuryConnection.class.getName());
|
||||
|
||||
protected final XMPPConnection connection;
|
||||
protected final AbstractXMPPConnection connection;
|
||||
protected final long accountId;
|
||||
|
||||
protected final ReconnectionManager reconnectionManager;
|
||||
|
@ -33,12 +33,12 @@ public class MercuryConnection implements ConnectionListener {
|
|||
protected final StableUniqueStanzaIdManager stanzaIdManager;
|
||||
protected final ServiceDiscoveryManager serviceDiscoveryManager;
|
||||
|
||||
public MercuryConnection(XMPPConnection connection, long accountId) {
|
||||
public MercuryConnection(AbstractXMPPConnection connection, long accountId) {
|
||||
this.connection = connection;
|
||||
connection.addConnectionListener(this);
|
||||
this.accountId = accountId;
|
||||
|
||||
reconnectionManager = ReconnectionManager.getInstanceFor((AbstractXMPPConnection) connection);
|
||||
reconnectionManager = ReconnectionManager.getInstanceFor(connection);
|
||||
reconnectionManager.enableAutomaticReconnection();
|
||||
|
||||
this.roster = Roster.getInstanceFor(connection);
|
||||
|
|
|
@ -68,7 +68,7 @@ public class RoomModule {
|
|||
@Singleton
|
||||
@Provides
|
||||
ContactDao provideContactAndEntityDao() {
|
||||
return mAppDatabase.contactAndEntityDao();
|
||||
return mAppDatabase.contactDao();
|
||||
}
|
||||
|
||||
@Singleton
|
||||
|
|
|
@ -1,24 +1,25 @@
|
|||
package org.mercury_im.messenger.persistence.room;
|
||||
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.AvatarRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.ChatRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.ContactRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.ContactAttributesRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.ContactRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.EntityRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.MessageRepository;
|
||||
import org.mercury_im.messenger.persistence.room.dao.AccountDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.AvatarDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.ChatDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.ContactDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.ContactAttributesDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.ContactDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.EntityDao;
|
||||
import org.mercury_im.messenger.persistence.room.dao.MessageDao;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IAccountRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IAvatarRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IChatRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IContactRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IContactAttributesRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IContactRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IEntityRepository;
|
||||
import org.mercury_im.messenger.persistence.room.repository.IMessageRepository;
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.mercury_im.messenger.persistence.room.dao;
|
||||
|
||||
import androidx.annotation.WorkerThread;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.TypeConverters;
|
||||
|
@ -10,19 +9,21 @@ import org.jxmpp.jid.EntityBareJid;
|
|||
import org.mercury_im.messenger.persistence.room.model.RoomAvatarModel;
|
||||
import org.mercury_im.messenger.persistence.room.type_converter.EntityBareJidConverter;
|
||||
|
||||
import io.reactivex.Maybe;
|
||||
|
||||
@Dao
|
||||
@TypeConverters(EntityBareJidConverter.class)
|
||||
@WorkerThread
|
||||
public interface AvatarDao extends BaseDao<RoomAvatarModel> {
|
||||
|
||||
@Query("SELECT * FROM avatars WHERE pk_avatar_id = :avatarId")
|
||||
LiveData<RoomAvatarModel> getAvatarById(long avatarId);
|
||||
Maybe<RoomAvatarModel> getAvatarById(long avatarId);
|
||||
|
||||
@Query("SELECT * FROM avatars WHERE fk_entity_id = :entityId")
|
||||
LiveData<RoomAvatarModel> getAvatarByEntityId(long entityId);
|
||||
Maybe<RoomAvatarModel> getAvatarByEntityId(long entityId);
|
||||
|
||||
@Query("SELECT avatars.* " +
|
||||
"FROM avatars INNER JOIN entities ON avatars.fk_entity_id = entities.pk_entity_id " +
|
||||
"WHERE entities.jid = :jid")
|
||||
LiveData<RoomAvatarModel> getAvatarByJid(EntityBareJid jid);
|
||||
Maybe<RoomAvatarModel> getAvatarByJid(EntityBareJid jid);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.mercury_im.messenger.persistence.room.repository;
|
||||
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
||||
import org.mercury_im.messenger.persistence.room.dao.AccountDao;
|
||||
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
||||
|
@ -22,6 +23,11 @@ public class IAccountRepository implements AccountRepository<RoomAccountModel> {
|
|||
this.accountDao = accountDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoomAccountModel newAccountModel() {
|
||||
return new RoomAccountModel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Maybe<RoomAccountModel> getAccount(long accountId) {
|
||||
return accountDao.getAccountById(accountId);
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package org.mercury_im.messenger.persistence.room.repository;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.mercury_im.messenger.persistence.repository.AvatarRepository;
|
||||
import org.mercury_im.messenger.persistence.room.dao.AvatarDao;
|
||||
|
@ -9,6 +7,9 @@ import org.mercury_im.messenger.persistence.room.model.RoomAvatarModel;
|
|||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.Single;
|
||||
|
||||
public class IAvatarRepository implements AvatarRepository<RoomAvatarModel> {
|
||||
|
||||
private final AvatarDao dao;
|
||||
|
@ -19,22 +20,27 @@ public class IAvatarRepository implements AvatarRepository<RoomAvatarModel> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public LiveData<RoomAvatarModel> getAvatarById(long avatarId) {
|
||||
public RoomAvatarModel newAvatarModel() {
|
||||
return new RoomAvatarModel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Maybe<RoomAvatarModel> getAvatarById(long avatarId) {
|
||||
return dao.getAvatarById(avatarId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<RoomAvatarModel> getAvatarByEntityId(long entityId) {
|
||||
public Maybe<RoomAvatarModel> getAvatarByEntityId(long entityId) {
|
||||
return dao.getAvatarByEntityId(entityId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LiveData<RoomAvatarModel> getAvatarByJid(EntityBareJid jid) {
|
||||
public Maybe<RoomAvatarModel> getAvatarByJid(EntityBareJid jid) {
|
||||
return dao.getAvatarByJid(jid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long updateOrInsertAvatar(RoomAvatarModel avatarModel) {
|
||||
public Single<Long> updateOrInsertAvatar(RoomAvatarModel avatarModel) {
|
||||
return dao.insert(avatarModel);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,11 @@ public class IChatRepository implements ChatRepository<RoomChatModel> {
|
|||
this.chatDao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoomChatModel newChatModel() {
|
||||
return new RoomChatModel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<RoomChatModel>> getAllChats() {
|
||||
return chatDao.getAllChats();
|
||||
|
|
|
@ -18,6 +18,11 @@ public class IContactAttributesRepository implements ContactAttributesRepository
|
|||
this.contactAttributesDao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoomContactAttributes newContactAttributesModel() {
|
||||
return new RoomContactAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<List<RoomContactAttributes>> getAllContactAttributes() {
|
||||
return contactAttributesDao.getAllContacts();
|
||||
|
|
|
@ -22,6 +22,11 @@ public class IContactRepository implements ContactRepository<RoomContactModel> {
|
|||
this.dao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoomContactModel newContactModel() {
|
||||
return new RoomContactModel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Maybe<RoomContactModel> getContact(long accountId, EntityBareJid jid) {
|
||||
return dao.getContactAndEntity(accountId, jid);
|
||||
|
|
|
@ -19,6 +19,11 @@ public class IEntityRepository implements EntityRepository<RoomEntityModel> {
|
|||
this.dao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoomEntityModel newEntityModel() {
|
||||
return new RoomEntityModel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <C extends ContactAttributes> Maybe<RoomEntityModel> getEntity(C contact) {
|
||||
return dao.getEntity(contact.getId());
|
||||
|
|
|
@ -19,6 +19,11 @@ public class IMessageRepository implements MessageRepository<RoomMessageModel> {
|
|||
this.messageDao = messageDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoomMessageModel newMessageModel() {
|
||||
return new RoomMessageModel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Maybe<RoomMessageModel> getMessage(long accountId, long chatId, long messageId) {
|
||||
return null;
|
||||
|
|
|
@ -11,5 +11,9 @@ dependencies {
|
|||
// RxJava2
|
||||
implementation "io.reactivex.rxjava2:rxjava:$rxJava2Version"
|
||||
|
||||
// Dagger 2 for dependency injection
|
||||
implementation "com.google.dagger:dagger:$daggerVersion"
|
||||
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
|
||||
|
||||
testImplementation "junit:junit:$junitVersion"
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ import io.reactivex.Single;
|
|||
|
||||
public interface AccountRepository<E extends AccountModel> {
|
||||
|
||||
E newAccountModel();
|
||||
|
||||
Maybe<E> getAccount(long accountId);
|
||||
|
||||
Observable<List<E>> getAllAccounts();
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
package org.mercury_im.messenger.persistence.repository;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.mercury_im.messenger.persistence.model.AvatarModel;
|
||||
|
||||
import io.reactivex.Maybe;
|
||||
import io.reactivex.Single;
|
||||
|
||||
public interface AvatarRepository<E extends AvatarModel> {
|
||||
|
||||
LiveData<E> getAvatarById(long avatarId);
|
||||
E newAvatarModel();
|
||||
|
||||
LiveData<E> getAvatarByEntityId(long entityId);
|
||||
Maybe<E> getAvatarById(long avatarId);
|
||||
|
||||
LiveData<E> getAvatarByJid(EntityBareJid jid);s
|
||||
Maybe<E> getAvatarByEntityId(long entityId);
|
||||
|
||||
long updateOrInsertAvatar(E avatarModel);
|
||||
Maybe<E> getAvatarByJid(EntityBareJid jid);
|
||||
|
||||
Single<Long> updateOrInsertAvatar(E avatarModel);
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ import io.reactivex.Observable;
|
|||
|
||||
public interface ChatRepository<E extends ChatModel> {
|
||||
|
||||
E newChatModel();
|
||||
|
||||
Observable<List<E>> getAllChats();
|
||||
|
||||
Observable<List<E>> getAllChatsOf(AccountModel account);
|
||||
|
|
|
@ -10,6 +10,8 @@ import io.reactivex.Single;
|
|||
|
||||
public interface ContactAttributesRepository<E extends ContactAttributes> {
|
||||
|
||||
E newContactAttributesModel();
|
||||
|
||||
Observable<List<E>> getAllContactAttributes();
|
||||
|
||||
Single<Long> updateOrInsertContactAttributes(E attributes);
|
||||
|
|
|
@ -12,6 +12,8 @@ import io.reactivex.Observable;
|
|||
|
||||
public interface ContactRepository<CE extends ContactModel<? extends ContactAttributes, ? extends EntityModel>> {
|
||||
|
||||
CE newContactModel();
|
||||
|
||||
Maybe<CE> getContact(long accountId, EntityBareJid jid);
|
||||
|
||||
Maybe<CE> getContactForEntity(EntityModel entityModel);
|
||||
|
|
|
@ -8,6 +8,8 @@ import io.reactivex.Maybe;
|
|||
|
||||
public interface EntityRepository<E extends EntityModel> {
|
||||
|
||||
E newEntityModel();
|
||||
|
||||
<C extends ContactAttributes> Maybe<E> getEntity(C contact);
|
||||
|
||||
Maybe<E> getEntity(long accountId, EntityBareJid jid);
|
||||
|
|
|
@ -11,6 +11,8 @@ import io.reactivex.Single;
|
|||
|
||||
public interface MessageRepository<E extends MessageModel> {
|
||||
|
||||
E newMessageModel();
|
||||
|
||||
Maybe<E> getMessage(long accountId, long chatId, long messageId);
|
||||
|
||||
Single<Long> insertMessage(E message);
|
||||
|
|
Loading…
Reference in a new issue