admin
2021-05-15 2aead6275fdd1bbbd778abc0e85663a2578fab06
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.tejia.lijin.app.ui.recommend;
 
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
import com.tejia.lijin.app.ShoppingApplication;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.util.common.StringUtils;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.ui.BaseActivity;
import com.tejia.lijin.app.ui.dialog.EditTextPreviewDialog;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
/**
 * 文字编辑模板
 */
public class ActivityContentCompile extends BaseActivity implements View.OnClickListener {
    private TextView tv_top_bar_left;
    private TextView tv_top_bar_middle;
 
    private TextView content_compile_preview;//预览文字模板
    private TextView content_compile_save;//保存文字模板
 
    private TextView tv_special_hint;
 
    private EditText et_content_compile;
    private String content;
    boolean hasCoupon = false;
    String goodsId, tljId;
    int goodsType;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_compile);
        init();
        setListeners();
        tv_top_bar_middle.setText("编辑文字模版");
 
        goodsId = getIntent().getStringExtra("goodsId");
        tljId = getIntent().getStringExtra("tljId");
        goodsType = getIntent().getIntExtra("goodsType", 1);
        hasCoupon = getIntent().getBooleanExtra("hasCoupon", false);
        getShareTextTemplate();
        getShareTextTemplateRules();
    }
 
    private void init() {
        tv_top_bar_left = findViewById(R.id.tv_top_bar_left);
        tv_top_bar_middle = findViewById(R.id.tv_top_bar_middle);
        content_compile_preview = findViewById(R.id.content_compile_preview);
        content_compile_save = findViewById(R.id.content_compile_save);
        et_content_compile = findViewById(R.id.et_content_compile);
        tv_special_hint = findViewById(R.id.tv_special_hint);
    }
 
    private void setListeners() {
        //设置状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //设置状态栏颜色为白色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(getResources().getColor(R.color.white));
            }
            //设置状态栏文字颜色及图标为深色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        tv_top_bar_left.setOnClickListener(this);
        content_compile_preview.setOnClickListener(this);
        content_compile_save.setOnClickListener(this);
    }
 
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.content_compile_preview://预览模板
                if (StringUtils.isEmpty(et_content_compile.getText().toString())) {
                    Toast.makeText(ActivityContentCompile.this, "内容不能为空",
                            Toast.LENGTH_LONG).show();
                    break;
                }
                content = et_content_compile.getText().toString();
                viewShareTextTemplate();
                break;
            case R.id.content_compile_save://保存模板
                saveShareTextTemplate();
                break;
            default:
 
                break;
        }
    }
 
    /*
     * 获取规则
     */
    private void getShareTextTemplateRules() {
        ShoppingApi.getShareTextTemplateRules(ActivityContentCompile.this, tljId, goodsType,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                        if (jsonObject.optInt("code") == 0) {
                            tv_special_hint.setText(jsonObject.optString("data"));
                        }
                    }
                });
    }
 
    /*
     * 获取模板
     */
    private void getShareTextTemplate() {
        ShoppingApi.getShareTextTemplate(ActivityContentCompile.this, UserUtil.getUid(ShoppingApplication.application), hasCoupon + "", tljId, goodsType,
                new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                        if (jsonObject.optInt("code") == 0) {
                            content = jsonObject.optJSONObject("data").optString("template");
                            et_content_compile.setText(content);
                            if (!StringUtils.isEmpty(content))
                                et_content_compile.setSelection(content.length());
                        }
                    }
                });
    }
 
    /*
     * 预览模板
     */
    private void viewShareTextTemplate() {
        ShoppingApi.viewShareTextTemplate(ActivityContentCompile.this, UserUtil.getUid(ShoppingApplication.application),
                goodsId, hasCoupon + "", content, tljId, goodsType, new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                        if (jsonObject.optInt("code") == 0) {
                            String content = jsonObject.optJSONObject("data").optString("content");
                            EditTextPreviewDialog.Builder builder = new EditTextPreviewDialog.Builder(ActivityContentCompile.this);
                            builder.setMessage(content)
                                    .setPositiveButton("继续编辑", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    }).create().show();
                        }
                    }
                });
    }
 
    /*
     * 保存模板
     */
    private void saveShareTextTemplate() {
        SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        Long uid = UserUtil.getUid(ShoppingApplication.application);
        ShoppingApi.saveShareTextTemplate(ActivityContentCompile.this, uid,
                hasCoupon + "", et_content_compile.getText().toString(), tljId, goodsType, new BasicTextHttpResponseHandler() {
                    @Override
                    public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                        if (jsonObject.optInt("code") == 0) {
                            finish();
                            Toast.makeText(ActivityContentCompile.this, "文字模板保存成功", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(ActivityContentCompile.this, jsonObject.optString("msg"), Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }
}