aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/BlocklistActivity.java
blob: 2927fe3e317edae9cca93118c24c50f6b92d41d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package de.pixart.messenger.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.widget.Toast;

import java.util.Collections;

import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.entities.Blockable;
import de.pixart.messenger.entities.Contact;
import de.pixart.messenger.entities.ListItem;
import de.pixart.messenger.entities.RawBlockable;
import de.pixart.messenger.ui.interfaces.OnBackendConnected;
import de.pixart.messenger.xmpp.OnUpdateBlocklist;
import rocks.xmpp.addr.Jid;

public class BlocklistActivity extends AbstractSearchableListItemActivity implements OnUpdateBlocklist {
    private Account account = null;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getListView().setOnItemLongClickListener((parent, view, position, id) -> {
            BlockContactDialog.show(BlocklistActivity.this, (Blockable) getListItems().get(position));
            return true;
        });
        this.binding.fab.show();
        this.binding.fab.setOnClickListener((v) -> showEnterJidDialog());
    }

    @Override
    public void onBackendConnected() {
        for (final Account account : xmppConnectionService.getAccounts()) {
            if (account.getJid().toString().equals(getIntent().getStringExtra(EXTRA_ACCOUNT))) {
                this.account = account;
                break;
            }
        }
        filterContacts();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_DIALOG);
        if (fragment instanceof OnBackendConnected) {
            ((OnBackendConnected) fragment).onBackendConnected();
        }
    }

    @Override
    protected void filterContacts(final String needle) {
        getListItems().clear();
        if (account != null) {
            for (final Jid jid : account.getBlocklist()) {
                ListItem item;
                if (jid.isFullJid()) {
                    item = new RawBlockable(account, jid);
                } else {
                    item = account.getRoster().getContact(jid);
                }
                if (item.match(this, needle)) {
                    getListItems().add(item);
                }
            }
            Collections.sort(getListItems());
        }
        getListItemAdapter().notifyDataSetChanged();
    }

    protected void showEnterJidDialog() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        EnterJidDialog dialog = EnterJidDialog.newInstance(
                null,
                getString(R.string.block_jabber_id),
                getString(R.string.block),
                null,
                account.getJid().asBareJid().toString(),
                true,
                xmppConnectionService.multipleAccounts(),
                false
        );

        dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> {
            Blockable blockable = new RawBlockable(account, contactJid);
            if (xmppConnectionService.sendBlockRequest(blockable, false)) {
                Toast.makeText(BlocklistActivity.this, R.string.corresponding_conversations_closed, Toast.LENGTH_SHORT).show();
            }
            return true;
        });
        dialog.show(ft, "dialog");
    }

    protected void refreshUiReal() {
        final Editable editable = getSearchEditText().getText();
        if (editable != null) {
            filterContacts(editable.toString());
        } else {
            filterContacts();
        }
    }

    @Override
    public void OnUpdateBlocklist(final OnUpdateBlocklist.Status status) {
        refreshUi();
    }
}