aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/EnterNameActivity.java
blob: 1b4a73e823aca913249a8ca27701c67418955e6f (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
package de.pixart.messenger.ui;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.view.View;

import java.util.concurrent.atomic.AtomicBoolean;

import de.pixart.messenger.R;
import de.pixart.messenger.databinding.ActivityEnterNameBinding;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.utils.FirstStartManager;

public class EnterNameActivity extends XmppActivity implements XmppConnectionService.OnAccountUpdate {

    private ActivityEnterNameBinding binding;
    private Account account;
    private AtomicBoolean setNick = new AtomicBoolean(false);

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_enter_name);
        setSupportActionBar((Toolbar) this.binding.toolbar);
        this.binding.next.setOnClickListener(this::next);
        updateNextButton();
        this.setNick.set(savedInstanceState != null && savedInstanceState.getBoolean("set_nick", false));
    }

    private void updateNextButton() {
        if (account != null && (account.getStatus() == Account.State.CONNECTING || account.getStatus() == Account.State.REGISTRATION_SUCCESSFUL)) {
            this.binding.next.setEnabled(false);
            this.binding.next.setText(R.string.account_status_connecting);
        } else if (account != null && (account.getStatus() == Account.State.ONLINE)) {
            this.binding.next.setEnabled(true);
            this.binding.next.setText(R.string.next);
        }
    }

    private void next(View view) {
        FirstStartManager firstStartManager = new FirstStartManager(this);
        if (account != null) {
            String name = this.binding.name.getText().toString().trim();
            account.setDisplayName(name);
            xmppConnectionService.publishDisplayName(account);
            if (firstStartManager.isFirstTimeLaunch()) {
                Intent intent = new Intent(this, SetSettingsActivity.class);
                intent.putExtra("setup", true);
                startActivity(intent);
                overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
            } else {
                Intent intent = new Intent(this, PublishProfilePictureActivity.class);
                intent.putExtra(PublishProfilePictureActivity.EXTRA_ACCOUNT, account.getJid().asBareJid().toEscapedString());
                intent.putExtra("setup", true);
                startActivity(intent);
                overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
            }
        }
        finish();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean("set_nick", this.setNick.get());
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    protected void refreshUiReal() {
        checkSuggestPreviousNick();
        updateNextButton();
    }

    @Override
    void onBackendConnected() {
        this.account = extractAccount(getIntent());
        if (this.account != null) {
            checkSuggestPreviousNick();
        }
        updateNextButton();
    }

    private void checkSuggestPreviousNick() {
        String displayName = this.account == null ? null : this.account.getDisplayName();
        if (displayName != null) {
            if (setNick.compareAndSet(false, true) && this.binding.name.getText().length() == 0) {
                this.binding.name.getText().append(displayName);
            }
        }
    }

    @Override
    public void onAccountUpdate() {
        refreshUi();
    }
}