Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Add workaround for using Moko in tests and previews #114

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions android/shared/src/main/res/values-en/generated_strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 1.1.2 -->
<!-- Generated by Twine 1.1.3 -->
<!-- Language: en -->
<resources>
<!-- SECTION: General -->
Expand Down Expand Up @@ -249,9 +249,7 @@
<string name="recipe_tipkit_popover_tip_title">This tip is floating, you can define arrow anchors</string>
<string name="recipe_tipkit_popover_tip_message">This message is optional</string>
<string name="recipe_tipkit_rule_tip_title">This tip has rule that appears the tip after 3 taps on button</string>
<string name="recipe_tipkit_rule_tip_message">If you close the tip you will have to restart the app for appear tip
again
</string>
<string name="recipe_tipkit_rule_tip_message">If you close the tip you will have to restart the app for appear tip again</string>
<string name="recipe_tipkit_title">Example of TipKit</string>
<string name="recipe_tipkit_inline_tip">Inline tip</string>
<string name="recipe_tipkit_rule_tip">Rule tip</string>
Expand Down
9 changes: 3 additions & 6 deletions android/shared/src/main/res/values-sk/generated_strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 1.1.2 -->
<!-- Generated by Twine 1.1.3 -->
<!-- Language: sk -->
<resources>
<!-- SECTION: General -->
Expand Down Expand Up @@ -248,11 +248,8 @@
<string name="recipe_tipkit_inline_tip_message">Táto správa je nepovinná</string>
<string name="recipe_tipkit_popover_tip_title">Tento tip je plávajúci, môžete definovať kotvy šípok</string>
<string name="recipe_tipkit_popover_tip_message">Táto správa je nepovinná</string>
<string name="recipe_tipkit_rule_tip_title">Tento tip má pravidlo, že sa tip zobrazí po 3 ťuknutiach na tlačidlo
</string>
<string name="recipe_tipkit_rule_tip_message">Ak tip zatvoríte, budete musieť reštartovať aplikáciu, aby sa tip opäť
zobrazil
</string>
<string name="recipe_tipkit_rule_tip_title">Tento tip má pravidlo, že sa tip zobrazí po 3 ťuknutiach na tlačidlo</string>
<string name="recipe_tipkit_rule_tip_message">Ak tip zatvoríte, budete musieť reštartovať aplikáciu, aby sa tip opäť zobrazil</string>
<string name="recipe_tipkit_title">Príklady TipKita</string>
<string name="recipe_tipkit_inline_tip">Inline tip</string>
<string name="recipe_tipkit_rule_tip">Rule tip</string>
Expand Down
9 changes: 3 additions & 6 deletions android/shared/src/main/res/values/generated_strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 1.1.2 -->
<!-- Generated by Twine 1.1.3 -->
<!-- Language: cs -->
<resources>
<!-- SECTION: General -->
Expand Down Expand Up @@ -248,11 +248,8 @@
<string name="recipe_tipkit_inline_tip_message">Tahle zpráva je nepovinná</string>
<string name="recipe_tipkit_popover_tip_title">Tento tip je plovoucí, můžete definovat kotvy šipek</string>
<string name="recipe_tipkit_popover_tip_message">Tahle zpráva je nepovinná</string>
<string name="recipe_tipkit_rule_tip_title">Tento tip má pravidlo, že se tip objeví po 3 klepnutích na tlačítko
</string>
<string name="recipe_tipkit_rule_tip_message">Pokud tip zavřete, budete muset aplikaci restartovat, aby se tip znovu
objevil.
</string>
<string name="recipe_tipkit_rule_tip_title">Tento tip má pravidlo, že se tip objeví po 3 klepnutích na tlačítko</string>
<string name="recipe_tipkit_rule_tip_message">Pokud tip zavřete, budete muset aplikaci restartovat, aby se tip znovu objevil.</string>
<string name="recipe_tipkit_title">Příklady TipKitu</string>
<string name="recipe_tipkit_inline_tip">Inline tip</string>
<string name="recipe_tipkit_rule_tip">Rule tip</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ extension ValidationError: LocalizedError {
switch self {
case .email(let reason):
switch reason {
#warning("TODO: Use MR strings when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
case .isEmpty: "Invalid email format" // MR.strings().invalid_email.desc().localized()
case .isEmpty: return MR.strings().invalid_email.desc().localized()
}
case .password(let reason):
switch reason {
#warning("TODO: Use MR strings when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
case .isEmpty: "Invalid password" // MR.strings().invalid_password.desc().localized()
case .isEmpty: return MR.strings().invalid_password.desc().localized()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ final class LoginViewModelTests: XCTestCase {
func testLoginInvalidPassword() async {
let vm = createViewModel()
let errorResult = AuthError.InvalidLoginCredentials(throwable: nil)
#warning("TODO: Use localizedMessage when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
fixMokoResourcesForTests()
loginUseCase.executeThrowableError = KmmLocalizedError(
errorResult: errorResult,
localizedMessage: "" // errorResult.localizedMessage(nil)
localizedMessage: errorResult.localizedMessage(nil)
)

vm.onIntent(.changeEmail(LoginData.stubValid.email))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class RegistrationViewModelTests: XCTestCase {
// MARK: Tests

func testRegisterEmptyEmail() async {
fixMokoResourcesForTests()
let vm = createViewModel()
registrationUseCase.executeThrowableError = ValidationError.email(.isEmpty)

Expand All @@ -39,14 +40,13 @@ final class RegistrationViewModelTests: XCTestCase {
await vm.awaitAllTasks()

XCTAssert(!vm.state.isLoading)
#warning("TODO: Use MR strings when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
// XCTAssertEqual(vm.state.alert, AlertData(title: MR.strings().invalid_email.desc().localized()))
XCTAssertEqual(vm.state.alert, AlertData(title: "Invalid email format"))
XCTAssertEqual(vm.state.alert, AlertData(title: MR.strings().invalid_email.desc().localized()))
XCTAssertEqual(fc.handleFlowValue, nil)
XCTAssert(registrationUseCase.executeReceivedInvocations == [.stubEmptyEmail])
}

func testRegisterEmptyPassword() async {
fixMokoResourcesForTests()
let vm = createViewModel()
registrationUseCase.executeThrowableError = ValidationError.password(.isEmpty)

Expand All @@ -56,9 +56,7 @@ final class RegistrationViewModelTests: XCTestCase {
await vm.awaitAllTasks()

XCTAssert(!vm.state.isLoading)
#warning("TODO: Use MR strings when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
// XCTAssertEqual(vm.state.alert, AlertData(title: MR.strings().invalid_password.desc().localized()))
XCTAssertEqual(vm.state.alert, AlertData(title: "Invalid password"))
XCTAssertEqual(vm.state.alert, AlertData(title: MR.strings().invalid_password.desc().localized()))
XCTAssertEqual(fc.handleFlowValue, nil)
XCTAssert(registrationUseCase.executeReceivedInvocations == [.stubEmptyPassword])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ExampleTipKitViewModel: BaseViewModel, ObservableObject, ViewModel {
super.init()

if #available(iOS 17, *) {
state.remainigTapsToShowTip = 3 - RuleTip.remainToShow.donations.count
state.remainingTapsToShowTip = 3 - RuleTip.remainToShow.donations.count
}
}

Expand Down

This file was deleted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this? 🤔 It seems to be created in the stencil files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried my best to remove the hack from swiftgen files, but this is the best I can do 😅 I think an iOS developer will know more about how they work and will be able to remove a lot of code and then this will not be needed anymore, but that's beyond my skills

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understandable 😅 I checked it and this file can be safely removed. But you don't have to do it here, I think.. We'll do that in MateeStarter.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Created by Petr Chmelar on 30.05.2022
// Copyright © 2022 Matee. All rights reserved.
//

import Foundation

private final class BundleToken {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by Julia Jakubcova on 23/09/2024
// Copyright © 2024 Matee. All rights reserved.
//

import DevstackKmpShared

#warning("TODO: Remove this workaround when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
public func fixMokoResourcesForTests() {
if ProcessInfo.processInfo.processName == "xctest" {
MokoResourcesPreviewWorkaroundKt.nsBundle = Bundle.init(for: KotlinBase.self)
}
}

#warning("TODO: Remove this workaround when issue [https://github.com/icerockdev/moko-resources/issues/714] is resolved")
public func fixMokoResourcesForPreviews() {
if ProcessInfo.processInfo.processName == "XCPreviewAgent" {
MokoResourcesPreviewWorkaroundKt.nsBundle = Bundle.init(for: KotlinBase.self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public enum Plurals: String {

func stringForCount(_ count: Int) -> String {
if count == 0 { // swiftlint:disable:this empty_count
return String(format: NSLocalizedString("zero_\(rawValue)", bundle: .myModule, comment: ""), count)
return String(format: NSLocalizedString("zero_\(rawValue)", bundle: .module, comment: ""), count)
} else if abs(count) == 1 {
return String(format: NSLocalizedString("one_\(rawValue)", bundle: .myModule, comment: ""), count)
return String(format: NSLocalizedString("one_\(rawValue)", bundle: .module, comment: ""), count)
} else if abs(count) > 1 && abs(count) < 5 {
return String(format: NSLocalizedString("few_\(rawValue)", bundle: .myModule, comment: ""), count)
return String(format: NSLocalizedString("few_\(rawValue)", bundle: .module, comment: ""), count)
} else if abs(count) >= 5 {
return String(format: NSLocalizedString("many_\(rawValue)", bundle: .myModule, comment: ""), count)
return String(format: NSLocalizedString("many_\(rawValue)", bundle: .module, comment: ""), count)
} else {
return String(format: NSLocalizedString("other_\(rawValue)", bundle: .myModule, comment: ""), count)
return String(format: NSLocalizedString("other_\(rawValue)", bundle: .module, comment: ""), count)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Revision: 7e13d641745b56775d9a7f983a5468d2d9952ada

// Modifications:
// - Use `.myModule` [workaround](https://developer.apple.com/forums/thread/664295) to address SwiftUI Previews crashes
// - Use snake_case instead of camelCase, this is done by changing `swiftIdentifier:"pretty"` to `swiftIdentifier`
// - Ability to change localization via Environment.localization

Expand Down Expand Up @@ -102,7 +101,7 @@ extension {{enumName}} {
private final class BundleToken {
static let bundle: Bundle = {
#if SWIFT_PACKAGE
return Bundle.myModule
return Bundle.module
#else
return Bundle(for: BundleToken.self)
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Revision: 937d05fa32ff389c63f93f0f9e2919d2c461b3c9

// Modifications:
// - Use `.myModule` [workaround](https://developer.apple.com/forums/thread/664295) to address SwiftUI Previews crashes
// - Ensure SwiftUI-first behavior for colors by renaming `.color` to `.uiColor` and `.swiftUIColor` to `.color`
// - Ensure SwiftUI-first behavior for images by renaming `.image` to `.uiImage` and `.swiftUIImage` to `.image`

Expand Down Expand Up @@ -433,7 +432,7 @@
private final class BundleToken {
static let bundle: Bundle = {
#if SWIFT_PACKAGE
return Bundle.myModule
return Bundle.module
#else
return Bundle(for: BundleToken.self)
#endif
Expand Down
2 changes: 1 addition & 1 deletion shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 1.1.2 -->
<!-- Generated by Twine 1.1.3 -->
<!-- Language: en -->
<resources>
<!-- SECTION: General -->
Expand Down
2 changes: 1 addition & 1 deletion shared/src/commonMain/resources/MR/cs/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 1.1.2 -->
<!-- Generated by Twine 1.1.3 -->
<!-- Language: cs -->
<resources>
<!-- SECTION: General -->
Expand Down
2 changes: 1 addition & 1 deletion shared/src/commonMain/resources/MR/sk/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Android Strings File -->
<!-- Generated by Twine 1.1.2 -->
<!-- Generated by Twine 1.1.3 -->
<!-- Language: sk -->
<resources>
<!-- SECTION: General -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.icerock.moko.resources.utils // must be same as in moko-resources to override method

import platform.Foundation.NSBundle
import platform.Foundation.NSDirectoryEnumerator
import platform.Foundation.NSFileManager
import platform.Foundation.NSURL
import platform.Foundation.pathExtension

/**
* Workaround by MAX-POLKOVNIK from https://github.com/icerockdev/moko-resources/issues/747#issuecomment-2330854244
* Remove when https://github.com/icerockdev/moko-resources/issues/747 is resolved
*/

var nsBundle: NSBundle = NSBundle.mainBundle // <-- this is where we should looking for resources, by default mainBundle

fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle {
val bundlePath: String = nsBundle.bundlePath // <-- path where we should search for bundle with resources
val enumerator: NSDirectoryEnumerator = requireNotNull(NSFileManager.defaultManager.enumeratorAtPath(bundlePath))
while (true) {
val relativePath: String = enumerator.nextObject() as? String ?: break
val url = NSURL(fileURLWithPath = relativePath)
if (url.pathExtension == "bundle") {
val fullPath = "$bundlePath/$relativePath"
val foundedBundle: NSBundle? = NSBundle.bundleWithPath(fullPath)
val loadedIdentifier: String? = foundedBundle?.bundleIdentifier

if (isBundleSearchLogEnabled) {
println("moko-resources auto-load bundle with identifier $loadedIdentifier at path $fullPath")
}

if (foundedBundle?.bundleIdentifier == identifier) return foundedBundle
}
}

throw IllegalArgumentException("bundle with identifier $identifier not found")
}

var isBundleSearchLogEnabled = false
Loading