Skip to content

Commit 50657a5

Browse files
committed
Refactor: convert Attachment from java to kotlin
1 parent e9c4952 commit 50657a5

File tree

6 files changed

+217
-237
lines changed

6 files changed

+217
-237
lines changed

legacy/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,13 +1985,13 @@ public void addAttachmentView(final Attachment attachment) {
19851985
attachmentsView.setVisibility(View.VISIBLE);
19861986

19871987
View view = getLayoutInflater().inflate(R.layout.message_compose_attachment, attachmentsView, false);
1988-
attachmentViews.put(attachment.uri, view);
1988+
attachmentViews.put(attachment.getUri(), view);
19891989

19901990
View deleteButton = view.findViewById(R.id.attachment_delete);
19911991
deleteButton.setOnClickListener(new OnClickListener() {
19921992
@Override
19931993
public void onClick(View view) {
1994-
attachmentPresenter.onClickRemoveAttachment(attachment.uri);
1994+
attachmentPresenter.onClickRemoveAttachment(attachment.getUri());
19951995
}
19961996
});
19971997

@@ -2001,26 +2001,26 @@ public void onClick(View view) {
20012001

20022002
@Override
20032003
public void updateAttachmentView(Attachment attachment) {
2004-
View view = attachmentViews.get(attachment.uri);
2004+
View view = attachmentViews.get(attachment.getUri());
20052005
if (view == null) {
20062006
throw new IllegalArgumentException();
20072007
}
20082008

20092009
MaterialTextView nameView = view.findViewById(R.id.attachment_name);
2010-
boolean hasMetadata = (attachment.state != Attachment.LoadingState.URI_ONLY);
2010+
boolean hasMetadata = (attachment.getState() != Attachment.LoadingState.URI_ONLY);
20112011
if (hasMetadata) {
2012-
nameView.setText(attachment.name);
2012+
nameView.setText(attachment.getName());
20132013
} else {
20142014
nameView.setText(R.string.loading_attachment);
20152015
}
20162016

2017-
if (attachment.size != null && attachment.size >= 0) {
2017+
if (attachment.getSize() != null && attachment.getSize() >= 0) {
20182018
MaterialTextView sizeView = view.findViewById(R.id.attachment_size);
2019-
sizeView.setText(sizeFormatter.formatSize(attachment.size));
2019+
sizeView.setText(sizeFormatter.formatSize(attachment.getSize()));
20202020
}
20212021

20222022
View progressBar = view.findViewById(R.id.progressBar);
2023-
boolean isLoadingComplete = (attachment.state == Attachment.LoadingState.COMPLETE);
2023+
boolean isLoadingComplete = (attachment.getState() == Attachment.LoadingState.COMPLETE);
20242024
if (isLoadingComplete) {
20252025
if (attachment.isSupportedImage()) {
20262026
ImageView attachmentTypeView = view.findViewById(R.id.attachment_type);
@@ -2029,7 +2029,7 @@ public void updateAttachmentView(Attachment attachment) {
20292029
ImageView preview = view.findViewById(R.id.attachment_preview);
20302030
preview.setVisibility(View.VISIBLE);
20312031
Glide.with(MessageCompose.this)
2032-
.load(new File(attachment.filename))
2032+
.load(new File(attachment.getFileName()))
20332033
.centerCrop()
20342034
.diskCacheStrategy(DiskCacheStrategy.NONE)
20352035
.into(preview);
@@ -2042,9 +2042,9 @@ public void updateAttachmentView(Attachment attachment) {
20422042

20432043
@Override
20442044
public void removeAttachmentView(Attachment attachment) {
2045-
View view = attachmentViews.get(attachment.uri);
2045+
View view = attachmentViews.get(attachment.getUri());
20462046
attachmentsView.removeView(view);
2047-
attachmentViews.remove(attachment.uri);
2047+
attachmentViews.remove(attachment.getUri());
20482048

20492049
if (attachmentViews.isEmpty()) {
20502050
attachmentsView.setVisibility(View.GONE);

legacy/ui/legacy/src/main/java/com/fsck/k9/activity/compose/AttachmentPresenter.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public void onRestoreInstanceState(Bundle savedInstanceState) {
8080
ArrayList<Attachment> attachmentList = savedInstanceState.getParcelableArrayList(STATE_KEY_ATTACHMENTS);
8181
// noinspection ConstantConditions, we know this is set in onSaveInstanceState
8282
for (Attachment attachment : attachmentList) {
83-
attachments.put(attachment.uri, attachment);
83+
attachments.put(attachment.getUri(), attachment);
8484
attachmentMvpView.addAttachmentView(attachment);
8585

86-
if (attachment.state == LoadingState.URI_ONLY) {
86+
if (attachment.getState() == LoadingState.URI_ONLY) {
8787
initAttachmentInfoLoader(attachment);
88-
} else if (attachment.state == LoadingState.METADATA) {
88+
} else if (attachment.getState() == LoadingState.METADATA) {
8989
initAttachmentContentLoader(attachment);
9090
}
9191
}
@@ -107,7 +107,7 @@ public boolean checkOkForSendingOrDraftSaving() {
107107

108108
private boolean hasLoadingAttachments() {
109109
for (Attachment attachment : attachments.values()) {
110-
Loader loader = loaderManager.getLoader(attachment.loaderId);
110+
Loader loader = loaderManager.getLoader(attachment.getLoaderId());
111111
if (loader != null && loader.isStarted()) {
112112
return true;
113113
}
@@ -183,11 +183,12 @@ private void addInlineAttachment(AttachmentViewInfo attachmentViewInfo) {
183183
attachment = attachment.deriveWithMetadataLoaded(
184184
attachmentViewInfo.mimeType, attachmentViewInfo.displayName, attachmentViewInfo.size);
185185

186-
inlineAttachments.put(attachment.uri, new InlineAttachment(attachmentViewInfo.part.getContentId(), attachment));
186+
inlineAttachments.put(
187+
attachment.getUri(), new InlineAttachment(attachmentViewInfo.part.getContentId(), attachment));
187188

188189
Bundle bundle = new Bundle();
189-
bundle.putParcelable(LOADER_ARG_ATTACHMENT, attachment.uri);
190-
loaderManager.initLoader(attachment.loaderId, bundle, mInlineAttachmentContentLoaderCallback);
190+
bundle.putParcelable(LOADER_ARG_ATTACHMENT, attachment.getUri());
191+
loaderManager.initLoader(attachment.getLoaderId(), bundle, mInlineAttachmentContentLoaderCallback);
191192
}
192193

193194
private void addInternalAttachment(Uri uri, String contentType, boolean allowMessageType) {
@@ -243,37 +244,37 @@ public void processMessageToForwardAsAttachment(MessageViewInfo messageViewInfo)
243244
}
244245

245246
private void addAttachmentAndStartLoader(Attachment attachment) {
246-
attachments.put(attachment.uri, attachment);
247+
attachments.put(attachment.getUri(), attachment);
247248
listener.onAttachmentAdded();
248249
attachmentMvpView.addAttachmentView(attachment);
249250

250-
if (attachment.state == LoadingState.URI_ONLY) {
251+
if (attachment.getState() == LoadingState.URI_ONLY) {
251252
initAttachmentInfoLoader(attachment);
252-
} else if (attachment.state == LoadingState.METADATA) {
253+
} else if (attachment.getState() == LoadingState.METADATA) {
253254
initAttachmentContentLoader(attachment);
254255
} else {
255256
throw new IllegalStateException("Attachment can only be added in URI_ONLY or METADATA state!");
256257
}
257258
}
258259

259260
private void initAttachmentInfoLoader(Attachment attachment) {
260-
if (attachment.state != LoadingState.URI_ONLY) {
261+
if (attachment.getState() != LoadingState.URI_ONLY) {
261262
throw new IllegalStateException("initAttachmentInfoLoader can only be called for URI_ONLY state!");
262263
}
263264

264265
Bundle bundle = new Bundle();
265-
bundle.putParcelable(LOADER_ARG_ATTACHMENT, attachment.uri);
266-
loaderManager.initLoader(attachment.loaderId, bundle, mAttachmentInfoLoaderCallback);
266+
bundle.putParcelable(LOADER_ARG_ATTACHMENT, attachment.getUri());
267+
loaderManager.initLoader(attachment.getLoaderId(), bundle, mAttachmentInfoLoaderCallback);
267268
}
268269

269270
private void initAttachmentContentLoader(Attachment attachment) {
270-
if (attachment.state != LoadingState.METADATA) {
271+
if (attachment.getState() != LoadingState.METADATA) {
271272
throw new IllegalStateException("initAttachmentContentLoader can only be called for METADATA state!");
272273
}
273274

274275
Bundle bundle = new Bundle();
275-
bundle.putParcelable(LOADER_ARG_ATTACHMENT, attachment.uri);
276-
loaderManager.initLoader(attachment.loaderId, bundle, mAttachmentContentLoaderCallback);
276+
bundle.putParcelable(LOADER_ARG_ATTACHMENT, attachment.getUri());
277+
loaderManager.initLoader(attachment.getLoaderId(), bundle, mAttachmentContentLoaderCallback);
277278
}
278279

279280
private int getNextFreeLoaderId() {
@@ -296,16 +297,16 @@ public void onLoadFinished(Loader<Attachment> loader, Attachment attachment) {
296297
int loaderId = loader.getId();
297298
loaderManager.destroyLoader(loaderId);
298299

299-
if (!attachments.containsKey(attachment.uri)) {
300+
if (!attachments.containsKey(attachment.getUri())) {
300301
return;
301302
}
302303

303-
if (attachment.state == LoadingState.METADATA) {
304+
if (attachment.getState() == LoadingState.METADATA) {
304305
attachmentMvpView.updateAttachmentView(attachment);
305-
attachments.put(attachment.uri, attachment);
306+
attachments.put(attachment.getUri(), attachment);
306307
initAttachmentContentLoader(attachment);
307308
} else {
308-
attachments.remove(attachment.uri);
309+
attachments.remove(attachment.getUri());
309310
attachmentMvpView.removeAttachmentView(attachment);
310311
}
311312
}
@@ -329,15 +330,15 @@ public void onLoadFinished(Loader<Attachment> loader, Attachment attachment) {
329330
int loaderId = loader.getId();
330331
loaderManager.destroyLoader(loaderId);
331332

332-
if (!attachments.containsKey(attachment.uri)) {
333+
if (!attachments.containsKey(attachment.getUri())) {
333334
return;
334335
}
335336

336-
if (attachment.state == Attachment.LoadingState.COMPLETE) {
337+
if (attachment.getState() == Attachment.LoadingState.COMPLETE) {
337338
attachmentMvpView.updateAttachmentView(attachment);
338-
attachments.put(attachment.uri, attachment);
339+
attachments.put(attachment.getUri(), attachment);
339340
} else {
340-
attachments.remove(attachment.uri);
341+
attachments.remove(attachment.getUri());
341342
attachmentMvpView.removeAttachmentView(attachment);
342343
}
343344

@@ -363,11 +364,11 @@ public void onLoadFinished(Loader<Attachment> loader, Attachment attachment) {
363364
int loaderId = loader.getId();
364365
loaderManager.destroyLoader(loaderId);
365366

366-
if (attachment.state == Attachment.LoadingState.COMPLETE) {
367-
inlineAttachments.put(attachment.uri, new InlineAttachment(
368-
inlineAttachments.get(attachment.uri).getContentId(), attachment));
367+
if (attachment.getState() == Attachment.LoadingState.COMPLETE) {
368+
inlineAttachments.put(attachment.getUri(), new InlineAttachment(
369+
inlineAttachments.get(attachment.getUri()).getContentId(), attachment));
369370
} else {
370-
inlineAttachments.remove(attachment.uri);
371+
inlineAttachments.remove(attachment.getUri());
371372
}
372373

373374
postPerformStalledAction();
@@ -432,7 +433,7 @@ public void attachmentProgressDialogCancelled() {
432433
public void onClickRemoveAttachment(Uri uri) {
433434
Attachment attachment = attachments.get(uri);
434435

435-
loaderManager.destroyLoader(attachment.loaderId);
436+
loaderManager.destroyLoader(attachment.getLoaderId());
436437

437438
attachmentMvpView.removeAttachmentView(attachment);
438439
attachments.remove(uri);

legacy/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentContentLoader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AttachmentContentLoader extends AsyncTaskLoader<Attachment> {
3030

3131
public AttachmentContentLoader(Context context, Attachment attachment) {
3232
super(context);
33-
if (attachment.state != LoadingState.METADATA) {
33+
if (attachment.getState() != LoadingState.METADATA) {
3434
throw new IllegalArgumentException("Attachment provided to content loader must be in METADATA state");
3535
}
3636

@@ -60,15 +60,15 @@ public Attachment loadInBackground() {
6060

6161
InputStream in;
6262

63-
if (sourceAttachment.internalAttachment) {
63+
if (sourceAttachment.isInternalAttachment()) {
6464
ContentResolver unsafeContentResolver = context.getContentResolver();
65-
in = unsafeContentResolver.openInputStream(sourceAttachment.uri);
65+
in = unsafeContentResolver.openInputStream(sourceAttachment.getUri());
6666
} else {
6767
SafeContentResolver safeContentResolver = SafeContentResolver.newInstance(context);
68-
in = safeContentResolver.openInputStream(sourceAttachment.uri);
68+
in = safeContentResolver.openInputStream(sourceAttachment.getUri());
6969
}
7070
if (in == null) {
71-
Log.w("Error opening attachment for reading: %s", sourceAttachment.uri);
71+
Log.w("Error opening attachment for reading: %s", sourceAttachment.getUri());
7272

7373
cachedResultAttachment = sourceAttachment.deriveWithLoadCancelled();
7474
return cachedResultAttachment;

legacy/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentInfoLoader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class AttachmentInfoLoader extends AsyncTaskLoader<Attachment> {
2626

2727
public AttachmentInfoLoader(Context context, Attachment attachment) {
2828
super(context);
29-
if (attachment.state != LoadingState.URI_ONLY) {
29+
if (attachment.getState() != LoadingState.URI_ONLY) {
3030
throw new IllegalArgumentException("Attachment provided to metadata loader must be in URI_ONLY state");
3131
}
3232

@@ -47,8 +47,8 @@ protected void onStartLoading() {
4747
@Override
4848
public Attachment loadInBackground() {
4949
try {
50-
Uri uri = sourceAttachment.uri;
51-
String contentType = sourceAttachment.contentType;
50+
Uri uri = sourceAttachment.getUri();
51+
String contentType = sourceAttachment.getContentType();
5252

5353
long size = -1;
5454
String name = null;
@@ -86,7 +86,7 @@ public Attachment loadInBackground() {
8686
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
8787
}
8888

89-
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
89+
if (!sourceAttachment.getAllowMessageType() && MimeUtility.isMessageType(usableContentType)) {
9090
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
9191
}
9292

0 commit comments

Comments
 (0)