Android Open Source - Test-SimpleTwitterClient Connected User Timeline Fragment






From Project

Back to project page Test-SimpleTwitterClient.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project Test-SimpleTwitterClient listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.Twitter_Android.Fragments;
// w  ww.ja  v  a  2 s . c o m
import android.app.Activity;
import android.app.DialogFragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import com.example.Twitter_Android.Fragments.Adapters.TimelineAdapter;
import com.example.Twitter_Android.Fragments.Adapters.TweetAdapter;
import com.example.Twitter_Android.Fragments.Dialogs.DeleteTweetDialog;
import com.example.Twitter_Android.Fragments.Dialogs.PostTweetDialog;
import com.example.Twitter_Android.Fragments.Dialogs.ReplyDialog;
import com.example.Twitter_Android.Loaders.UserTimelineLoader;
import com.example.Twitter_Android.Logic.Constants;
import com.example.Twitter_Android.Logic.DataCache;
import com.example.Twitter_Android.Logic.Tweet;
import com.example.Twitter_Android.R;

import java.util.List;

public class ConnectedUserTimelineFragment extends TimelineFragment<Tweet> {
  private TimelineAdapter<Tweet> currentAdapter;
  private static final DataCache cache;

  private static final int TIMELINE_LOADER_ID = Constants.CONNECTED_USER_TIMELINE_LOADER;
  private static final int OLD_TWEETS_LOADER = Constants.CONNECTED_USER_OLD_TWEETS_LOADER;
  public static final String TAG = "TAG_CONNECTED_USER_TIMELINE";
  private static final String ADAPTER_TAG = "CON_U_TL_F";
  private static final String MAX_ID = "MAX_ID";
  private static final ConnectedUserTimelineFragment instance;

  static {
    instance = new ConnectedUserTimelineFragment();
    instance.setRetainInstance(true);
    instance.setHasOptionsMenu(true);
    cache = DataCache.getInstance();
  }

  //------------------------------------------------------------------------------------------------------------------
  public static ConnectedUserTimelineFragment getInstance() {
    return instance;
  }
  //------------------------------------------------------------------------------------------------------------------

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
  }

  //------------------------------------------------------------------------------------------------------------------
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (currentAdapter == null) {
      /*
        ????? ?????????? ????????, ????? ?? ???????????? ? ????? ?????????, ???????? ?? ?????? ????????.
        ? ????? ??????, ????? ?? ??????? ????? ????? ? ?? ????????? ??????? (?????), ????? ??????????? ???????
        ? ?????????? ???.
       */
      currentAdapter = (TweetAdapter) cache.getAdapter(ADAPTER_TAG);
      if (currentAdapter == null) {
        LoaderManager loaderManager = getLoaderManager();
        if (loaderManager != null) {
          loaderManager.initLoader(TIMELINE_LOADER_ID, null, this);
        }
      } else {
        setListAdapter(currentAdapter);
        currentAdapter.updateContext(getActivity());
      }
    } else {
      /*
        ?.?. ?????? ???????? setRetainInstance(true), ?? ??? ????????? ????????????
        (??? ???????? ??????, ????????) ?????????? ???????? ?????? ?? MainActivity.
        ????? ????????? ?????? ???? "java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState",
        ????? ?????? ???????? ???????????? ? ??????????? (? ?? ? ???????) ????????.
       */
      currentAdapter.updateContext(getActivity());
    }
    needNewestTweet = false;
  }

  @Override
  protected void loadOldItems() {
    long maxID = getMaxId();
    Bundle args = new Bundle();
    args.putLong(MAX_ID, maxID);
    LoaderManager loaderManager = getLoaderManager();
    if (loaderManager != null) {
      loaderManager.restartLoader(OLD_TWEETS_LOADER, args, this);
    }
  }

  //------------------------------------------------------------------------------------------------------------------

  @Override
  public Loader<List<Tweet>> onCreateLoader(int id, Bundle args) {
    return new UserTimelineLoader(getActivity(), cache.getConnectedUserID(), 0, 0);
  }

  @Override
  public void onLoadFinished(Loader<List<Tweet>> loader, List<Tweet> data) {
    if ((data.size() > 0) && (loader != null)) {
      switch (loader.getId()) {
        case TIMELINE_LOADER_ID:
          if (currentAdapter == null) {
            currentAdapter = new TweetAdapter(getActivity(), data, ADAPTER_TAG);
            setListAdapter(currentAdapter);
          }
          break;
        case OLD_TWEETS_LOADER:
          if (currentAdapter != null) {
            currentAdapter.addItemsToBottom(data);
          }
          break;
      }
    }
    //????????? ???? ?????? - ?????? ?????? ??????? ?????? ?, ??????????????, ?? ?????.
    if (data.size() < TWEETS_BEFORE_END) {
      needOldTweet = false;
    }
    isLoading = false;
  }

  @Override
  public void onLoaderReset(Loader<List<Tweet>> loader) {
  }

  //------------------------------------------------------------------------------------------------------------------
  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Tweet tweetToReply = getItem(position);
    if (tweetToReply != null) {
      Activity activity = getActivity();
      if (activity != null) {
        DialogFragment dialogFragment = ReplyDialog.getInstance(tweetToReply);
        dialogFragment.show(getActivity().getFragmentManager().beginTransaction(), ReplyDialog.TAG);
        return true;
      }
    }
    return false;
  }

  //------------------------------------------------------------------------------------------------------------------

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_my_tweets, menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_post_tweet:
        postTweet();
        return true;
      case R.id.menu_item_remove_tweet:
        deleteTweet();
        return true;
    }
    return super.onOptionsItemSelected(item);
  }

  private void postTweet() {
    Activity activity = getActivity();
    if (activity != null) {
      DialogFragment dialog = PostTweetDialog.newInstance(currentAdapter);
      dialog.show(activity.getFragmentManager().beginTransaction(), PostTweetDialog.TAG);
    }
  }

  private void deleteTweet() {
    Activity activity = getActivity();
    if (activity != null) {
      DialogFragment dialog = DeleteTweetDialog.getInstance(currentAdapter, getSelectedItem());
      dialog.show(activity.getFragmentManager().beginTransaction(), DeleteTweetDialog.TAG);
    }
  }
}




Java Source Code List

com.example.Twitter_Android.AppActivity.ConcreteUserTimelineActivity.java
com.example.Twitter_Android.AppActivity.MainActivity.java
com.example.Twitter_Android.AppActivity.MessagesActivity.java
com.example.Twitter_Android.AppActivity.SearchableActivity.java
com.example.Twitter_Android.AppActivity.SettingsActivity.java
com.example.Twitter_Android.AsynkTasks.ImageDownloader.java
com.example.Twitter_Android.AsynkTasks.TaskDeleteTweet.java
com.example.Twitter_Android.AsynkTasks.TaskFollow.java
com.example.Twitter_Android.AsynkTasks.TaskPostTweet.java
com.example.Twitter_Android.AsynkTasks.TaskReply.java
com.example.Twitter_Android.AsynkTasks.TaskRetweet.java
com.example.Twitter_Android.AsynkTasks.TaskSendMessage.java
com.example.Twitter_Android.AsynkTasks.TaskUnfollow.java
com.example.Twitter_Android.Fragments.ConcreteUserTimelineFragment.java
com.example.Twitter_Android.Fragments.ConnectedUserTimelineFragment.java
com.example.Twitter_Android.Fragments.FollowersFragment.java
com.example.Twitter_Android.Fragments.FollowingsFragment.java
com.example.Twitter_Android.Fragments.HomeTimelineFragment.java
com.example.Twitter_Android.Fragments.TimelineFragment.java
com.example.Twitter_Android.Fragments.UserInfoFragment.java
com.example.Twitter_Android.Fragments.Adapters.ConcreteUserTimelineAdapter.java
com.example.Twitter_Android.Fragments.Adapters.FollowingsListAdapter.java
com.example.Twitter_Android.Fragments.Adapters.MessagesAdapter.java
com.example.Twitter_Android.Fragments.Adapters.TimelineAdapter.java
com.example.Twitter_Android.Fragments.Adapters.TweetAdapter.java
com.example.Twitter_Android.Fragments.Dialogs.DeleteTweetDialog.java
com.example.Twitter_Android.Fragments.Dialogs.DirectMessageDialog.java
com.example.Twitter_Android.Fragments.Dialogs.ErrorDialog.java
com.example.Twitter_Android.Fragments.Dialogs.PostTweetDialog.java
com.example.Twitter_Android.Fragments.Dialogs.ReplyDialog.java
com.example.Twitter_Android.Fragments.Dialogs.RetweetDialog.java
com.example.Twitter_Android.Fragments.Dialogs.ShowImageDialog.java
com.example.Twitter_Android.Fragments.Dialogs.UserInfoDialog.java
com.example.Twitter_Android.Fragments.Preferences.SettingsFragment.java
com.example.Twitter_Android.Loaders.AsyncUserInfoLoader.java
com.example.Twitter_Android.Loaders.FollowersLoader.java
com.example.Twitter_Android.Loaders.FollowingsLoader.java
com.example.Twitter_Android.Loaders.HomeTimelineLoader.java
com.example.Twitter_Android.Loaders.MessageLoader.java
com.example.Twitter_Android.Loaders.Task_LoadSearchedTweets.java
com.example.Twitter_Android.Loaders.Task_LoadSearchedUsers.java
com.example.Twitter_Android.Loaders.TweetLoader.java
com.example.Twitter_Android.Loaders.UserTimelineLoader.java
com.example.Twitter_Android.Logic.AppDatabase.java
com.example.Twitter_Android.Logic.Constants.java
com.example.Twitter_Android.Logic.DataCache.java
com.example.Twitter_Android.Logic.FileWorker.java
com.example.Twitter_Android.Logic.Person.java
com.example.Twitter_Android.Logic.Tweet.java
com.example.Twitter_Android.Net.Connector.java
com.example.Twitter_Android.Net.Parser.java
com.mass.cmassive.CMassive.java