Commit d9b4841d authored by Yusuke Iwaki's avatar Yusuke Iwaki

createChannel, createPrivateGroup, createDirectMessage

parent ce7d453d
......@@ -283,6 +283,27 @@ public class MethodCallHelper {
.onSuccessTask(CONVERT_TO_JSON_OBJECT);
}
public Task<Void> createChannel(final String name, final boolean readOnly) {
return call("createChannel", TIMEOUT_MS, () -> new JSONArray()
.put(name)
.put(new JSONArray())
.put(readOnly))
.onSuccessTask(task -> Task.forResult(null));
}
public Task<Void> createPrivateGroup(final String name, final boolean readOnly) {
return call("createPrivateGroup", TIMEOUT_MS, () -> new JSONArray()
.put(name)
.put(new JSONArray())
.put(readOnly))
.onSuccessTask(task -> Task.forResult(null));
}
public Task<Void> createDirectMessage(final String username) {
return call("createDirectMessage", TIMEOUT_MS, () -> new JSONArray().put(username))
.onSuccessTask(task -> Task.forResult(null));
}
/**
* send message.
*/
......
......@@ -5,6 +5,11 @@ import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;
import bolts.Task;
import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
import com.trello.rxlifecycle.components.support.RxAppCompatDialogFragment;
......@@ -12,6 +17,7 @@ import com.trello.rxlifecycle.components.support.RxAppCompatDialogFragment;
public abstract class AbstractAddRoomDialogFragment extends RxAppCompatDialogFragment {
protected RealmHelper realmHelper;
protected MethodCallHelper methodCall;
protected String hostname;
protected @LayoutRes abstract int getLayout();
......@@ -30,6 +36,7 @@ public abstract class AbstractAddRoomDialogFragment extends RxAppCompatDialogFra
protected void handleArgs(@NonNull Bundle args) {
String serverConfigId = args.getString("serverConfigId");
realmHelper = RealmStore.get(serverConfigId);
methodCall = new MethodCallHelper(getContext(), serverConfigId);
hostname = args.getString("hostname");
}
......@@ -38,4 +45,27 @@ public abstract class AbstractAddRoomDialogFragment extends RxAppCompatDialogFra
dialog.setContentView(getLayout());
onSetupDialog();
}
protected final void showOrHideWaitingView(boolean show) {
View waiting = getDialog().findViewById(R.id.waiting);
if (waiting != null) {
waiting.setVisibility(show ? View.VISIBLE : View.GONE);
}
}
protected abstract Task<Void> getMethodCallForSubmitAction();
protected final void createRoom() {
showOrHideWaitingView(true);
getMethodCallForSubmitAction().continueWith(task -> {
showOrHideWaitingView(false);
if (task.isFaulted()) {
Toast.makeText(getContext(), task.getError().getMessage(), Toast.LENGTH_SHORT).show();
} else {
dismiss();
}
return null;
});
}
}
......@@ -3,6 +3,7 @@ package chat.rocket.android.fragment.sidebar.dialog;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import bolts.Task;
import chat.rocket.android.R;
import chat.rocket.android.helper.TextUtils;
import com.jakewharton.rxbinding.view.RxView;
......@@ -26,13 +27,7 @@ public class AddChannelDialogFragment extends AbstractAddRoomDialogFragment {
.compose(bindToLifecycle())
.subscribe(RxView.enabled(buttonAddChannel));
buttonAddChannel.setOnClickListener(view -> {
TextView channelNameText = (TextView) getDialog().findViewById(R.id.editor_channel_name);
String channelName = channelNameText.getText().toString();
boolean isPrivate = isChecked(R.id.checkbox_private);
boolean isReadOnly = isChecked(R.id.checkbox_read_only);
createChannel(channelName, isPrivate, isReadOnly);
});
buttonAddChannel.setOnClickListener(view -> createRoom());
}
private boolean isChecked(int viewId) {
......@@ -40,7 +35,16 @@ public class AddChannelDialogFragment extends AbstractAddRoomDialogFragment {
return check.isChecked();
}
private void createChannel(String name, boolean isPrivate, boolean isReadOnly) {
@Override protected Task<Void> getMethodCallForSubmitAction() {
TextView channelNameText = (TextView) getDialog().findViewById(R.id.editor_channel_name);
String channelName = channelNameText.getText().toString();
boolean isPrivate = isChecked(R.id.checkbox_private);
boolean isReadOnly = isChecked(R.id.checkbox_read_only);
if (isPrivate) {
return methodCall.createPrivateGroup(channelName, isReadOnly);
} else {
return methodCall.createChannel(channelName, isReadOnly);
}
}
}
......@@ -2,6 +2,8 @@ package chat.rocket.android.fragment.sidebar.dialog;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import bolts.Task;
import chat.rocket.android.R;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.sidebar.dialog.SuggestUserAdapter;
......@@ -38,6 +40,12 @@ public class AddDirectMessageDialogFragment extends AbstractAddRoomDialogFragmen
.compose(bindToLifecycle())
.subscribe(RxView.enabled(buttonAddDirectMessage));
//buttonAddDirectMessage.setOnClick...
buttonAddDirectMessage.setOnClickListener(view -> createRoom());
}
@Override protected Task<Void> getMethodCallForSubmitAction() {
String username =
((TextView) getDialog().findViewById(R.id.editor_username)).getText().toString();
return methodCall.createDirectMessage(username);
}
}
......@@ -3,7 +3,6 @@ package chat.rocket.android.layouthelper.sidebar.dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmAutoCompleteAdapter;
......@@ -25,8 +24,7 @@ public class SuggestUserAdapter extends RealmAutoCompleteAdapter<User> {
@Override protected void onBindItemView(View itemView, User user) {
new UserRenderer(itemView.getContext(), user)
.statusColorInto((ImageView) itemView.findViewById(R.id.room_user_status))
.avatarInto((ImageView) itemView.findViewById(R.id.room_user_avatar), hostname)
.usernameInto((TextView) itemView.findViewById(R.id.room_user_name));
.avatarInto((ImageView) itemView.findViewById(R.id.room_user_avatar), hostname);
}
@Override protected void filterList(List<User> users, String text) {
......@@ -39,4 +37,8 @@ public class SuggestUserAdapter extends RealmAutoCompleteAdapter<User> {
}
}
}
@Override protected String getStringForSelectedItem(User user) {
return user.getUsername();
}
}
......@@ -7,6 +7,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmResults;
......@@ -28,7 +29,10 @@ public abstract class RealmAutoCompleteAdapter<T extends RealmObject> extends Ar
protected void filterList(List<T> items, String text) {
}
public class AutoCompleteFilter<T extends RealmObject> extends Filter {
/**
* Filter for completion.
*/
private class AutoCompleteFilter<T extends RealmObject> extends Filter {
private final RealmAutoCompleteAdapter<T> adapter;
private final RealmFilter<T> realmFilter;
......@@ -62,17 +66,23 @@ public abstract class RealmAutoCompleteAdapter<T extends RealmObject> extends Ar
adapter.addAll((List<T>) filterResults.values);
}
}
@Override public CharSequence convertResultToString(Object resultValue) {
return adapter.getStringForSelectedItem((T) resultValue);
}
}
private RealmHelper realmHelper;
private AutoCompleteFilter filter;
private final int textViewResourceId;
protected RealmAutoCompleteAdapter(Context context, int resource) {
super(context, resource);
}
/**
* NOTE
* getStringForSelectedItem(T model) is automatically set to the TextView(id=textViewResourceId).
*/
protected RealmAutoCompleteAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
this.textViewResourceId = textViewResourceId;
}
/*package*/ RealmAutoCompleteAdapter<T> initializeWith(RealmHelper realmHelper,
......@@ -87,11 +97,18 @@ public abstract class RealmAutoCompleteAdapter<T extends RealmObject> extends Ar
return filter;
}
@NonNull @Override public final View getView(int position, View convertView, ViewGroup parent) {
@NonNull
@Override public final View getView(int position, View convertView, @NonNull ViewGroup parent) {
View itemView = super.getView(position, convertView, parent);
onBindItemView(itemView, getItem(position));
T item = getItem(position);
TextView textView = (TextView) itemView.findViewById(textViewResourceId);
textView.setText(getStringForSelectedItem(item));
onBindItemView(itemView, item);
return itemView;
}
protected abstract void onBindItemView(View itemView, T model);
protected abstract String getStringForSelectedItem(T model);
}
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