Commit 319a52f9 authored by Christof Wolke's avatar Christof Wolke

get Comments and react on swipeRefresh

parent c4e93cac
......@@ -6,5 +6,5 @@ import java.io.Serializable;
* Superklasse für alle Listen Objekte
* Created by Christof on 01.09.2016.
*/
public class MyList implements Serializable {
public abstract class MyList implements Serializable {
}
......@@ -18,15 +18,15 @@ public class Forum extends RushObject implements Serializable {
@JsonField(name = "id")
private int forumId;
@RushList(classType = Forum.Comment.class)
@RushList(classType = Comment.class)
@JsonField(name = "comments")
private ArrayList<Forum.Comment>comments;
private ArrayList<Comment> comments;
public Forum(){
}
public Forum(int id, ArrayList<Forum.Comment> comments) {
public Forum(int id, ArrayList<Comment> comments) {
this.forumId = id;
this.comments = comments;
}
......@@ -39,11 +39,11 @@ public class Forum extends RushObject implements Serializable {
this.forumId = forumId;
}
public ArrayList<Forum.Comment> getComments() {
public ArrayList<Comment> getComments() {
return comments;
}
public void setComments(ArrayList<Forum.Comment> comments) {
public void setComments(ArrayList<Comment> comments) {
this.comments = comments;
}
......
......@@ -4,6 +4,7 @@ import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
......@@ -178,6 +179,7 @@ public class MainActivity extends BaseActivity {
@Override
public void run() {
setTitle(event.getName());
Log.d(TAG, event.getName());
map = event.getApps();
Collection<String> appnames = map.values();
updateData(appnames);
......
package de.uni_oldenburg.bidl.listfragment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import classes.MyList;
import classes.apps.forum.Comment;
import classes.apps.forum.CommentList;
import de.uni_oldenburg.bidl.R;
/**
* Created by Christof on 01.09.2016.
*/
public class CommentListAdapter {
public class CommentListAdapter extends ListAdapter {
private Context context;
private TextView username;
private TextView commentText;
private TextView time;
private CommentList commentList;
/**
* @param context Context der Activity
* @param commentList Liste die Dargestellt werden soll
*
* */
public CommentListAdapter(Context context, CommentList commentList) {
this.context = context;
this.commentList = commentList;
}
@Override
void setList(MyList list) {
this.commentList = (CommentList) list;
}
@Override
public int getCount() {
return this.commentList.getCommentList().size();
}
@Override
public Object getItem(int position) {
return this.commentList.getCommentList().get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
//current comment
Comment comment = this.commentList.getCommentList().get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(R.layout.comment_item, parent, false);
this.username = (TextView) item.findViewById(R.id.commentUsername);
this.username.setText(comment.getUsername());
this.commentText = (TextView) item.findViewById(R.id.commentText);
this.commentText.setText(comment.getText());
this.time = (TextView) item.findViewById(R.id.commentTime);
this.time.setText(comment.getTimestamp());
return item;
}
}
package de.uni_oldenburg.bidl.listfragment;
import android.widget.ListView;
/**
* Kommunikator Interface, damit Listen-Events gefeuert werden können
* Created by Christof on 01.09.2016.
*/
public interface ListCommunicator {
/**
* Wird gerufen, wenn Fragment aktualisiert wird
*/
void updateFragList();
/**
* Wird gerufen, wenn aus einer Liste ein Item "herausgeswipet" wird
* @param listView ListView, in denen die Items sind
* @param reverseSortedPositions Positionen der Items, die herausgeswipt worden sind
*/
void onSwipeDismiss(ListView listView, int[] reverseSortedPositions);
}
package de.uni_oldenburg.bidl.listfragment;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import classes.MyList;
import classes.apps.forum.CommentList;
import de.uni_oldenburg.bidl.R;
/**
* Created by Christof on 01.09.2016.
*/
public class SuperListFragment extends ListFragment implements SwipeRefreshLayout.OnRefreshListener {
/* TAG Kostante für das Logging */
private static final String TAG = SuperListFragment.class.getSimpleName();
//Verchiedene Typen von ListFragmenten
public static final String TYPE_COMMENTLIST = "commentlist";
//Optionen
private String type;
private boolean withSwipe;
private boolean isRefreshable;
private SwipeRefreshLayout swipeRefreshLayout;
private MyList list;
private ListCommunicator communicator;
public SuperListFragment() {
}
public static SuperListFragment newInstance(String type, MyList list, boolean withSwipe, boolean isRefreshable) {
SuperListFragment fragment = new SuperListFragment();
Bundle args = new Bundle();
args.putString("listType", type);
args.putSerializable("list",list);
args.putBoolean("withSwipe", withSwipe);
args.putBoolean("isRefreshable", isRefreshable);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
if (getArguments() != null){
this.type = getArguments().getString("listType");
this.list = (MyList) getArguments().getSerializable("list");
this.withSwipe = getArguments().getBoolean("withSwipe", false);
this.isRefreshable = getArguments().getBoolean("isRefreshable", false);
}
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (withSwipe){
//ToDo: das Löschen von Daten durch swipen realisieren.
}
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipelayout);
if (isRefreshable){
swipeRefreshLayout.setOnRefreshListener(this);
}else {
swipeRefreshLayout.setEnabled(false);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bidl_list, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
if (savedInstanceState != null){
this.type = savedInstanceState.getString("listType");
this.list = (MyList) savedInstanceState.getSerializable("list");
if (getActivity() instanceof ListCommunicator)
this.communicator = (ListCommunicator) getActivity();
}
setList();
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof ListCommunicator)
this.communicator = (ListCommunicator) activity;
}
@Override
public void onDetach() {
super.onDetach();
this.communicator = null;
}
@Override
public void onRefresh() {
communicator.updateFragList();
}
/**
* Zeigt Refresh Animation an oder beendet sie
* @param show true: wird angezeigt; false: wird nicht angezeigt
*/
public void showRefreshAnimation(final boolean show){
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(show);
}
});
//swipeRefreshLayout.setRefreshing(show);
}
/**
* setzt den richtigen Adapter
*/
private void setList() {
if (this.list != null){
switch (this.type){
case TYPE_COMMENTLIST:
setListAdapter(new CommentListAdapter(getActivity(), (CommentList) list));
break;
}
}
}
public void update(final MyList newList){
this.list = newList;
if(this.getListAdapter() != null){
final ListAdapter adapter = (ListAdapter) this.getListAdapter();
if (getActivity() != null){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Update request received, setting new list and notifying the adapter");
adapter.setList(newList);
adapter.notifyDataSetChanged();
}
});
} else {
if (getActivity() != null){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setList();
}
});
}
}
}
}
}
......@@ -2,31 +2,54 @@ package de.uni_oldenburg.bidl.member_view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import classes.Event;
import classes.apps.forum.Comment;
import classes.apps.forum.CommentList;
import classes.apps.forum.Forum;
import de.uni_oldenburg.bidl.R;
import de.uni_oldenburg.bidl.listfragment.CommentListAdapter;
import de.uni_oldenburg.bidl.listfragment.ListCommunicator;
import de.uni_oldenburg.bidl.listfragment.SuperListFragment;
import services.db.DBHelper;
import services.servercommunication.NetworkService;
public class ForumActivity extends AppCompatActivity {
public class ForumActivity extends AppCompatActivity implements ListCommunicator, View.OnTouchListener {
/** Tag for Logging **/
private static final String TAG = ForumActivity.class.getSimpleName();
private Forum forum;
private Event event;
private CommentList comments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forum);
setTitle(getIntent().getStringExtra("appName"));
Event event = DBHelper.getEvent();
event = DBHelper.getEvent();
getForumObjectFromServer(event.getEventId());
}
private void getForumObjectFromServer(int eventId) {
NetworkService.forum(eventId, new NetworkService.RequestForumHandler() {
@Override
......@@ -36,7 +59,7 @@ public class ForumActivity extends AppCompatActivity {
ForumActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
initGui();
addCommentFragment();
}
});
}
......@@ -45,10 +68,14 @@ public class ForumActivity extends AppCompatActivity {
});
}
public void initGui() {
public void addCommentFragment() {
try {
if (forum.getComments() != null){
FrameLayout commentContainer = (FrameLayout) findViewById(R.id.commentContainer);
commentContainer.removeAllViews();
SuperListFragment commentFragment = SuperListFragment.newInstance(SuperListFragment.TYPE_COMMENTLIST, new CommentList(forum.getComments()), false, true);
getSupportFragmentManager().beginTransaction().add(R.id.commentContainer, commentFragment).commit();
}
}catch (NullPointerException e){
e.printStackTrace();
......@@ -59,4 +86,45 @@ public class ForumActivity extends AppCompatActivity {
public void setForum(Forum forum) {
this.forum = forum;
}
@Override
public void updateFragList() {
final SuperListFragment superListFragment = (SuperListFragment) getSupportFragmentManager().findFragmentById(R.id.commentContainer);
NetworkService.forum(event.getEventId(), new NetworkService.RequestForumHandler() {
@Override
public void done(Forum forum) {
if (forum.getComments() != null){
comments = new CommentList(forum.getComments());
setCommentList(comments);
Log.d(TAG, "REFRESHING: "+comments.toString());
superListFragment.update(comments);
superListFragment.showRefreshAnimation(false);
}
}
});
}
private void setCommentList(CommentList comments) {
this.comments = comments;
}
@Override
public void onSwipeDismiss(ListView listView, int[] reverseSortedPositions) {
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float x = motionEvent.getX();
float y = motionEvent.getY();
int eventaction = motionEvent.getAction();
switch (eventaction){
case MotionEvent.ACTION_DOWN:
// updateFragList();
}
return true;
}
}
......@@ -9,4 +9,9 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.uni_oldenburg.bidl.member_view.ForumActivity">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/commentContainer"></FrameLayout>
</RelativeLayout>
......@@ -7,24 +7,28 @@
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"
android:id="@+id/commentUsername"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message"
android:id="@+id/commentText"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time"
android:id="@+id/commentTime"
android:layout_margin="10dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment