|
1 | 1 | package com.philkes.notallyx.data.model |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.net.Uri |
4 | 6 | import android.text.Html |
5 | 7 | import android.util.Base64 |
| 8 | +import androidx.core.content.IntentCompat |
6 | 9 | import androidx.core.text.toHtml |
7 | 10 | import com.philkes.notallyx.R |
| 11 | +import com.philkes.notallyx.data.dao.BaseNoteDao.Companion.MAX_BODY_CHAR_LENGTH |
8 | 12 | import com.philkes.notallyx.data.dao.NoteIdReminder |
9 | 13 | import com.philkes.notallyx.data.imports.markdown.createMarkdownFromBodyAndSpans |
10 | 14 | import com.philkes.notallyx.data.model.BaseNote.Companion.COLOR_DEFAULT |
11 | 15 | import com.philkes.notallyx.presentation.applySpans |
| 16 | +import com.philkes.notallyx.presentation.showToast |
| 17 | +import com.philkes.notallyx.presentation.viewmodel.NotallyModel |
12 | 18 | import com.philkes.notallyx.utils.decodeToBitmap |
| 19 | +import com.philkes.notallyx.utils.getFileName |
| 20 | +import com.philkes.notallyx.utils.getMimeType |
13 | 21 | import java.io.File |
14 | 22 | import java.text.DateFormat |
15 | 23 | import java.text.SimpleDateFormat |
@@ -560,3 +568,75 @@ fun ColorString.isValid() = |
560 | 568 | false |
561 | 569 | } |
562 | 570 | } |
| 571 | + |
| 572 | +fun Intent.generateBaseNote(context: Context): BaseNote { |
| 573 | + val title = getStringExtra(Intent.EXTRA_SUBJECT) ?: data?.let { context.getFileName(it) } ?: "" |
| 574 | + val intentFiles = |
| 575 | + IntentCompat.getParcelableArrayListExtra(this, Intent.EXTRA_STREAM, Uri::class.java) |
| 576 | + ?: IntentCompat.getParcelableExtra(this, Intent.EXTRA_STREAM, Uri::class.java)?.let { |
| 577 | + listOf(it) |
| 578 | + } |
| 579 | + val text = |
| 580 | + data?.let { uri -> |
| 581 | + context.contentResolver.openInputStream(uri)?.use { inputStream -> |
| 582 | + inputStream.bufferedReader().readText() |
| 583 | + } |
| 584 | + ?: run { |
| 585 | + context.showToast(R.string.cant_load_file) |
| 586 | + null |
| 587 | + } |
| 588 | + } |
| 589 | + ?: getStringExtra(Intent.EXTRA_TEXT)?.let { |
| 590 | + if (it.length > MAX_BODY_CHAR_LENGTH) { |
| 591 | + context.showToast( |
| 592 | + context.getString( |
| 593 | + R.string.note_text_too_long_truncated, |
| 594 | + MAX_BODY_CHAR_LENGTH, |
| 595 | + ) |
| 596 | + ) |
| 597 | + } |
| 598 | + it.take(MAX_BODY_CHAR_LENGTH) |
| 599 | + } |
| 600 | + ?: "" |
| 601 | + val (images, files) = |
| 602 | + intentFiles?.let { |
| 603 | + val filesByType = |
| 604 | + it.groupBy { uri -> |
| 605 | + context.getMimeType(uri)?.let { mimeType -> |
| 606 | + if (mimeType.isImageMimeType) { |
| 607 | + NotallyModel.FileType.IMAGE |
| 608 | + } else { |
| 609 | + NotallyModel.FileType.ANY |
| 610 | + } |
| 611 | + } ?: NotallyModel.FileType.ANY |
| 612 | + } |
| 613 | + val images = |
| 614 | + filesByType[NotallyModel.FileType.IMAGE]?.let { images -> |
| 615 | + images.map { FileAttachment("", it.toString(), "") } |
| 616 | + } ?: listOf() |
| 617 | + val files = |
| 618 | + filesByType[NotallyModel.FileType.ANY]?.let { otherFiles -> |
| 619 | + otherFiles.map { FileAttachment("", it.toString(), "") } |
| 620 | + } ?: listOf() |
| 621 | + Pair(images, files) |
| 622 | + } ?: Pair(listOf(), listOf()) |
| 623 | + return BaseNote( |
| 624 | + id = -1L, |
| 625 | + type = Type.NOTE, |
| 626 | + folder = Folder.NOTES, |
| 627 | + color = Color.FOG.toString(), |
| 628 | + title = title, |
| 629 | + pinned = false, |
| 630 | + timestamp = 0L, |
| 631 | + modifiedTimestamp = 0L, |
| 632 | + labels = listOf(), |
| 633 | + body = text, |
| 634 | + spans = listOf(), |
| 635 | + items = listOf(), |
| 636 | + images = images, |
| 637 | + files = files, |
| 638 | + audios = listOf(), |
| 639 | + reminders = listOf(), |
| 640 | + viewMode = NoteViewMode.EDIT, |
| 641 | + ) |
| 642 | +} |
0 commit comments