Commit 841b9f94 by 段启岩

更新栏目加载方式-工具用户登录状态加载

parent 8389aa75
......@@ -8,6 +8,7 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.chad.library.adapter.base.BaseQuickAdapter;
......@@ -25,6 +26,7 @@ import cn.yunliyunwai.beyondclouds.ui.common.RefreshableFragment;
import cn.yunliyunwai.beyondclouds.ui.common.listener.OnNavigationReselectedListener;
import cn.yunliyunwai.beyondclouds.util.CategoryViewpagerUtils;
import cn.yunliyunwai.beyondclouds.util.ContextUtils;
import cn.yunliyunwai.beyondclouds.viewmodel.AuthenticationViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.DynamicFragmentViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.DynamicListFragmentViewModel;
......@@ -34,6 +36,9 @@ public class DynamicFragment extends BaseFragment<DynamicFragmentViewModel, Frag
@Inject
ViewModelProvider.Factory factory;
@Inject
AuthenticationViewModel authenticationViewModel;
@Override
protected void initInject() {
ContextUtils.getAppComponent(requireActivity()).inject(this);
......@@ -53,6 +58,17 @@ public class DynamicFragment extends BaseFragment<DynamicFragmentViewModel, Frag
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
authenticationViewModel.getAuthenticationState().observe(this, new Observer<AuthenticationViewModel.AuthenticationState>() {
@Override
public void onChanged(AuthenticationViewModel.AuthenticationState authenticationState) {
if (authenticationState == AuthenticationViewModel.AuthenticationState.AUTHENTICATED) {
viewModel.loadCategories(true);
} else {
viewModel.loadCategories(false);
}
}
});
CategoryViewpagerUtils.bindViewPager(this, new FragmentFactory() {
@Override
public Fragment create() {
......
......@@ -9,6 +9,7 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.chad.library.adapter.base.BaseQuickAdapter;
......@@ -29,6 +30,7 @@ import cn.yunliyunwai.beyondclouds.ui.popup.PopupAddMenu;
import cn.yunliyunwai.beyondclouds.util.CategoryViewpagerUtils;
import cn.yunliyunwai.beyondclouds.util.ContextUtils;
import cn.yunliyunwai.beyondclouds.util.DimensionUtils;
import cn.yunliyunwai.beyondclouds.viewmodel.AuthenticationViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.BlogListFragmentViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.HomeFragmentViewModel;
......@@ -37,6 +39,9 @@ public class HomeFragment extends BaseFragment<HomeFragmentViewModel, FragmentHo
@Inject
ViewModelProvider.Factory factory;
@Inject
AuthenticationViewModel authenticationViewModel;
@Override
protected void initInject() {
((BeyondApplication)(requireActivity().getApplication())).getAppComponent().inject(this);
......@@ -61,7 +66,18 @@ public class HomeFragment extends BaseFragment<HomeFragmentViewModel, FragmentHo
return new BlogListFragment();
}
}, binding.magicIndicator, binding.viewPager, viewModel, false);
viewModel.loadCategories(authenticationViewModel.getAuthenticationState().getValue() == AuthenticationViewModel.AuthenticationState.AUTHENTICATED);
authenticationViewModel.getAuthenticationState().observe(this, new Observer<AuthenticationViewModel.AuthenticationState>() {
@Override
public void onChanged(AuthenticationViewModel.AuthenticationState authenticationState) {
if (authenticationState == AuthenticationViewModel.AuthenticationState.AUTHENTICATED) {
viewModel.loadCategories(true);
} else {
viewModel.loadCategories(false);
}
}
});
PopupAddMenu popupAddMenu = new PopupAddMenu(getContext());
binding.searchBar.setRightIconClickListener(new View.OnClickListener() {
......
......@@ -9,6 +9,7 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.chad.library.adapter.base.BaseQuickAdapter;
......@@ -27,6 +28,7 @@ import cn.yunliyunwai.beyondclouds.ui.common.RefreshableFragment;
import cn.yunliyunwai.beyondclouds.ui.common.listener.OnNavigationReselectedListener;
import cn.yunliyunwai.beyondclouds.util.CategoryViewpagerUtils;
import cn.yunliyunwai.beyondclouds.util.ContextUtils;
import cn.yunliyunwai.beyondclouds.viewmodel.AuthenticationViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.ProjectFragmentViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.ProjectListFragmentViewModel;
......@@ -36,6 +38,9 @@ public class ProjectFragment extends BaseFragment<ProjectFragmentViewModel, Frag
@Inject
ViewModelProvider.Factory factory;
@Inject
AuthenticationViewModel authenticationViewModel;
@Override
protected void initInject() {
((BeyondApplication)(requireActivity().getApplication())).getAppComponent().inject(this);
......@@ -54,6 +59,18 @@ public class ProjectFragment extends BaseFragment<ProjectFragmentViewModel, Frag
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
authenticationViewModel.getAuthenticationState().observe(this, new Observer<AuthenticationViewModel.AuthenticationState>() {
@Override
public void onChanged(AuthenticationViewModel.AuthenticationState authenticationState) {
if (authenticationState == AuthenticationViewModel.AuthenticationState.AUTHENTICATED) {
viewModel.loadCategories(true);
} else {
viewModel.loadCategories(false);
}
}
});
CategoryViewpagerUtils.bindViewPager(this, new FragmentFactory() {
@Override
public Fragment create() {
......
......@@ -8,6 +8,7 @@ import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.chad.library.adapter.base.BaseQuickAdapter;
......@@ -26,6 +27,7 @@ import cn.yunliyunwai.beyondclouds.ui.common.listener.OnNavigationReselectedList
import cn.yunliyunwai.beyondclouds.util.CategoryViewpagerUtils;
import cn.yunliyunwai.beyondclouds.util.ContextUtils;
import cn.yunliyunwai.beyondclouds.util.ViewModelUtils;
import cn.yunliyunwai.beyondclouds.viewmodel.AuthenticationViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.QAFragmentViewModel;
import cn.yunliyunwai.beyondclouds.viewmodel.QuestionListFragmentViewModel;
......@@ -35,6 +37,9 @@ public class QAFragment extends BaseFragment<QAFragmentViewModel, FragmentQaBind
@Inject
ViewModelProvider.Factory factory;
@Inject
AuthenticationViewModel authenticationViewModel;
@Override
protected void initInject() {
ContextUtils.getAppComponent(requireActivity()).inject(this);
......@@ -53,6 +58,18 @@ public class QAFragment extends BaseFragment<QAFragmentViewModel, FragmentQaBind
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
authenticationViewModel.getAuthenticationState().observe(this, new Observer<AuthenticationViewModel.AuthenticationState>() {
@Override
public void onChanged(AuthenticationViewModel.AuthenticationState authenticationState) {
if (authenticationState == AuthenticationViewModel.AuthenticationState.AUTHENTICATED) {
viewModel.loadCategories(true);
} else {
viewModel.loadCategories(false);
}
}
});
CategoryViewpagerUtils.bindViewPager(this, new FragmentFactory() {
@Override
public Fragment create() {
......
......@@ -25,7 +25,6 @@ public class CategoryViewpagerUtils {
magicIndicator.setNavigator(commonNavigator);
ViewPagerHelper.bind(magicIndicator, viewPager);
viewModel.loadCategories();
viewModel
.getCategories()
.observe(fragment.getViewLifecycleOwner(),
......@@ -42,7 +41,6 @@ public class CategoryViewpagerUtils {
magicIndicator.setNavigator(commonNavigator);
ViewPagerHelper.bind(magicIndicator, viewPager);
viewModel.loadCategories();
viewModel
.getCategories()
.observe(activity,
......
......@@ -14,7 +14,8 @@ public class DynamicFragmentViewModel extends CategoryViewModel {
}
@Override
public void loadCategories() {
public void loadCategories(boolean authenticated) {
if (authenticated) {
refreshCategories(Arrays.asList(
CommonCategory.latest(),
CommonCategory.recommend(),
......@@ -24,5 +25,14 @@ public class DynamicFragmentViewModel extends CategoryViewModel {
CommonCategory.photos(),
CommonCategory.videos()
));
} else {
refreshCategories(Arrays.asList(
CommonCategory.latest(),
CommonCategory.recommend(),
CommonCategory.words(),
CommonCategory.photos(),
CommonCategory.videos()));
}
}
}
......@@ -22,7 +22,7 @@ public class HomeFragmentViewModel extends CategoryViewModel {
}
@Override
public void loadCategories() {
public void loadCategories(boolean authenticated) {
List<CommonCategory> commonCategories = new ArrayList<>(Arrays.asList(CommonCategory.recommend(), CommonCategory.latest()));
blogRepository.loadCategoriesFromCache().observeForever(new Observer<List<BlogCategory>>() {
@Override
......
......@@ -23,7 +23,7 @@ public class ProjectFragmentViewModel extends CategoryViewModel {
}
@Override
public void loadCategories() {
public void loadCategories(boolean authenticated) {
List<CommonCategory> commonCategories = new ArrayList<>(Arrays.asList(CommonCategory.recommend(), CommonCategory.latest()));
projectRepository.loadCategoriesFromCache().observeForever(new Observer<List<ProjectCategory>>() {
@Override
......
......@@ -22,7 +22,7 @@ public class QAFragmentViewModel extends CategoryViewModel {
}
@Override
public void loadCategories() {
public void loadCategories(boolean authenticated) {
List<CommonCategory> commonCategories = new ArrayList<>(Arrays.asList(CommonCategory.latest()));
questionRepository.loadCategoriesFromCache().observeForever(new Observer<List<QuestionCategory>>() {
@Override
......
......@@ -20,5 +20,5 @@ public abstract class CategoryViewModel extends ViewModel {
return categories;
}
public abstract void loadCategories();
public abstract void loadCategories(boolean authenticated);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment