classdef CloseConfirmAppExample < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        AddPointsButton  matlab.ui.control.Button
        ClearButton      matlab.ui.control.Button
        UIAxes           matlab.ui.control.UIAxes
    end


    methods (Access = private)
    
        function confirmClose(app, ~, event)
            % Determine which dialog button the user clicked
            answer = event.SelectedOption;
            
            % Close the app if the user clicks OK 
            if strcmp(answer,'OK')
                delete(app);
            end
        end
        
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: AddPointsButton
        function AddPointsButtonPushed(app, event)
            % Add random points to the plot
            plot(app.UIAxes,1:10,rand(1,10),'o');
            hold(app.UIAxes,'on');
        end

        % Button pushed function: ClearButton
        function ClearButtonPushed(app, event)
            % Clear the axes
            cla(app.UIAxes);
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            % Display confirmation dialog box
            msg = 'Are you sure you want to close?';
            uiconfirm(app.UIFigure,msg,'Confirm Close','CloseFcn',@app.confirmClose);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'Plot Random Points';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create AddPointsButton
            app.AddPointsButton = uibutton(app.UIFigure, 'push');
            app.AddPointsButton.ButtonPushedFcn = createCallbackFcn(app, @AddPointsButtonPushed, true);
            app.AddPointsButton.Position = [204 70 100 22];
            app.AddPointsButton.Text = 'Add Points';

            % Create ClearButton
            app.ClearButton = uibutton(app.UIFigure, 'push');
            app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
            app.ClearButton.Position = [348 70 100 22];
            app.ClearButton.Text = 'Clear';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            app.UIAxes.Toolbar.Visible = 'off';
            app.UIAxes.Position = [96 124 429 284];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = CloseConfirmAppExample

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end