Skip to content

Commit

Permalink
Fix Arabic Text Rendering in Notes Sharing (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
yismailuofa authored Aug 24, 2024
1 parent 592e0e8 commit 8af1fd2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ public struct ShareableVerseTextRetriever {

private func arabicText(for verse: AyahNumber) async throws -> String {
let verseNumber = NumberFormatter.arabicNumberFormatter.format(verse.ayah)
return try await shareableVersePersistence.textForVerse(verse) + "﴿ \(verseNumber)"

// Avoid the arabic text to be displayed in the wrong direction in LTR languages
let rightToLeftMark = "\u{202B}"
let endMark = "\u{202C}"

let arabicVerse = try await shareableVersePersistence.textForVerse(verse) + "﴿ \(verseNumber)"

return "\(rightToLeftMark)\(arabicVerse)\(endMark)"
}

private func arabicScript(for verses: [AyahNumber]) async throws -> [String] {
Expand Down
15 changes: 8 additions & 7 deletions Domain/QuranTextKit/Tests/ShareableVerseTextRetrieverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ final class ShareableVerseTextRetrieverTests: XCTestCase {
let tests = [
(
verses: [quran.suras[0].verses[2]],
result: ["ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾",
result: ["\(rightToLeftMark)ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾\(endMark)",
"",
"Al-Fātihah, Ayah 3"]
),
(
verses: [quran.suras[0].verses[0], quran.suras[0].verses[1], quran.suras[0].verses[2]],
result: ["بِسۡمِ ٱللَّهِ ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ١ ﴾ ٱلۡحَمۡدُ لِلَّهِ رَبِّ ٱلۡعَـٰلَمِینَ﴿ ٢ ﴾ ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾",
result: ["\(rightToLeftMark)بِسۡمِ ٱللَّهِ ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ١ ﴾\(endMark) \(rightToLeftMark)ٱلۡحَمۡدُ لِلَّهِ رَبِّ ٱلۡعَـٰلَمِینَ﴿ ٢ ﴾\(endMark) \(rightToLeftMark)ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾\(endMark)",
"",
"Al-Fātihah, Ayah 1 - Al-Fātihah, Ayah 3"]
),
Expand All @@ -74,7 +74,7 @@ final class ShareableVerseTextRetrieverTests: XCTestCase {
let tests = [
(
verses: [quran.suras[0].verses[2]],
result: ["ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾",
result: ["\(rightToLeftMark)ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾\(endMark)",
"",
"• Khan & Hilai:",
TestData.translationTextAt(translations[0], quran.suras[0].verses[2]),
Expand All @@ -86,7 +86,7 @@ final class ShareableVerseTextRetrieverTests: XCTestCase {
),
(
verses: [quran.suras[0].verses[0], quran.suras[0].verses[1], quran.suras[0].verses[2]],
result: ["بِسۡمِ ٱللَّهِ ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ١ ﴾ ٱلۡحَمۡدُ لِلَّهِ رَبِّ ٱلۡعَـٰلَمِینَ﴿ ٢ ﴾ ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾",
result: ["\(rightToLeftMark)بِسۡمِ ٱللَّهِ ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ١ ﴾\(endMark) \(rightToLeftMark)ٱلۡحَمۡدُ لِلَّهِ رَبِّ ٱلۡعَـٰلَمِینَ﴿ ٢ ﴾\(endMark) \(rightToLeftMark)ٱلرَّحۡمَـٰنِ ٱلرَّحِیمِ﴿ ٣ ﴾\(endMark)",
"",
"• Khan & Hilai:",
TestData.translationTextAt(translations[0], quran.suras[0].verses[0]),
Expand All @@ -113,15 +113,15 @@ final class ShareableVerseTextRetrieverTests: XCTestCase {
try await localTranslationsFake.setTranslations([TestData.khanTranslation])

let numberReference = try await shareableTextRetriever.textForVerses([quran.suras[1].verses[49]])
XCTAssertEqual(numberReference, ["وَإِذۡ فَرَقۡنَا بِكُمُ ٱلۡبَحۡرَ فَأَنجَیۡنَـٰكُمۡ وَأَغۡرَقۡنَاۤ ءَالَ فِرۡعَوۡنَ وَأَنتُمۡ تَنظُرُونَ﴿ ٥٠ ﴾",
XCTAssertEqual(numberReference, ["\(rightToLeftMark)وَإِذۡ فَرَقۡنَا بِكُمُ ٱلۡبَحۡرَ فَأَنجَیۡنَـٰكُمۡ وَأَغۡرَقۡنَاۤ ءَالَ فِرۡعَوۡنَ وَأَنتُمۡ تَنظُرُونَ﴿ ٥٠ ﴾\(endMark)",
"",
"• Khan & Hilai:",
"See ayah 38.",
"",
"Al-Baqarah, Ayah 50"])

let verseSavedAsTextReference = try await shareableTextRetriever.textForVerses([quran.suras[1].verses[50]])
XCTAssertEqual(verseSavedAsTextReference, ["وَإِذۡ وَ ٰ⁠عَدۡنَا مُوسَىٰۤ أَرۡبَعِینَ لَیۡلَةࣰ ثُمَّ ٱتَّخَذۡتُمُ ٱلۡعِجۡلَ مِنۢ بَعۡدِهِۦ وَأَنتُمۡ ظَـٰلِمُونَ﴿ ٥١ ﴾",
XCTAssertEqual(verseSavedAsTextReference, ["\(rightToLeftMark)وَإِذۡ وَ ٰ⁠عَدۡنَا مُوسَىٰۤ أَرۡبَعِینَ لَیۡلَةࣰ ثُمَّ ٱتَّخَذۡتُمُ ٱلۡعِجۡلَ مِنۢ بَعۡدِهِۦ وَأَنتُمۡ ظَـٰلِمُونَ﴿ ٥١ ﴾\(endMark)",
"",
"• Khan & Hilai:",
"See ayah 38.",
Expand All @@ -136,7 +136,8 @@ final class ShareableVerseTextRetrieverTests: XCTestCase {
private var localTranslationsFake: LocalTranslationsFake!
private let quran = Quran.hafsMadani1405
private let statePreferences = QuranContentStatePreferences.shared

private let rightToLeftMark = "\u{202B}"
private let endMark = "\u{202C}"
private let translations = [
TestData.khanTranslation,
TestData.sahihTranslation,
Expand Down
19 changes: 6 additions & 13 deletions Features/NotesFeature/NotesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,18 @@ final class NotesViewModel: ObservableObject {

func prepareNotesForSharing() async throws -> String {
try await crasher.recordError("Failed to share notes") {
var notesText = ""
var notesText = [String]()
for note in await notes {
let noteText = if let noteContent = note.note.note, noteContent != "" {
"\(noteContent.trimmingCharacters(in: .newlines))\n"
let title = if let noteContent = note.note.note, noteContent != "" {
"\(noteContent.trimmingCharacters(in: .newlines))\n\n"
} else {
""
}
let verses = try await textRetriever.textForVerses(Array(note.note.verses)).joined(separator: "\n")

let verses = try await textRetriever.textForVerses(Array(note.note.verses))
.filter { !$0.isEmpty }
.map { $0.hasSuffix("") ? String($0.dropLast()) : $0 }
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }

let verseText = verses.joined(separator: "\n")

notesText += "\(noteText)\(verseText)\n\n"
notesText.append("\(title)\(verses)")
}

return notesText
return notesText.joined(separator: "\n\n\n\n")
}
}

Expand Down

0 comments on commit 8af1fd2

Please sign in to comment.