Photo Storage & Kotlin Integration

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last segment, you built a complete Swift persistence layer: TaskManager with CRUD operations, PhotoStorage for managing photo files, and TaskStorage for JSON serialization. Your Swift code is production-ready and thoroughly tested.

But there’s a critical gap: your Kotlin UI can’t use these Swift methods yet. The Swift methods are compiled and exposed via swift-java, but you haven’t wired them up to your Kotlin Repository layer. The UI still displays mock data, and edits don’t persist.

In this segment, you’ll complete the integration:

  • Wire TaskRepository methods to Swift CRUD operations via swift-java.
  • Implement photo display in TaskCard using cross-platform path resolution.
  • Add getPhotoPath() bridge method to TaskManager.
  • Build edit functionality with pre-populated forms.
  • Test the complete workflow from UI tap to disk persistence.

By the end, your app will have a fully functional CRUD system where UI changes flow through Swift and persist to disk, complete with photo support and reactive updates via StateFlow.

Understanding the Integration Architecture

Before wiring up Kotlin to Swift, let’s understand how the layers fit together and why this architecture provides clean separation of concerns.

The Complete Stack

When a user taps “Edit” on a task card, data flows through five layers:

Building the Photo Save Helper

To begin, you need a helper method that bridges Android’s photo system with Swift’s photo storage. When users add or change task photos, the photo data comes from Android (camera, gallery, file picker) but needs to be saved via Swift’s PhotoStorage.

Implementing savePhotoViaPath()

Open TaskRepository.kt and add this private helper method after the init block and before other methods:

Verifying Swift’s savePhotoFromPath Method

The helper calls TaskManager.savePhotoFromPath(), which doesn’t exist yet. You’ll add that now.

Implementing TaskRepository.updateTask()

With the architecture clear and PhotoStorage understood, you’re ready to wire up Kotlin’s Repository layer to Swift’s CRUD operations. Start with the update operation, which is the most complex due to photo handling.

Implementing updateTask()

Add this method after addTask():

Understanding swift-java Object Marshaling

When you call manager.updateTask(task), swift-java:

Improving TaskRepository.addTask()

Now that you’ve implemented the updateTask() operation, you’ll improve addTask() for creating new tasks. It follows the same photo-handling pattern you just learned but is simpler because you’re creating the task from scratch, there’s no existing fields to preserve.

Updating the Method

Replace the TaskRepository.addTask() implementation with the following:

Implementing TaskRepository.deleteTask()

Deleting a task is fairly straight-forward because you don’t need to construct a Task object, you just pass the ID. However, you still follow the same pattern: call Swift, check result, reload on success.

Adding the Method

Add this method to TaskRepository.kt after updateTask():

Understanding Cascading Delete

The beauty of this implementation i that you don’t manage photo cleanup in Kotlin. It happens automatically in Swift:

Adding getPhotoPath() to TaskManager

Next, TaskCard needs to display task photos, but it only knows task IDs, not photo filenames or paths. You need a bridge method in TaskManager that resolves the following: task ID → photo filename → full file path.

Implementing getPhotoPath()

Open taskmanager-lib/Sources/TaskManagerKit/TaskManager.swift. Add this method after getTask(by:):

Why documentsPath Parameter?

PhotoStorage needs documentsPath because Swift’s FileManager.default.urls(for: .documentDirectory) doesn’t work correctly on Android. You must pass the path from Kotlin. In TaskCard, you’ll get it from LocalContext:

Displaying Photos in TaskCard

With getPhotoPath() available in TaskManager, you can now display task photos in your Compose UI. The TaskCard composable will fetch photo paths and load them using AsyncImage.

Understanding TaskCard’s Current State

Open app/src/main/java/com/kodeco/android/swiftsdkforandroid/taskmanager/ui/TaskCard.kt. The Starter project has a basic TaskCard that displays task information but has a TODO for photos:

Implementing TaskCard with Photo Display

Replace the TaskCard implementation with this complete version:

Understanding the Implementation

Let’s break down what each numbered section does:

Adding Edit Functionality

With photos displaying, the last piece is edit functionality. Users should tap a task card’s edit button, see a dialog pre-populated with current values, make changes, and save.

Updating CreateTaskDialog for Edit Mode

The Starter project’s CreateTaskDialog has this signature:

Adding editTask Parameter

Open app/src/main/java/.../ui/CreateTaskDialog.kt and modify the function signature:

Adding Mode Detection

Add this right after the function signature, before any state declarations:

Pre-Populating State from editTask

Find your existing state declarations and update them to use editTask values:

Adding Photo Loading Effect

After your state declarations, add this LaunchedEffect:

Updating Dialog Title

Find the AlertDialog title parameter and update it:

Implementing Branching Save Logic

This is the critical piece. Find your save Button’s onClick handler and replace it with:

Wiring Edit and Delete in TaskListScreen

TaskCard’s action buttons are already implemented. Now wire them in your TaskListScreen to handle edit and delete operations. Open ui/TaskListScreen.kt and update the LazyColumn section:

Testing the Edit Functionality

It’s time for you to test and validate the full update workflow from UI to persistence and back.

Mifv uvux hojftaobetiqj
Kodr owen zuhqzuogojezs

Learning Resources

To deepen your understanding of the technologies used in this lesson:

Key Takeaways

You’ve completed the integration between Kotlin UI and Swift persistence layer, creating a fully functional CRUD system with photo support. Here’s what you learned:

See forum comments
Download course materials from Github
Previous: CRUD Operations & Validation Next: Conclusion