1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.ui;
31
32import android.app.Activity;
33import android.content.Context;
34import android.databinding.DataBindingUtil;
35import android.os.Bundle;
36import android.util.Log;
37import android.view.LayoutInflater;
38import android.view.View;
39import android.view.ViewGroup;
40
41import java.util.ArrayList;
42import java.util.List;
43
44import de.timroes.android.listview.EnhancedListView;
45import eu.siacs.conversations.Config;
46import eu.siacs.conversations.R;
47import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
48import eu.siacs.conversations.entities.Conversation;
49import eu.siacs.conversations.ui.adapter.ConversationAdapter;
50import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
51
52public class ConversationsOverviewFragment extends XmppFragment {
53
54 private FragmentConversationsOverviewBinding binding;
55
56 private final List<Conversation> conversations = new ArrayList<>();
57 private ConversationAdapter conversationsAdapter;
58 private XmppActivity activity;
59
60 @Override
61 public void onAttach(Activity activity) {
62 super.onAttach(activity);
63 Log.d(Config.LOGTAG,"on attach");
64 if (activity instanceof XmppActivity) {
65 this.activity = (XmppActivity) activity;
66 } else {
67 throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
68 }
69 }
70
71 @Override
72 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
73 Log.d(Config.LOGTAG,"onCreateView");
74 this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
75 this.binding.fab.setOnClickListener((view)-> StartConversationActivity.launch(getActivity()));
76
77 this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
78 this.binding.list.setAdapter(this.conversationsAdapter);
79 this.binding.list.setSwipeDirection(EnhancedListView.SwipeDirection.BOTH);
80 this.binding.list.setOnItemClickListener((parent, view, position, id) -> {
81 Conversation conversation = this.conversations.get(position);
82 if (activity instanceof OnConversationSelected) {
83 ((OnConversationSelected) activity).onConversationSelected(conversation);
84 } else {
85 Log.w(ConversationsOverviewFragment.class.getCanonicalName(),"Activity does not implement OnConversationSelected");
86 }
87 });
88
89 return binding.getRoot();
90 }
91
92 @Override
93 void onBackendConnected() {
94 Log.d(Config.LOGTAG,"nice!");
95 refresh();
96 }
97
98 @Override
99 public void onStart() {
100 super.onStart();
101 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onStart()");
102 if (activity.xmppConnectionService != null) {
103 refresh();
104 }
105 }
106
107 @Override
108 public void onResume() {
109 super.onResume();
110 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onResume()");
111 }
112
113 @Override
114 void refresh() {
115 this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
116 this.conversationsAdapter.notifyDataSetChanged();
117 }
118}