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