| | |
| | | package com.application.zhangshi_app_android.ui.main; |
| | | |
| | | |
| | | import android.app.Activity; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.text.TextUtils; |
| | | import android.view.MenuItem; |
| | | import android.view.View; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.fragment.app.Fragment; |
| | | import androidx.lifecycle.Observer; |
| | | import androidx.viewpager2.widget.ViewPager2; |
| | | |
| | | import com.android.app_base.base.adapter.BaseVp2FragmentAdapter; |
| | |
| | | import com.android.app_base.base.view.BaseFragment; |
| | | import com.android.app_base.helper.DoubleClickHelper; |
| | | import com.android.app_base.manager.AppManager; |
| | | import com.android.app_base.manager.UserManager; |
| | | import com.android.app_base.utils.rxbus.MessageEvent; |
| | | import com.android.app_base.utils.rxbus.RxBus; |
| | | import com.application.zhangshi_app_android.BR; |
| | | import com.application.zhangshi_app_android.R; |
| | | import com.application.zhangshi_app_android.bean.GrowthExperienceInformationBean; |
| | | import com.application.zhangshi_app_android.bean.UpdateVersionBean; |
| | | import com.application.zhangshi_app_android.databinding.ActivityMainBinding; |
| | | import com.application.zhangshi_app_android.ui.dialog.UpdateDialog; |
| | | import com.application.zhangshi_app_android.ui.dialog.VersionUpdateDialog; |
| | | import com.application.zhangshi_app_android.ui.function.FunctionFragment; |
| | | import com.application.zhangshi_app_android.ui.home.HomeFragment; |
| | | import com.application.zhangshi_app_android.ui.personal_center.PersonalCenterFragment; |
| | | import com.blankj.utilcode.util.AppUtils; |
| | | import com.blankj.utilcode.util.ToastUtils; |
| | | import com.google.android.material.navigation.NavigationBarView; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * @author Ljj |
| | |
| | | * @desc app主页 |
| | | */ |
| | | public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> { |
| | | BaseVp2FragmentAdapter<BaseFragment> mPagerAdapter; |
| | | @Override |
| | | public int getLayoutId() { |
| | | return R.layout.activity_main; |
| | |
| | | |
| | | @Override |
| | | public void initView() { |
| | | BaseVp2FragmentAdapter<BaseFragment> mPagerAdapter = new BaseVp2FragmentAdapter<>(this); |
| | | mPagerAdapter = new BaseVp2FragmentAdapter<>(this); |
| | | mPagerAdapter.addFragment(HomeFragment.newInstance()); |
| | | mPagerAdapter.addFragment(FunctionFragment.newInstance()); |
| | | mPagerAdapter.addFragment(PersonalCenterFragment.newInstance()); |
| | |
| | | } |
| | | }); |
| | | binding.mainVp.setUserInputEnabled(false); |
| | | |
| | | binding.mainVp.setOffscreenPageLimit(3); |
| | | // 不使用图标默认变色 |
| | | binding.mainBnv.setItemIconTintList(null); |
| | | binding.mainBnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() { |
| | |
| | | |
| | | @Override |
| | | public void initData() { |
| | | |
| | | viewModel.getUpdateVersion(); |
| | | if (TextUtils.isEmpty(UserManager.getInstance().getUserName()) || TextUtils.isEmpty(UserManager.getInstance().getUserAvatar())) { |
| | | viewModel.getInfo(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void initLiveDataObserve() { |
| | | viewModel.getUpdateVersionBeanLiveData().observe(this, new Observer<UpdateVersionBean>() { |
| | | @Override |
| | | public void onChanged(UpdateVersionBean updateVersionBean) { |
| | | if(updateVersionBean != null && updateVersionBean.getVersionCode()> AppUtils.getAppVersionCode()&&!TextUtils.isEmpty(updateVersionBean.getAppUrl())){ |
| | | // VersionUpdateDialog.Builder dialogBuilder = new VersionUpdateDialog.Builder(getSelfActivity()) |
| | | // .setContent(updateVersionBean.getContent()) |
| | | // .setDownloadUrl(updateVersionBean.getAppUrl()) |
| | | // ; |
| | | // if(updateVersionBean.getForceUpdate() == 1){ |
| | | // dialogBuilder.setCancelable(false); |
| | | // dialogBuilder.setCanceledOnTouchOutside(false); |
| | | // } |
| | | // dialogBuilder.show(); |
| | | |
| | | new UpdateDialog.Builder(getSelfActivity()) |
| | | // 版本名 |
| | | .setVersionName(updateVersionBean.getVersionName()) |
| | | // 是否强制更新 |
| | | .setForceUpdate(updateVersionBean.getForceUpdate()==1) |
| | | // 更新日志 |
| | | .setUpdateLog(updateVersionBean.getContent()) |
| | | // 下载 URL |
| | | .setDownloadUrl(updateVersionBean.getAppUrl()) |
| | | .show(); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | viewModel.getInfoLiveData().observe(this, new Observer<GrowthExperienceInformationBean>() { |
| | | @Override |
| | | public void onChanged(GrowthExperienceInformationBean informationBean) { |
| | | if(informationBean != null){ |
| | | UserManager.getInstance().setUserName(informationBean.getNickName()); |
| | | UserManager.getInstance().setUserAvatar(informationBean.getImg()); |
| | | RxBus.getInstance().post(new MessageEvent(MessageEvent.EVENT_UPDATE_USER_INFO,informationBean)); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | |
| | | ToastUtils.showShort("再按一次退出"); |
| | | } |
| | | } |
| | | |
| | | private static final String INTENT_KEY_IN_FRAGMENT_INDEX = "fragmentIndex"; |
| | | private static final String INTENT_KEY_IN_FRAGMENT_CLASS = "fragmentClass"; |
| | | |
| | | public static void start(Context context) { |
| | | start(context, HomeFragment.class); |
| | | } |
| | | |
| | | public static void start(Context context, Class<? extends BaseFragment> fragmentClass) { |
| | | Intent intent = new Intent(context, MainActivity.class); |
| | | intent.putExtra(INTENT_KEY_IN_FRAGMENT_CLASS, fragmentClass); |
| | | if (!(context instanceof Activity)) { |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| | | } |
| | | context.startActivity(intent); |
| | | } |
| | | |
| | | } |