Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gauss.movv.main.fragment;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.ImageButton;
- import android.widget.ListView;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import com.gauss.movv.R;
- import com.gauss.movv.base.fragment.BaseFragment;
- import com.gauss.movv.camera.activity.CameraActivity;
- import com.gauss.movv.commons.BusProvider;
- import com.gauss.movv.commons.MovvService;
- import com.gauss.movv.commons.PreferenceHelper;
- import com.gauss.movv.inbox.activity.InboxActivity;
- import com.gauss.movv.main.adapter.HomeFeedAdapter;
- import com.gauss.movv.main.model.FeedRequest;
- import com.gauss.movv.main.model.FeedResponse;
- import com.gauss.movv.main.model.HomeFeedModel;
- import com.gauss.movv.main.model.ProductsDataModel;
- import com.gauss.movv.news.activity.NewsActivity;
- import com.gauss.movv.profile.activity.ProfileFinalActivity;
- import com.gauss.movv.search.activity.SearchActivity;
- import com.gauss.movv.users.model.LoginRequest;
- import com.squareup.otto.Subscribe;
- import java.util.ArrayList;
- import retrofit.RetrofitError;
- /**
- * Created by Laptop on 8.6.2015..
- */
- public class MainFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {
- public static final String DETAILS_KEY = "details_key";
- private ListView mPostsListView;
- private SwipeRefreshLayout mSwipeRefreshLayout;
- ArrayList<ProductsDataModel> mProducts = new ArrayList<>();
- private RadioGroup mNavigationBottomRadioGroup;
- private RadioButton mHomeRadioButton;
- private RadioButton mNewsRadioButton;
- private RadioButton mSearchRadioButton;
- private RadioButton mProfileRadioButton;
- private RadioButton mInboxRadioButton;
- FeedRequest feedRequest;
- private HomeFeedAdapter homeFeedAdapter;
- private ImageButton mActionButton;
- private int mScrollState = AbsListView.OnScrollListener.SCROLL_STATE_IDLE;
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_main, container, false);
- }
- @Override
- public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- prepareUI(view);
- initListeners();
- if (PreferenceHelper.getUserId(getActivity().getApplicationContext()) != null)
- Toast.makeText(getActivity(), "tu si", Toast.LENGTH_SHORT).show();
- feedRequest = new FeedRequest();
- feedRequest.setUserId(Long.valueOf(PreferenceHelper.getUserId(getActivity())));
- setAdapter();
- // DataModel data[] = new DataModel[]{
- // new DataModel(R.drawable.user1, "@ivan", "Zagreb, Croatia", " 10m", "Sony Xperia Z1", "technology, phone, mobile", " 21", " 5", "$120", R.drawable.soccer_shoes, R.raw.small_video),
- // new DataModel(R.drawable.user2, "@john", "Paris, France", " 15m", "Lenovo B596", "technology, laptop, computer", " 30", " 8", "$550", R.drawable.soccer_shoes, R.raw.small_video),
- // new DataModel(R.drawable.user3, "@melisa", "Split, Croatia", " 20m", "Samsung S4", "technology, phone, mobile", " 22", " 11", "$150", R.drawable.soccer_shoes, R.raw.small_video),
- // new DataModel(R.drawable.user1, "@tibor", "Budimpesta, Hungary", " 10m", "Sony Xperia Z1", "technology, phone, mobile", " 21", " 5", "$120", R.drawable.soccer_shoes, R.raw.small_video),
- // new DataModel(R.drawable.user2, "@mark", "Sarajevo, BiH", " 15m", "Lenovo B596", "technology, laptop, computer", " 30", " 8", "$550", R.drawable.soccer_shoes, R.raw.small_video),
- // new DataModel(R.drawable.user3, "@helen", "London, England", " 20m", "Samsung S4", "technology, phone, mobile", " 22", " 11", "$150", R.drawable.soccer_shoes, R.raw.small_video),
- //
- // };
- //
- // final PostsListsAdapter adapter = new PostsListsAdapter(getActivity(), R.layout.list_item_post, data);
- //
- // mPostsListView.setAdapter(adapter);
- //
- // mPostsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- // @Override
- // public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- //
- // DataModel model = adapter.getItem(position);
- // Intent intent = new Intent(getActivity(), ItemDetailsActivity.class);
- // intent.putExtra(DETAILS_KEY, model);
- // startActivity(intent);
- //
- // }
- // });
- }
- @Subscribe
- public void onFeedSucess(FeedResponse feedResponse) {
- Log.d("retrofit", "uspjesno");
- mProducts = feedResponse.getProducts();
- setAdapter();
- }
- @Subscribe
- public void onFeedFailure(RetrofitError error){
- Log.d("retrofit", "greska");
- }
- private void setAdapter() {
- if (mProducts == null) {
- return;
- }
- if (mPostsListView.getAdapter() == null) {
- homeFeedAdapter = new HomeFeedAdapter(getActivity(), mProducts);
- // materialsListAdapter.setListener(this);
- mPostsListView.setAdapter(homeFeedAdapter);
- } else {
- homeFeedAdapter.refreshData(mProducts);
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- BusProvider.getInstance().register(this);
- MovvService.getUserFeed(feedRequest);
- }
- @Override
- public void onPause() {
- super.onPause();
- BusProvider.getInstance().unregister(this);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- }
- @Override
- protected void prepareUI(@NonNull View layoutView) {
- mPostsListView = (ListView) layoutView.findViewById(R.id.adapter_view);
- mNavigationBottomRadioGroup = (RadioGroup) layoutView.findViewById(R.id.radio_group);
- mInboxRadioButton = (RadioButton) layoutView.findViewById(R.id.inbox_bar);
- mNewsRadioButton = (RadioButton) layoutView.findViewById(R.id.news_bar);
- mHomeRadioButton = (RadioButton) layoutView.findViewById(R.id.home_bar);
- mSearchRadioButton = (RadioButton) layoutView.findViewById(R.id.search_bar);
- mProfileRadioButton = (RadioButton) layoutView.findViewById(R.id.profile_bar);
- mActionButton = (ImageButton) layoutView.findViewById(R.id.floating_action_button);
- mActionButton.setBackgroundResource(0);
- mSearchRadioButton.setButtonDrawable(R.mipmap.search_bar_button);
- mNewsRadioButton.setButtonDrawable(R.mipmap.news_bar_button);
- mProfileRadioButton.setButtonDrawable(R.mipmap.profile_bar_button);
- mInboxRadioButton.setButtonDrawable(R.mipmap.inbox_bar_button);
- mHomeRadioButton.setButtonDrawable(R.mipmap.home_bar_button);
- }
- @Override
- protected void prepareData() {
- }
- protected void initListeners() {
- mInboxRadioButton.setOnClickListener(this);
- mNewsRadioButton.setOnClickListener(this);
- mHomeRadioButton.setOnClickListener(this);
- mSearchRadioButton.setOnClickListener(this);
- mProfileRadioButton.setOnClickListener(this);
- mActionButton.setOnClickListener(this);
- }
- @Override
- public void onActivityCreated(@Nullable Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- prepareData();
- }
- @Override
- public void onRefresh() {
- prepareData();
- mSwipeRefreshLayout.setRefreshing(true);
- }
- @Override
- public void onClick(View view) {
- RadioButton checkedAnswer = (RadioButton) view.findViewById(mNavigationBottomRadioGroup.getCheckedRadioButtonId());
- switch (view.getId()) {
- case R.id.home_bar:
- if (checkedAnswer == mHomeRadioButton) {
- replaceFragment(R.id.fragment_container, new MainFragment(), false);
- }
- break;
- case R.id.news_bar:
- if (checkedAnswer == mNewsRadioButton) {
- Intent intent = new Intent(getActivity(), NewsActivity.class);
- startActivity(intent);
- }
- break;
- case R.id.search_bar:
- if (checkedAnswer == mSearchRadioButton) {
- Intent intent = new Intent(getActivity(), SearchActivity.class);
- startActivity(intent);
- }
- break;
- case R.id.profile_bar:
- if (checkedAnswer == mProfileRadioButton) {
- Intent intent = new Intent(getActivity(), ProfileFinalActivity.class);
- startActivity(intent);
- }
- break;
- case R.id.inbox_bar:
- if (checkedAnswer == mInboxRadioButton) {
- Intent intent = new Intent(getActivity(), InboxActivity.class);
- startActivity(intent);
- }
- break;
- case R.id.floating_action_button:
- Intent intent = new Intent(getActivity(), CameraActivity.class);
- startActivity(intent);
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement