riku
2020-10-10 5771916ffb24807dd57c1969baa6371611c334da
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package cn.flightfeather.thirdapp.module;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.List;
 
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.bean.Subtask;
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo;
import cn.flightfeather.thirdapp.business.widgettype2.HomeFragment_comptent;
import cn.flightfeather.thirdapp.business.widgettype3.HomeFragment_scene;
import cn.flightfeather.thirdapp.dataanalysis.AnalysisOverViewFragment;
import cn.flightfeather.thirdapp.fragment.HomeFragment;
import cn.flightfeather.thirdapp.fragment.SettingFragment;
import cn.flightfeather.thirdapp.fragment.TaskFragment;
import cn.flightfeather.thirdapp.module.inspection.InspectionFragment;
import cn.flightfeather.thirdapp.module.inspectioninfo.InspectionInfoFragment;
import cn.flightfeather.thirdapp.module.inspectioninfo.ProblemChangeDetailActivity;
import cn.flightfeather.thirdapp.util.Constant;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout ll_home;
    private LinearLayout ll_inspection_infos;
    private LinearLayout ll_task;
    private LinearLayout ll_inspection;
    private LinearLayout ll_anysis;
    private LinearLayout ll_setting;
    private List<LinearLayout> tabList;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FrameLayout fl_fragmentContainer;
    private FragmentTransaction fragmentTransaction;
    private Fragment fragmentCurrent;
    private InspectionFragment mInspectionFragment;
    private boolean exist = false;
    private CommonApplication app;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what ==0){
                    exist = false;
            }
        }
    };
//    //定义菜单控制器对象数组
//    private OnMenuController[] onMenuControllers;
    //广播
    private MainReceiver mainReceiver;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        app = (CommonApplication) getApplication();
        initUI();
        switchSelectedFragment(ll_home);
        initBroadcast();
    }
 
    /**
     * Dispatch onPause() to fragments.
     */
    @Override
    protected void onPause() {
        super.onPause();
    }
 
    @Override
    protected void onStop() {
        super.onStop();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mainReceiver);
    }
 
    private void initUI() {
        ll_home = (LinearLayout) findViewById(R.id.ll_home);
        ll_inspection_infos = findViewById(R.id.ll_inspection_infos);
        ll_task = (LinearLayout) findViewById(R.id.ll_task);
        ll_inspection = (LinearLayout) findViewById(R.id.ll_inspection);
        ll_anysis = (LinearLayout) findViewById(R.id.ll_anysis);
        ll_setting = (LinearLayout) findViewById(R.id.ll_setting);
        fl_fragmentContainer = (FrameLayout) findViewById(R.id.fl_fragment_container);
 
        tabList = new ArrayList<>();
        fragmentList.clear();
        mInspectionFragment = new InspectionFragment();
        //region 不同用户的主页不同
        if (app.getCurrentUser().getUsertypeid() == 1 || app.getCurrentUser().getUsertypeid() == 0) {//公司内部人员
            tabList.add(ll_home);
//            tabList.add(ll_inspection_infos);
            tabList.add(ll_task);
            tabList.add(ll_inspection);
//            tabList.add(ll_anysis);
            tabList.add(ll_setting);
            fragmentList.add(new HomeFragment());
            fragmentList.add(new TaskFragment());
            fragmentList.add(mInspectionFragment);
//            fragmentList.add(new AnysisFragment());
            fragmentList.add(new SettingFragment());
 
            ll_inspection_infos.setVisibility(View.GONE);
            ll_anysis.setVisibility(View.GONE);
        } else if (app.getCurrentUser().getUsertypeid() == 2) {//环保部门
            tabList.add(ll_home);
//            tabList.add(ll_inspection_infos);
//            tabList.add(ll_task);
            tabList.add(ll_inspection);
            tabList.add(ll_anysis);
            tabList.add(ll_setting);
            fragmentList.add(new HomeFragment_comptent());
            fragmentList.add(mInspectionFragment);
            fragmentList.add(AnalysisOverViewFragment.newInstance());
            fragmentList.add(new SettingFragment());
 
            ll_inspection_infos.setVisibility(View.GONE);
            ll_task.setVisibility(View.GONE);
        } else if (app.getCurrentUser().getUsertypeid() == 3) {//企业
            tabList.add(ll_home);
            tabList.add(ll_inspection_infos);
//            tabList.add(ll_task);
//            tabList.add(ll_inspection);
//            tabList.add(ll_anysis);
            tabList.add(ll_setting);
            fragmentList.add(new HomeFragment_scene());
            fragmentList.add(InspectionInfoFragment.newInstance());
            fragmentList.add(new SettingFragment());
 
            ll_task.setVisibility(View.GONE);
            ll_inspection.setVisibility(View.GONE);
            ll_anysis.setVisibility(View.GONE);
        }
        //endregion
 
        ll_home.setOnClickListener(this);
        ll_inspection_infos.setOnClickListener(this);
        ll_task.setOnClickListener(this);
        ll_inspection.setOnClickListener(this);
        ll_anysis.setOnClickListener(this);
        ll_setting.setOnClickListener(this);
    }
 
    private void initBroadcast(){
        mainReceiver = new MainReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.GOTOSUBTASK);
        filter.addAction(Constant.GOTO_INSPECTION_FRAGMENT);
        filter.addAction(Constant.REFRESH_DATA);
        registerReceiver(mainReceiver, filter);
    }
 
    private void switchSelectedFragment(LinearLayout ll) {
        for (LinearLayout ll_tab : tabList) {
            ll_tab.setSelected(false);
        }
        ll.setSelected(true);
        int position = tabList.indexOf(ll);
        if (position > -1) {
            fragmentTransaction = getSupportFragmentManager().beginTransaction();
            if (!fragmentList.get(position).isAdded()) {
                if (fragmentCurrent != null) {
                    fragmentTransaction.hide(fragmentCurrent);
                }
                fragmentTransaction.add(R.id.fl_fragment_container, fragmentList.get(position));
            } else if (fragmentCurrent != fragmentList.get(position)) {
                fragmentTransaction.hide(fragmentCurrent);
                fragmentTransaction.show(fragmentList.get(position));
            }
            fragmentCurrent = fragmentList.get(position);
            fragmentTransaction.commitAllowingStateLoss();
        }
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                switchSelectedFragment(ll_home);
                break;
            case R.id.ll_inspection_infos:
                switchSelectedFragment(ll_inspection_infos);
                break;
            case R.id.ll_task:
                switchSelectedFragment(ll_task);
                break;
            case R.id.ll_inspection:
                switchSelectedFragment(ll_inspection);
                break;
            case R.id.ll_anysis:
                switchSelectedFragment(ll_anysis);
                break;
            case R.id.ll_setting:
                switchSelectedFragment(ll_setting);
                break;
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUESTCODE_FROM_TASKFRAGMENT && requestCode == RESULT_OK) {
            final Subtask subtask = (Subtask) data.getSerializableExtra(Constant.KEY_INTENT_SUBTASK);
            ll_home.postDelayed(new Runnable() {
                @Override
                public void run() {
                    switchSelectedFragment(ll_inspection);
//                    mInspectionFragment.loadInspection(subtask);
                }
            }, 2000);
        }
    }
 
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
 
    @Override
    public void onBackPressed() {
        if (exist){
            Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
            launcherIntent.addCategory(Intent.CATEGORY_HOME);
            startActivity(launcherIntent);
//
//            finish();
        }else {
            exist = true;
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(0,1000);
        }
    }
 
    class MainReceiver extends BroadcastReceiver{
 
        @Override
        public void onReceive(final Context context, Intent intent) {
            switch (intent.getAction()){
                case Constant.GOTOSUBTASK:
                    final Subtask  subtask = (Subtask) intent.getSerializableExtra("subtask");
                    if (subtask != null) {
                        switchSelectedFragment(ll_inspection);
                        ll_home.postDelayed(new Runnable() {
                            @Override
                            public void run() {
//                                Log.e("Subtask", subtask.getStguid());
//                            if (fragmentCurrent instanceof InspectionFragment)
//                                ((InspectionFragment) fragmentCurrent).loadInspection(subtask);
//                                mInspectionFragment.loadInspection(subtask);
//                                Log.e("Subtask2", subtask.getStguid());
                            }
                        }, 2000);
                    }
                    break;
                case Constant.GOTO_INSPECTION_FRAGMENT:
                    switchSelectedFragment(ll_inspection_infos);
                    break;
                case Constant.REFRESH_DATA:
                    if (app.getCurrentUser().getUsertypeid() == 3) {//企业用户
                        HomeFragment_scene h = (HomeFragment_scene) fragmentList.get(0);
                        h.setUnHiddenRefresh(true);
                        ProblemlistVo p = (ProblemlistVo) intent.getSerializableExtra(ProblemChangeDetailActivity.ARG_PROBLEM);
                        InspectionInfoFragment i = (InspectionInfoFragment) fragmentList.get(1);
                        i.updateData();
                    }
                    break;
            }
        }
    }
 
//    /***
//     * 根据传入的位置 给menu 控制器数组对象赋值
//     *
//     * @param position
//     * @param onMenuController
//     */
//    public void setOnMenuController(@IntRange(from = 0, to = 4) int position, OnMenuController onMenuController) {
//        onMenuControllers[position] = onMenuController;
//        invalidateOptionsMenu();
//    }
//
//    public interface OnMenuController {
//        /**
//         * 获取页面所加载的menu文件
//         *
//         * @return
//         */
//        int getMenuRes();
//
//        /**
//         * 页面menu创建的回调方法
//         *
//         * @param menu
//         */
//        void onMenuCreate(Menu menu);
//
//        /***
//         * menu 中每一项菜单选中的事件
//         *
//         * @param item
//         */
//        void onMenuItemSelected(MenuItem item);
//
//    }
}