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.content.Context;
33import android.databinding.DataBindingUtil;
34import android.os.Bundle;
35import android.util.Log;
36import android.view.LayoutInflater;
37import android.view.View;
38import android.view.ViewGroup;
39import android.widget.AdapterView;
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 ConversationsMainActivity activity;
59
60 @Override
61 public void onAttach(Context context) {
62 if (context instanceof ConversationsMainActivity) {
63 this.activity = (ConversationsMainActivity) context;
64 } else {
65 throw new IllegalStateException("Trying to attach fragment to activity that is not the ConversationActivity");
66 }
67 super.onAttach(context);
68 }
69
70 @Override
71 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
72 this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
73 this.binding.fab.setOnClickListener((view)-> StartConversationActivity.launch(getActivity()));
74
75 this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
76 this.binding.list.setAdapter(this.conversationsAdapter);
77 this.binding.list.setSwipeDirection(EnhancedListView.SwipeDirection.BOTH);
78 this.binding.list.setOnItemClickListener((parent, view, position, id) -> {
79 Conversation conversation = this.conversations.get(position);
80 if (activity instanceof OnConversationSelected) {
81 ((OnConversationSelected) activity).onConversationSelected(conversation);
82 } else {
83 Log.w(ConversationsOverviewFragment.class.getCanonicalName(),"Activity does not implement OnConversationSelected");
84 }
85 });
86
87 return binding.getRoot();
88 }
89
90 @Override
91 void onBackendConnected() {
92 Log.d(Config.LOGTAG,"nice!");
93 this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
94 this.conversationsAdapter.notifyDataSetChanged();
95 }
96}