diff --git a/app/components/TaskItem.tsx b/app/components/TaskItem.tsx index 3eca151..f652e6f 100644 --- a/app/components/TaskItem.tsx +++ b/app/components/TaskItem.tsx @@ -1,6 +1,6 @@ import React from 'react'; import Realm from 'realm'; -import {View, Text, Pressable, StyleSheet} from 'react-native'; +import {View, Text, Pressable, StyleSheet, TouchableOpacity} from 'react-native'; import {shadows} from '../styles/shadows'; import colors from '../styles/colors'; @@ -33,9 +33,9 @@ export const TaskItem = React.memo( - - Edit first item - + + {`Toggle address (object <-> null)`} + SubItems: {JSON.stringify(task.items)} diff --git a/app/components/TaskManager.tsx b/app/components/TaskManager.tsx index 50e4b19..b48944f 100644 --- a/app/components/TaskManager.tsx +++ b/app/components/TaskManager.tsx @@ -69,8 +69,13 @@ export const TaskManager: React.FC<{ const handleAddSubItems = useCallback((task: Task & Realm.Object): void => { realm.write(() => { - // different behaviour on Realm 12, it pushes new item instead of updating one - task.items[0] = {name: 'First item changed'} + // different behaviour on Realm 12, it doesn't allow setting null for embedded schema field, + // but it can be null on init app + const firstItem = task.items[0] + firstItem['address'] = !firstItem.address ? { + city: 'Warsaw', + country: 'PL' + } : null }); }, [realm]) diff --git a/app/models/Task.ts b/app/models/Task.ts index 145c31b..476d061 100644 --- a/app/models/Task.ts +++ b/app/models/Task.ts @@ -51,7 +51,19 @@ export class ItemSchema extends Realm.Object { name: 'ItemSchema', embedded: true, properties: { - name: 'string' + name: 'string', + address: 'AddressSchema' + }, + }; +} + +export class AddressSchema extends Realm.Object { + static schema: Realm.ObjectSchema = { + name: 'AddressSchema', + embedded: true, + properties: { + city: 'string', + country: 'string' }, }; } \ No newline at end of file diff --git a/app/models/index.ts b/app/models/index.ts index 060f7bf..168de11 100644 --- a/app/models/index.ts +++ b/app/models/index.ts @@ -1,3 +1,3 @@ -import {ItemSchema, Task} from './Task'; +import {AddressSchema, ItemSchema, Task} from './Task'; -export const schemas = [Task, ItemSchema]; +export const schemas = [Task, ItemSchema, AddressSchema];