refactoring. move functionality from activity to fragment

This commit is contained in:
Daniel Gultsch 2018-02-20 08:21:07 +01:00
parent d55a3842d3
commit c17f902be2
3 changed files with 186 additions and 156 deletions

View file

@ -59,8 +59,8 @@ import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdat
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.ui.service.EmojiService;
import eu.siacs.conversations.ui.util.SendButtonAction;
import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
@ -556,29 +556,12 @@ public class ConversationActivity extends XmppActivity
return;
}
}
final ConversationFragment.SendButtonAction action;
switch (attachmentChoice) {
case ATTACHMENT_CHOICE_LOCATION:
action = ConversationFragment.SendButtonAction.SEND_LOCATION;
break;
case ATTACHMENT_CHOICE_RECORD_VOICE:
action = ConversationFragment.SendButtonAction.RECORD_VOICE;
break;
case ATTACHMENT_CHOICE_RECORD_VIDEO:
action = ConversationFragment.SendButtonAction.RECORD_VIDEO;
break;
case ATTACHMENT_CHOICE_TAKE_PHOTO:
action = ConversationFragment.SendButtonAction.TAKE_PHOTO;
break;
case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
action = ConversationFragment.SendButtonAction.CHOOSE_PICTURE;
break;
default:
action = null;
break;
}
if (action != null) {
getPreferences().edit().putString(RECENTLY_USED_QUICK_ACTION,action.toString()).apply();
try {
getPreferences().edit()
.putString(RECENTLY_USED_QUICK_ACTION, SendButtonAction.of(attachmentChoice).toString())
.apply();
} catch (IllegalArgumentException e) {
//just do not save
}
final Conversation conversation = getSelectedConversation();
final int encryption = conversation.getNextEncryption();
@ -693,22 +676,12 @@ public class ConversationActivity extends XmppActivity
handleEncryptionSelection(item);
break;
case R.id.attach_choose_picture:
attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break;
case R.id.attach_take_picture:
attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case R.id.attach_record_video:
attachFile(ATTACHMENT_CHOICE_RECORD_VIDEO);
break;
case R.id.attach_choose_file:
attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
break;
case R.id.attach_record_voice:
attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case R.id.attach_location:
attachFile(ATTACHMENT_CHOICE_LOCATION);
handleAttachmentSelection(item);
break;
case R.id.action_archive:
this.endConversation(getSelectedConversation());
@ -783,8 +756,7 @@ public class ConversationActivity extends XmppActivity
final CheckBox endConversationCheckBox = dialogView.findViewById(R.id.end_conversation_checkbox);
builder.setView(dialogView);
builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.delete_messages),
(dialog, which) -> {
builder.setPositiveButton(getString(R.string.delete_messages), (dialog, which) -> {
ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
if (endConversationCheckBox.isChecked()) {
endConversation(conversation);
@ -796,6 +768,29 @@ public class ConversationActivity extends XmppActivity
builder.create().show();
}
private void handleAttachmentSelection(MenuItem item) {
switch (item.getItemId()) {
case R.id.attach_choose_picture:
attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break;
case R.id.attach_take_picture:
attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case R.id.attach_record_video:
attachFile(ATTACHMENT_CHOICE_RECORD_VIDEO);
break;
case R.id.attach_choose_file:
attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
break;
case R.id.attach_record_voice:
attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case R.id.attach_location:
attachFile(ATTACHMENT_CHOICE_LOCATION);
break;
}
}
private void handleEncryptionSelection(MenuItem item) {
Conversation conversation = getSelectedConversation();
if (conversation == null) {
@ -839,11 +834,7 @@ public class ConversationActivity extends XmppActivity
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.disable_notifications);
final int[] durations = getResources().getIntArray(R.array.mute_options_durations);
builder.setItems(R.array.mute_options_descriptions,
new OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
builder.setItems(R.array.mute_options_descriptions, (dialog, which) -> {
final long till;
if (durations[which] == -1) {
till = Long.MAX_VALUE;
@ -855,7 +846,6 @@ public class ConversationActivity extends XmppActivity
updateConversationList();
ConversationActivity.this.mConversationFragment.updateMessages();
invalidateOptionsMenu();
}
});
builder.create().show();
}
@ -1472,22 +1462,12 @@ public class ConversationActivity extends XmppActivity
@Override
public void inform(final String text) {
hidePrepareFileToast(prepareFileToast);
runOnUiThread(new Runnable() {
@Override
public void run() {
replaceToast(text);
}
});
runOnUiThread(() -> replaceToast(text));
}
@Override
public void success(Message message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
hideToast();
}
});
runOnUiThread(() -> hideToast());
hidePrepareFileToast(prepareFileToast);
xmppConnectionService.sendMessage(message);
}
@ -1495,12 +1475,7 @@ public class ConversationActivity extends XmppActivity
@Override
public void error(final int errorCode, Message message) {
hidePrepareFileToast(prepareFileToast);
runOnUiThread(new Runnable() {
@Override
public void run() {
replaceToast(getString(errorCode));
}
});
runOnUiThread(() -> replaceToast(getString(errorCode)));
}
@ -1551,13 +1526,7 @@ public class ConversationActivity extends XmppActivity
private void hidePrepareFileToast(final Toast prepareFileToast) {
if (prepareFileToast != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
prepareFileToast.cancel();
}
});
runOnUiThread(() -> prepareFileToast.cancel());
}
}
@ -1716,12 +1685,7 @@ public class ConversationActivity extends XmppActivity
@Override
public void onShowErrorToast(final int resId) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
}
});
runOnUiThread(() -> Toast.makeText(ConversationActivity.this, resId, Toast.LENGTH_SHORT).show());
}
public boolean highlightSelectedConversations() {

View file

@ -71,8 +71,7 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected;
import eu.siacs.conversations.ui.XmppActivity.OnValueEdited;
import eu.siacs.conversations.ui.adapter.MessageAdapter;
import eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureClicked;
import eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureLongClicked;
import eu.siacs.conversations.ui.util.SendButtonAction;
import eu.siacs.conversations.ui.widget.EditMessage;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.NickValidityChecker;
@ -82,6 +81,12 @@ import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.jid.Jid;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_LOCATION;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VIDEO;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO;
public class ConversationFragment extends Fragment implements EditMessage.KeyboardListener {
final protected List<Message> messageList = new ArrayList<>();
@ -357,19 +362,11 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
SendButtonAction action = (SendButtonAction) tag;
switch (action) {
case TAKE_PHOTO:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case RECORD_VIDEO:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_RECORD_VIDEO);
break;
case SEND_LOCATION:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_LOCATION);
break;
case RECORD_VOICE:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case CHOOSE_PICTURE:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE);
activity.attachFile(action.toChoice());
break;
case CANCEL:
if (conversation != null) {
@ -1674,8 +1671,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
}
@Override
public void onActivityResult(int requestCode, int resultCode,
final Intent data) {
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
activity.getSelectedConversation().getAccount().getPgpDecryptionService().continueDecryption(data);
@ -1695,16 +1691,4 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
}
}
enum SendButtonAction {
TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE, RECORD_VIDEO;
public static SendButtonAction valueOfOrDefault(String setting, SendButtonAction text) {
try {
return valueOf(setting);
} catch (IllegalArgumentException e) {
return TEXT;
}
}
}
}

View file

@ -0,0 +1,82 @@
/*
* Copyright (c) 2018, Daniel Gultsch All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package eu.siacs.conversations.ui.util;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_LOCATION;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VIDEO;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO;
public enum SendButtonAction {
TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE, RECORD_VIDEO;
public static SendButtonAction valueOfOrDefault(String setting, SendButtonAction text) {
try {
return valueOf(setting);
} catch (IllegalArgumentException e) {
return TEXT;
}
}
public static SendButtonAction of(int attachmentChoice) {
switch (attachmentChoice) {
case ATTACHMENT_CHOICE_LOCATION:
return SEND_LOCATION;
case ATTACHMENT_CHOICE_RECORD_VOICE:
return RECORD_VOICE;
case ATTACHMENT_CHOICE_RECORD_VIDEO:
return RECORD_VIDEO;
case ATTACHMENT_CHOICE_TAKE_PHOTO:
return TAKE_PHOTO;
case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
return CHOOSE_PICTURE;
default:
throw new IllegalArgumentException("Not a known attachment choice");
}
}
public int toChoice() {
switch (this) {
case TAKE_PHOTO:
return ATTACHMENT_CHOICE_TAKE_PHOTO;
case SEND_LOCATION:
return ATTACHMENT_CHOICE_LOCATION;
case RECORD_VOICE:
return ATTACHMENT_CHOICE_RECORD_VOICE;
case CHOOSE_PICTURE:
return ATTACHMENT_CHOICE_CHOOSE_IMAGE;
case RECORD_VIDEO:
return ATTACHMENT_CHOICE_RECORD_VIDEO;
default:
return 0;
}
}
}