diff --git a/src/NotesMainPanel.js b/src/NotesMainPanel.js index 5aef9d3..4722884 100644 --- a/src/NotesMainPanel.js +++ b/src/NotesMainPanel.js @@ -5,9 +5,11 @@ import React from 'react'; import { + Alert, AppRegistry, Dimensions, FlatList, + NativeModules, StyleSheet, View, } from 'react-native'; @@ -23,14 +25,14 @@ class NotesMainPanel extends React.Component { constructor(props) { super(props); this.state = { - notes: [{key: "1"}, {key: "2"}, {key: "3"}, {key: "4"}, {key: "5"}, {key: "6"}, {key: "7"}, {key: "8"}, {key: "9"}], + notes: [], dimensions: {window, screen}, columns: this.calculateColumnWidth(window), } }; calculateColumnWidth = (window) => { - return Math.floor(window.width / noteWidgetWidth); + return Math.floor(Dimensions.get("window").width / noteWidgetWidth); }; onChange = ({ window, screen }) => { @@ -38,6 +40,7 @@ class NotesMainPanel extends React.Component { }; componentDidMount() { + this.getDataFromDatabase(); Dimensions.addEventListener("change", this.onChange); }; @@ -45,6 +48,21 @@ class NotesMainPanel extends React.Component { Dimensions.removeEventListener("change", this.onChange); }; + createNotesKeys = (numberOfNotes) => { + let allNotesKeys = []; + for(id = 0; id < numberOfNotes; id++) { + const nextObject = {key: id}; + allNotesKeys.push(nextObject); + } + this.setState({notes: allNotesKeys}); + }; + + getDataFromDatabase = () => { + NativeModules.Database.getNumberOfNotes() + .then(result => this.createNotesKeys(result)) + .catch(error => Alert.alert("ERROR!", `Result: ${error}`)); + }; + renderNote = notes => { return }; @@ -52,7 +70,7 @@ class NotesMainPanel extends React.Component { render() { return( - + ); } diff --git a/src/Widgets/NoteWidget.js b/src/Widgets/NoteWidget.js index 54e9ace..35806a3 100644 --- a/src/Widgets/NoteWidget.js +++ b/src/Widgets/NoteWidget.js @@ -3,7 +3,7 @@ * @flow strict-local */ -import React from 'react'; +import React, {useState, useEffect} from 'react'; import { AppRegistry, StyleSheet, @@ -11,51 +11,66 @@ import { TouchableHighlight, View, NativeModules, + Alert, } from 'react-native'; -class NoteWidget extends React.Component { - constructor(props) { - super(props); - this.state = { - width: props.width, - ID: Number(props.ID), - } - }; +export default function NoteWidget(props){ + const {width, ID} = props; + + const [title, setTitle] = useState(""); + const [shortMessage, setShortMessage] = useState(""); + + useEffect(() => { + getNoteTitle(); + getNoteShortMessage(); + }, []); - enterNote = () => { - NativeModules.NoteWidgetClickHandler.openWidget(this.state.ID); + const enterNote = () => { + NativeModules.NoteWidgetClickHandler.openWidget(ID); }; - render() { - return( - - - - {this.state.ID} - - Header - - + const getNoteTitle = () => { + NativeModules.Database.getNoteTitle(ID) + .then(result => setTitle(result)) + .catch(error => Alert.alert("ERROR!", `${error}`)); + }; + + const getNoteShortMessage = () => { + NativeModules.Database.getNoteShortPost(ID) + .then(result => setShortMessage(result)) + .catch(error => Alert.alert("ERROR!", `${error}`)); + }; - + return( + + - - - This is the single widget - {'\n'}With the text written here only for the presentation purpose. + + {ID} + + + {title} - This note has the ID: {this.state.ID} + + + + + + {shortMessage} + - - ); - } + + + + ); }; + const styles = StyleSheet.create({ noteWidget: { borderColor: "grey", @@ -95,4 +110,3 @@ const styles = StyleSheet.create({ AppRegistry.registerComponent("NoteWidget", () => NoteWidget); -export default NoteWidget; diff --git a/windows/ReactNativeNotes/App.cpp b/windows/ReactNativeNotes/App.cpp index 0cfceaa..89e3048 100644 --- a/windows/ReactNativeNotes/App.cpp +++ b/windows/ReactNativeNotes/App.cpp @@ -53,7 +53,7 @@ void App::OnLaunched(activation::LaunchActivatedEventArgs const& e) super::OnLaunched(e); Frame rootFrame = Window::Current().Content().as(); - rootFrame.Navigate(xaml_typename(), box_value(e.Arguments())); + rootFrame.Navigate(xaml_typename(), box_value(e.Arguments())); } /// diff --git a/windows/ReactNativeNotes/NativeModules/DatabaseHandler.hpp b/windows/ReactNativeNotes/NativeModules/DatabaseHandler.hpp index 6c42f35..ea7ef03 100644 --- a/windows/ReactNativeNotes/NativeModules/DatabaseHandler.hpp +++ b/windows/ReactNativeNotes/NativeModules/DatabaseHandler.hpp @@ -24,21 +24,45 @@ namespace winrt::ReactNativeNotes::implementation } REACT_METHOD( GetNoteTitle, L"getNoteTitle" ); - const winrt::hstring GetNoteTitle( const unsigned int ID ) noexcept + void GetNoteTitle( const int index, React::ReactPromise&& result ) noexcept { - return winrt::to_hstring(data->Read( ID ).Title()); + result.Resolve( React::JSValue( data->Read( index ).Title() ) ); } REACT_METHOD( GetNotePost, L"getNotePost" ); - const winrt::hstring GetNotePost( const unsigned int ID ) noexcept + const winrt::hstring GetNotePost( const int index ) noexcept { - return winrt::to_hstring( data->Read( ID ).Post() ); + return winrt::to_hstring( data->Read( index ).Post() ); } REACT_METHOD( GetNoteShortPost, L"getNoteShortPost" ); - const winrt::hstring GetNoteShortPost( const unsigned int ID ) noexcept + void GetNoteShortPost( const int index, React::ReactPromise&& result ) noexcept { - return winrt::to_hstring( data->Read( ID ).ShortPost() ); + result.Resolve( React::JSValue( data->Read( index ).ShortPost() ) ); + } + + REACT_METHOD( GetNumberOfNotes, L"getNumberOfNotes" ); + void GetNumberOfNotes( React::ReactPromise&& result ) noexcept + { + result.Resolve( React::JSValue( std::to_string(data->Size()) ) ); + } + + REACT_METHOD( DoesIDExists, L"doesIDExists" ); + const bool DoesIDExists( const unsigned int ID ) noexcept + { + return data->Exists( ID ); + } + + REACT_METHOD( GetAllNotesIDs, L"getAllNotesIDs" ); + Microsoft::ReactNative::JSValue GetAllNotesIDs() noexcept + { + Microsoft::ReactNative::JSValueArray keyArray; + for( unsigned int i = 0; i < data->Size(); ++i ) + { + if( data->Exists( i ) ) + keyArray.push_back( Microsoft::ReactNative::JSValueObject{ { "key", i } } ); + } + return Microsoft::ReactNative::JSValue( std::move( keyArray ) ); } private: diff --git a/windows/ReactNativeNotes/NativeModules/Repository/Repository.cpp b/windows/ReactNativeNotes/NativeModules/Repository/Repository.cpp index c25b97e..20c1280 100644 --- a/windows/ReactNativeNotes/NativeModules/Repository/Repository.cpp +++ b/windows/ReactNativeNotes/NativeModules/Repository/Repository.cpp @@ -20,6 +20,14 @@ namespace winrt::ReactNativeNotes::implementation return NoteModel(); } + NoteModel Repository::Read( const int index ) const noexcept + { + if( index >= notes.size() ) + return NoteModel(); + else + return notes.at(index); + } + void Repository::Update( const NoteModel& note ) noexcept { for( unsigned int index = 0; index < notes.size(); ++index ) @@ -36,8 +44,22 @@ namespace winrt::ReactNativeNotes::implementation notes.erase( it ); } + void Repository::Delete( const int index ) noexcept + { + notes.erase( notes.cbegin() + index ); + } + unsigned int Repository::Size() const noexcept { return notes.size(); } + + const bool Repository::Exists( const unsigned int ID ) const noexcept + { + return std::find_if( notes.cbegin(), notes.cend(), [=]( const NoteModel& n )->bool { return n.ID() == ID; } ) != notes.cend(); + } + const bool Repository::Exists( const int index ) const noexcept + { + return index < notes.size(); + } } diff --git a/windows/ReactNativeNotes/NativeModules/Repository/Repository.hpp b/windows/ReactNativeNotes/NativeModules/Repository/Repository.hpp index e7a882c..81bfe7b 100644 --- a/windows/ReactNativeNotes/NativeModules/Repository/Repository.hpp +++ b/windows/ReactNativeNotes/NativeModules/Repository/Repository.hpp @@ -19,13 +19,18 @@ namespace winrt::ReactNativeNotes::implementation void Create( NoteModel& note ) noexcept; NoteModel Read( const unsigned int ID ) const noexcept; + NoteModel Read( const int index ) const noexcept; void Update( const NoteModel& note ) noexcept; void Delete( const unsigned int ID ) noexcept; + void Delete( const int index ) noexcept; unsigned int Size() const noexcept; + const bool Exists( const unsigned int ID ) const noexcept; + const bool Exists( const int index ) const noexcept; + private: std::vector notes; };