Skip to content

Commit

Permalink
refactored code to user ArrayAdapters methods instead of immutable Ar…
Browse files Browse the repository at this point in the history
…rayList
  • Loading branch information
Nexen23 committed Aug 25, 2015
1 parent 9ab2920 commit 9bef9bc
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 39 deletions.
11 changes: 9 additions & 2 deletions app/src/main/java/alex/imhere/activity/model/AbstractModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import alex.imhere.fragment.view.AbstractView;

abstract public class AbstractModel {
static public final int UNIVERSAL_NOTIFICATION = 0;
static protected final int LAST_NOTIFICATION_FLAG = 0;
// TODO: 25.08.2015 refactor Observer/Observable
private Handler uiHandler;
ArrayList<AbstractView> listeners = new ArrayList<>();

Expand All @@ -19,13 +22,17 @@ public void addEventListener(@NonNull AbstractView view) {
listeners.add(view);
}

public void notifyDataChanged() {
public void notifyDataChanged(int notification) {
notifyDataChanged(notification, null);
}

public void notifyDataChanged(final int notification, final Object data) {
for (int i = 0; i < listeners.size(); i++) {
final AbstractView view = listeners.get(i);
uiHandler.post(new Runnable() {
@Override
public void run() {
view.onDataUpdate();
view.onDataUpdate(notification, data);
}
});
}
Expand Down
34 changes: 29 additions & 5 deletions app/src/main/java/alex/imhere/activity/model/ImhereModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
import alex.imhere.layer.server.ServerAPI;
import alex.imhere.layer.server.Session;
import alex.imhere.service.ChannelService;
import alex.imhere.util.ListObservable;
import alex.imhere.util.TemporarySet;

public class ImhereModel extends AbstractModel {
// TODO: 25.08.2015 make smart flags without chances of collision
static public final int ADD_USER_NOTIFICATION = 1 + LAST_NOTIFICATION_FLAG;
static public final int REMOVE_USER_NOTIFICATION = 2 + LAST_NOTIFICATION_FLAG;
static public final int CLEAR_USER_NOTIFICATION = 3 + LAST_NOTIFICATION_FLAG;
static public final int LOGIN_NOTIFICATION = 4 + LAST_NOTIFICATION_FLAG;
//static protected final int LAST_NOTIFICATION_FLAG = 5;

//TODO: exerpt methods to Service! This is too complex for Model in MVC
private ServerAPI api = new ServerAPI();
private ChannelService channel;
Expand All @@ -29,7 +37,23 @@ public class ImhereModel extends AbstractModel {
private Observer onlineUsersObserver = new Observer() {
@Override
public void update(Observable observable, Object data) {
notifyDataChanged();
try {
ListObservable.NotificationData notificationData = (ListObservable.NotificationData) data;

if (notificationData.notification == ListObservable.Notification.ADD) {
notifyDataChanged(ADD_USER_NOTIFICATION, notificationData.data);
}
if (notificationData.notification == ListObservable.Notification.REMOVE) {
notifyDataChanged(REMOVE_USER_NOTIFICATION, notificationData.data);
}
if (notificationData.notification == ListObservable.Notification.CLEAR) {
notifyDataChanged(UNIVERSAL_NOTIFICATION, null);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: 25.08.2015 log
throw e;
}
}
};

Expand All @@ -46,14 +70,14 @@ public ImhereModel(@NonNull Handler uiHandler, @NonNull String udid) {
@Override
public void onUserOnline(Session session) {
if ( isCurrentSessionAlive() && onlineUsersSet.add(session, session.getAliveTo()) ) {
notifyDataChanged();
//notifyDataChanged(ADD_USER_NOTIFICATION, session);
}
}

@Override
public void onUserOffline(Session session) {
if ( isCurrentSessionAlive() && onlineUsersSet.remove(session) ) {
notifyDataChanged();
//notifyDataChanged(REMOVE_USER_NOTIFICATION, session);
}
}
};
Expand Down Expand Up @@ -104,7 +128,7 @@ public void run() {
timer.schedule(timerTask, currentSession.getAliveTo().toDate());

this.currentSession = currentSession;
notifyDataChanged();
notifyDataChanged(LOGIN_NOTIFICATION);

return currentSession;
}
Expand All @@ -121,7 +145,7 @@ public void cancelCurrentSession() {
currentSession = null;
//onlineUsersSet.clear();

notifyDataChanged();
notifyDataChanged(CLEAR_USER_NOTIFICATION);
}
}
}
28 changes: 20 additions & 8 deletions app/src/main/java/alex/imhere/adapter/UsersAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,34 @@
public class UsersAdapter extends ArrayAdapter<Session> {
private final int resourceId;
private Context context;
/*@BindColor(R.color.user_born) */int userBornColor;
/*@BindColor(R.color.user_alive) */int userAliveColor;
/*@BindColor(R.color.user_dead) */int userDeadColor;
List<Session> items;

public UsersAdapter(Activity activity, int item_user, List<Session> items) {
super(activity, item_user, items);
this.items = items;

this.context = activity;
this.resourceId = item_user;

/*ButterKnife.bind(activity);*/
userBornColor = activity.getResources().getColor(R.color.user_born);
userAliveColor = activity.getResources().getColor(R.color.user_alive);
userDeadColor = activity.getResources().getColor(R.color.user_dead);
}

@Override
public void add(Session insertingSession) {
if (getCount() == 0) {
super.add(insertingSession);
} else {
boolean notInserted = true;
for( int i = 0; i < getCount() && notInserted; i++ ) {
Session session = getItem(i);
if (session.getRestLifetime().getMillis() >= insertingSession.getRestLifetime().getMillis()) {
insert(insertingSession, i);
notInserted = false;
}
}
if (notInserted) {
super.add(insertingSession);
}
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/alex/imhere/fragment/StatusFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void onDetach() {
}

@Override
public void onDataUpdate() {
public void onDataUpdate(final int notification, final Object data) {
boolean currentSessionIsAlive = model.isCurrentSessionAlive(),
statusChanged = currentSessionIsAlive != currentSessionWasAlive;
updateStatus(statusChanged, currentSessionIsAlive);
Expand Down
36 changes: 28 additions & 8 deletions app/src/main/java/alex/imhere/fragment/UsersFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,47 @@ public void onActivityCreated(Bundle savedInstanceState) {
setListAdapter(usersAdapter);
}

@Override
public void onResume() {
super.onResume();
onDataUpdate(AbstractModel.UNIVERSAL_NOTIFICATION, null);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
uiHandler = new Handler();
usersAdapter = new UsersAdapter(getActivity(), R.layout.item_user, new ArrayList<Session>());

updatingViewTimer = new UpdatingViewTimer(uiHandler, this);
updatingViewTimer.start();

return inflater.inflate(R.layout.fragment_users, container);
}

@Override
public void onResume() {
super.onResume();
onDataUpdate();
}

@Override
public void onDataUpdate() {
public void onDataUpdate(final int notification, final Object data) {
boolean currentSessionIsAlive = model.isCurrentSessionAlive(),
statusChanged = currentSessionIsAlive != currentSessionWasAlive;

Session session = (Session) data;
switch (notification) {
case ImhereModel.ADD_USER_NOTIFICATION :
usersAdapter.add(session);
break;

case ImhereModel.REMOVE_USER_NOTIFICATION :
usersAdapter.remove(session);
break;

case ImhereModel.CLEAR_USER_NOTIFICATION :
usersAdapter.clear();
break;

/*default :
usersAdapter.notifyDataSetChanged();
break;*/
}
usersAdapter.notifyDataSetChanged();

currentSessionWasAlive = currentSessionIsAlive;
Expand All @@ -71,7 +92,6 @@ public void setModel(AbstractModel abstractModel) {
model = (ImhereModel) abstractModel;
model.addEventListener(this);

readOnlyUsers = model.getOnlineUsersSet();
usersAdapter = new UsersAdapter(getActivity(), R.layout.item_user, readOnlyUsers);
//readOnlyUsers = model.getOnlineUsersSet();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package alex.imhere.fragment.view;

import android.support.annotation.NonNull;

import alex.imhere.activity.model.AbstractModel;

public interface AbstractView {
void setModel(AbstractModel abstractModel);
void onDataUpdate();
void onDataUpdate(final int notification, @NonNull final Object data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Timer;
import java.util.TimerTask;

import alex.imhere.activity.model.AbstractModel;

public class UpdatingViewTimer {
private AbstractView view;
private Handler uiHandler;
Expand All @@ -17,7 +19,7 @@ public void run() {
uiHandler.post( new Runnable() {
@Override
public void run() {
view.onDataUpdate();
view.onDataUpdate(AbstractModel.UNIVERSAL_NOTIFICATION, null);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected void onDraw(Canvas canvas) {
canvas.drawPath(shapeBorderPath, borderPaint);

// drawing border of real Layout for child Views
canvas.drawRect(sidesGap, 0f, (width - 1) - sidesGap, height - 1, borderPaint);
//canvas.drawRect(sidesGap, 0f, (width - 1) - sidesGap, height - 1, borderPaint);

super.onDraw(canvas);
}
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/alex/imhere/util/ListObservable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package alex.imhere.util;


import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.Observable;

public class ListObservable<T> extends Observable {
public enum Notification {
UNIVERSAL,

CLEAR,

ADD,
REMOVE
};

protected void notifyCollectionChanged(@NonNull Notification notification, @Nullable T data) {
setChanged();
notifyObservers(new NotificationData(notification, data));
}

public class NotificationData {
public Notification notification;
public T data;

public NotificationData(Notification notification, T data) {
this.notification = notification;
this.data = data;
}
}
}
14 changes: 4 additions & 10 deletions app/src/main/java/alex/imhere/util/TemporarySet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
import java.util.SortedSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;

public class TemporarySet<T> extends Observable {
public class TemporarySet<T> extends ListObservable {
// TODO: 18.08.2015 make sure it's thread-safe implementation
protected SortedSet<TemporaryElement<T>> sortedElementsSet = new TreeSet<>();
protected List<T> list = new ArrayList<>();
Expand Down Expand Up @@ -44,7 +43,7 @@ private synchronized void _clear() {
list.clear();
sortedElementsSet.clear();

notifyCollectionChanged();
notifyCollectionChanged(Notification.CLEAR, null);
}


Expand All @@ -64,7 +63,7 @@ private synchronized boolean _add(TemporaryElement<T> isertingElement) {
openNextDeath();
}

notifyCollectionChanged();
notifyCollectionChanged(Notification.ADD, isertingElement.object);
}

return wasAdded;
Expand All @@ -82,7 +81,7 @@ private synchronized boolean _remove(TemporaryElement<T> deletingElement) {
openNextDeath();
}

notifyCollectionChanged();
notifyCollectionChanged(Notification.REMOVE, deletingElement.object);
}

return wasRemoved;
Expand Down Expand Up @@ -116,9 +115,4 @@ private synchronized void cancelNextDeath() {
nextElementToDie = null;
timerTask = null;
}

private void notifyCollectionChanged() {
setChanged();
notifyObservers();
}
}
2 changes: 0 additions & 2 deletions app/src/main/res/layout/item_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
android:weightSum="1"
android:padding="@dimen/padding_default">


<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Expand All @@ -35,7 +34,6 @@
android:textAlignment="center"
android:gravity="center_horizontal"
android:singleLine="true"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 9bef9bc

Please sign in to comment.