kimi
2021-02-18 749a5510a9f014446a3cd6ba57b3cb0cc8148dc1
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */
 
package cn.wildfire.chat.kit.channel;
 
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
 
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
 
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wildfire.chat.kit.common.OperateResult;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfirechat.model.ChannelInfo;
 
public class ChannelInfoActivity extends AppCompatActivity {
    @BindView(R2.id.portraitImageView)
    ImageView portraitImageView;
    @BindView(R2.id.channelNameTextView)
    TextView channelTextView;
    @BindView(R2.id.channelDescTextView)
    TextView channelDescTextView;
    @BindView(R2.id.followChannelButton)
    Button followChannelButton;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
 
    private boolean isFollowed = false;
    private ChannelViewModel channelViewModel;
    private ChannelInfo channelInfo;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.channel_info_activity);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        init();
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    private void init() {
        Intent intent = getIntent();
        channelInfo = intent.getParcelableExtra("channelInfo");
        channelViewModel = ViewModelProviders.of(this).get(ChannelViewModel.class);
 
        if (channelInfo == null) {
            String channelId = intent.getStringExtra("channelId");
            if (!TextUtils.isEmpty(channelId)) {
                channelInfo = channelViewModel.getChannelInfo(channelId, true);
            }
        }
        if (channelInfo == null) {
            finish();
            return;
        }
 
        // FIXME: 2018/12/24 只有应用launcher icon应当反倒mipmap下面,其他还是应当放到drawable下面
        Glide.with(this).load(channelInfo.portrait).apply(new RequestOptions().placeholder(R.mipmap.ic_group_cheat)).into(portraitImageView);
        channelTextView.setText(channelInfo.name);
        channelDescTextView.setText(TextUtils.isEmpty(channelInfo.desc) ? "频道主什么也没写" : channelInfo.desc);
 
 
        UserViewModel userViewModel =ViewModelProviders.of(this).get(UserViewModel.class);
        if (channelInfo.owner.equals(userViewModel.getUserId())) {
            followChannelButton.setVisibility(View.GONE);
            return;
        }
 
        isFollowed = channelViewModel.isListenedChannel(channelInfo.channelId);
        if (isFollowed) {
            followChannelButton.setText("取消收听");
        } else {
            followChannelButton.setText("收听频道");
        }
    }
 
    @OnClick(R2.id.followChannelButton)
    void followChannelButtonClick() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content(isFollowed ? "正在取消收听" : "正在收听")
                .progress(true, 100)
                .cancelable(false)
                .build();
        dialog.show();
        channelViewModel.listenChannel(channelInfo.channelId, !isFollowed).observe(this, new Observer<OperateResult<Boolean>>() {
            @Override
            public void onChanged(@Nullable OperateResult<Boolean> booleanOperateResult) {
                dialog.dismiss();
                if (booleanOperateResult.isSuccess()) {
                    Toast.makeText(ChannelInfoActivity.this, isFollowed ? "取消收听成功" : "收听成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(ChannelInfoActivity.this, isFollowed ? "取消收听失败" : "收听失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}