PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/celites/sharechannels/ChannelsDetailActivityFragment.java

https://gitlab.com/PrashamTrivedi/sharechannels-for-pushbullet
Java | 398 lines | 319 code | 67 blank | 12 comment | 21 complexity | 4e11f683d28fe1d37272e9e44862c37a MD5 | raw file
  1. package com.celites.sharechannels;
  2. import android.app.Activity;
  3. import android.databinding.DataBindingUtil;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v4.app.Fragment;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.LinearLayoutManager;
  10. import android.support.v7.widget.Toolbar;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.Menu;
  14. import android.view.MenuInflater;
  15. import android.view.MenuItem;
  16. import android.view.SubMenu;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.view.ViewGroup;
  20. import android.widget.ProgressBar;
  21. import android.widget.TextView;
  22. import com.bumptech.glide.Glide;
  23. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  24. import com.bumptech.glide.load.resource.drawable.GlideDrawable;
  25. import com.bumptech.glide.request.animation.GlideAnimation;
  26. import com.bumptech.glide.request.target.SimpleTarget;
  27. import com.ceelites.devutils.ConstantMethods;
  28. import com.ceelites.devutils.SharedPreferenceUtils;
  29. import com.ceelites.librariesdemo.FakeDataProvider;
  30. import com.celites.sharechannels.adapters.ChannelsItemAdapter;
  31. import com.celites.sharechannels.databinding.FragmentChannelsDetailBinding;
  32. import com.celites.sharechannels.models.Channel;
  33. import com.celites.sharechannels.models.ChannelInfo;
  34. import com.celites.sharechannels.models.Contacts;
  35. import com.celites.sharechannels.models.PushbulletInfo;
  36. import com.celites.sharechannels.models.PushbulletSubscriptions;
  37. import com.celites.sharechannels.models.SubscribeRequest;
  38. import com.celites.sharechannels.utils.CircleTransform;
  39. import com.celites.sharechannels.utils.LoadingRecyclerView;
  40. import com.celites.sharechannels.utils.PushRequest;
  41. import com.celites.sharechannels.utils.PushbulletApiService;
  42. import com.celites.sharechannels.utils.Utils;
  43. import java.util.ArrayList;
  44. import java.util.concurrent.TimeUnit;
  45. import rx.Observable;
  46. import rx.Observer;
  47. import rx.Subscriber;
  48. import rx.Subscription;
  49. import rx.android.app.AppObservable;
  50. import rx.android.schedulers.AndroidSchedulers;
  51. /**
  52. * A placeholder fragment containing a simple view.
  53. */
  54. public class ChannelsDetailActivityFragment
  55. extends Fragment {
  56. public ChannelsDetailActivityFragment() {
  57. }
  58. private String tag;
  59. private PushbulletApiService service;
  60. private LinearLayoutManager mLayoutManager;
  61. private ChannelsItemAdapter adapter;
  62. private SharedPreferenceUtils preferenceUtils;
  63. private MenuItem item;
  64. private String iden;
  65. Toolbar channelDetailToolbar;
  66. private AppCompatActivity appCompatActivity;
  67. private ChannelInfo info;
  68. /**
  69. * Instantiates new fragment.
  70. *
  71. * @param bundle
  72. * : Any extra data to be passed to fragment, pass null if you don't want to pass anything.
  73. *
  74. * @return : Instance of current fragment.
  75. */
  76. public static ChannelsDetailActivityFragment newInstance(@NonNull Bundle bundle) {
  77. ChannelsDetailActivityFragment channelsDetailFragment = new ChannelsDetailActivityFragment();
  78. channelsDetailFragment.setArguments(bundle);
  79. return channelsDetailFragment;
  80. }
  81. @Override
  82. public void onAttach(Activity activity) {
  83. super.onAttach(activity);
  84. this.appCompatActivity = activity instanceof AppCompatActivity ? ((AppCompatActivity) activity) : null;
  85. service = Utils.getWebservice(activity);
  86. preferenceUtils = SharedPreferenceUtils.initWith(activity);
  87. }
  88. @Override
  89. public void onCreate(Bundle savedInstanceState) {
  90. super.onCreate(savedInstanceState);
  91. getData();
  92. setHasOptionsMenu(true);
  93. }
  94. @Override
  95. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  96. binding = DataBindingUtil.inflate(inflater, R.layout.fragment_channels_detail, container, false);
  97. return binding.getRoot();
  98. }
  99. @Override
  100. public void onViewCreated(View view, Bundle savedInstanceState) {
  101. super.onViewCreated(view, savedInstanceState);
  102. channelDetailToolbar = binding.channelDetailToolbar;
  103. recentPosts = binding.recentPosts;
  104. channelDescription = binding.channelDescription;
  105. emptyView = binding.emptyView;
  106. progress = binding.progress;
  107. if (appCompatActivity != null) {
  108. appCompatActivity.setSupportActionBar(channelDetailToolbar);
  109. }
  110. fakeDataProvider = new FakeDataProvider();
  111. recentPosts.setHasFixedSize(true);
  112. mLayoutManager = new LinearLayoutManager(getActivity());
  113. mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  114. recentPosts.setEmptyView(emptyView);
  115. recentPosts.setLoadingView(progress);
  116. recentPosts.setLayoutManager(mLayoutManager);
  117. if (ConstantMethods.isOnline(getActivity())) {
  118. AppObservable.bindFragment(this, service.getContacts()).observeOn(AndroidSchedulers.mainThread())
  119. .subscribe(new Observer<PushbulletInfo>() {
  120. @Override
  121. public void onCompleted() {
  122. }
  123. @Override
  124. public void onError(Throwable e) {
  125. }
  126. @Override
  127. public void onNext(PushbulletInfo info) {
  128. contacts = info.getContacts();
  129. getActivity().supportInvalidateOptionsMenu();
  130. }
  131. });
  132. if (!ConstantMethods.isEmptyString(tag)) {
  133. recentPosts.updateLoadingView();
  134. AppObservable.bindFragment(this, service.getChannelInfo(tag)).observeOn(AndroidSchedulers.mainThread())
  135. .subscribe(new Subscriber<ChannelInfo>() {
  136. @Override
  137. public void onCompleted() {
  138. }
  139. @Override
  140. public void onError(Throwable e) {
  141. }
  142. @Override
  143. public void onNext(ChannelInfo info) {
  144. ChannelsDetailActivityFragment.this.info = info;
  145. binding.setChannelInfo(info);
  146. Glide.with(ChannelsDetailActivityFragment.this).load(info.getImageUrl())
  147. .diskCacheStrategy(DiskCacheStrategy.RESULT).placeholder(R.mipmap.ic_launcher).crossFade()
  148. .transform(new CircleTransform(getActivity())).into(new SimpleTarget<GlideDrawable>(50, 50) {
  149. @Override
  150. public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
  151. if (!fakeDataProvider.loadFakeData(binding.channelLogo)) {
  152. binding.channelLogo.setImageDrawable(resource);
  153. }
  154. }
  155. });
  156. adapter = new ChannelsItemAdapter(getActivity(), info.getRecentPushes());
  157. recentPosts.setAdapter(adapter);
  158. }
  159. });
  160. }
  161. } else {
  162. emptyView.setText("You have to be online to get details");
  163. recentPosts.setEmptyView(emptyView);
  164. recentPosts.updateEmptyView();
  165. showSnackbarMessage("You have to be online to get details");
  166. }
  167. }
  168. @Override
  169. public void onDestroyView() {
  170. super.onDestroyView();
  171. }
  172. @Override
  173. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  174. inflater.inflate(R.menu.menu_channels_detail, menu);
  175. super.onCreateOptionsMenu(menu, inflater);
  176. }
  177. @Override
  178. public void onPrepareOptionsMenu(Menu menu) {
  179. changeOptionsMenu(menu);
  180. super.onPrepareOptionsMenu(menu);
  181. }
  182. private void changeOptionsMenu(Menu menu) {
  183. item = menu.findItem(R.id.action_subscribe_unsubscribe);
  184. AppObservable.bindFragment(this, service.getSubscriptions()).observeOn(AndroidSchedulers.mainThread())
  185. .subscribe(new Observer<PushbulletInfo>() {
  186. @Override
  187. public void onCompleted() {
  188. }
  189. @Override
  190. public void onError(Throwable e) {
  191. }
  192. @Override
  193. public void onNext(PushbulletInfo info) {
  194. ArrayList<PushbulletSubscriptions> activeSubscriptions = new ArrayList<PushbulletSubscriptions>();
  195. for (PushbulletSubscriptions subscriptions : info.getSubscriptions()) {
  196. if (subscriptions.isActive()) {
  197. Channel channel = subscriptions.getChannel();
  198. if (channel != null) {
  199. if (channel.getTag().equalsIgnoreCase(tag)) {
  200. iden = subscriptions.getIden();
  201. item.setTitle("Unsubscribe");
  202. break;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. });
  209. MenuItem shareItem = menu.findItem(R.id.action_share);
  210. boolean empty = ConstantMethods.isArrayListEmpty(contacts);
  211. shareItem.setVisible(!empty);
  212. SubMenu menu1 = shareItem.getSubMenu();
  213. if (!empty) {
  214. menu1.clear();
  215. int i = 0;
  216. for (Contacts contact : contacts) {
  217. menu1.add(1, 2488 + i, i, fakeDataProvider.getFakeData(getActivity(), contact.getName(), 2));
  218. i++;
  219. }
  220. }
  221. }
  222. @Override
  223. public boolean onOptionsItemSelected(MenuItem item) {
  224. return handleMenuItemClick(item);
  225. }
  226. private boolean handleMenuItemClick(MenuItem item) {
  227. int id = item.getItemId();
  228. if (id == R.id.action_subscribe_unsubscribe) {
  229. if (item.getTitle().toString().equalsIgnoreCase("Unsubscribe")) {
  230. final Snackbar make = Snackbar.make(channelDetailToolbar, "Unsubscribing from channel", Snackbar.LENGTH_LONG);
  231. final Observable<Long> observable = AppObservable.bindFragment(this, Observable.timer(2750, TimeUnit.MILLISECONDS));
  232. final Subscription subscribe = observable.subscribe(new Observer<Long>() {
  233. @Override
  234. public void onCompleted() {
  235. unsubscribe();
  236. }
  237. @Override
  238. public void onError(Throwable e) {
  239. }
  240. @Override
  241. public void onNext(Long aLong) {
  242. }
  243. });
  244. make.setAction("UNDO", new OnClickListener() {
  245. @Override
  246. public void onClick(View v) {
  247. subscribe.unsubscribe();
  248. }
  249. });
  250. make.show();
  251. // unsubscribe();
  252. } else {
  253. SubscribeRequest request = new SubscribeRequest();
  254. request.setChannel_tag(tag);
  255. AppObservable.bindFragment(this, service.subscribeToAChannel(request)).observeOn(AndroidSchedulers.mainThread())
  256. .subscribe(new Subscriber<PushbulletSubscriptions>() {
  257. @Override
  258. public void onCompleted() {
  259. String subscribed = "Subscribed";
  260. showSnackbarMessage(subscribed);
  261. getActivity().supportInvalidateOptionsMenu();
  262. }
  263. @Override
  264. public void onError(Throwable e) {
  265. String text = "Subscription failed " + e.getMessage();
  266. showSnackbarMessage(text);
  267. }
  268. @Override
  269. public void onNext(PushbulletSubscriptions subscriptions) {
  270. }
  271. });
  272. }
  273. } else if (id >= 2488 && item.getGroupId() == 1) {
  274. int index = item.getOrder();
  275. Contacts contact = contacts.get(index);
  276. PushRequest request = PushRequest.creteFromDefault();
  277. request.setTitle(info.getName());
  278. request.setEmail(contact.getEmail());
  279. request.setUrl("http://prashamtrivedi.github.io/ShareChannels/?tag=" + tag);
  280. service.sendInvite(request).subscribe(new Subscriber<Void>() {
  281. @Override
  282. public void onCompleted() {
  283. String text = "Subscription sent successfully";
  284. Snackbar.make(channelDetailToolbar, text, Snackbar.LENGTH_LONG).show();
  285. }
  286. @Override
  287. public void onError(Throwable e) {
  288. Log.d("Error", e.getMessage());
  289. }
  290. @Override
  291. public void onNext(Void aVoid) {
  292. }
  293. });
  294. return true;
  295. }
  296. return true;
  297. }
  298. private void unsubscribe() {
  299. AppObservable.bindFragment(this, service.deleteSubscription(iden)).observeOn(AndroidSchedulers.mainThread())
  300. .subscribe(new Subscriber<Void>() {
  301. @Override
  302. public void onCompleted() {
  303. String text = "Subscription Deleted";
  304. getActivity().supportInvalidateOptionsMenu();
  305. showSnackbarMessage(text);
  306. }
  307. @Override
  308. public void onError(Throwable e) {
  309. String text = "Subscription can not be deleted " + e.getMessage();
  310. showSnackbarMessage(text);
  311. }
  312. @Override
  313. public void onNext(Void aVoid) {
  314. }
  315. });
  316. }
  317. private void showSnackbarMessage(String message) {
  318. Snackbar.make(channelDetailToolbar, message, Snackbar.LENGTH_LONG).show();
  319. }
  320. private void getData() {
  321. Bundle arguments = getArguments();
  322. tag = arguments.getString("channel_tag");
  323. }
  324. LoadingRecyclerView recentPosts;
  325. TextView channelDescription;
  326. private FragmentChannelsDetailBinding binding;
  327. private ArrayList<Contacts> contacts;
  328. private TextView emptyView;
  329. private ProgressBar progress;
  330. private FakeDataProvider fakeDataProvider;
  331. }