/**
 * @project RoboMVVM(https://github.com/debdattabasu/RoboMVVM)
 * @author Debdatta Basu
 *
 * @license 3-clause BSD license(http://opensource.org/licenses/BSD-3-Clause).
 *      Copyright (c) 2014, Debdatta Basu. All rights reserved.
 *
 *      Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 *      the following conditions are met:
 *
 *          1. Redistributions of source code must retain the above copyright notice, this list of
 *             conditions and the following disclaimer.
 *
 *          2. Redistributions in binary form must reproduce the above copyright notice, this list of
 *             conditions and the following disclaimer in the documentation and/or other materials
 *             provided with the distribution.
 *
 *          3. Neither the name of the copyright holder nor the names of its contributors may be used
 *             to endorse or promote products derived from this software without specific prior
 *             written permission.
 *
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *      INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *      IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *      OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 *      OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 *      OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *      POSSIBILITY OF SUCH DAMAGE.
 */
package org.dbasu.robomvvm.sample_itemlist;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.PopupWindow;


/**
 * Main activity for a basic use case of RoboMVVM(https://github.com/debdattabasu/RoboMVVM). This app lets you add,
 * remove and modify string items in a ListView. It also has an options menu where you can view a description of this app.
 */
public class MainActivity extends Activity {

    private PopupWindow aboutPopup;
    private View rootView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**
         * Set MainActivityViewModel as the view model of this
         * activity.
         */
        MainActivityViewModel viewModel = new MainActivityViewModel(this);
        rootView = viewModel.createView();
        setContentView(rootView);

        /**
         * Set AboutPopupViewModel as the view model for
         * the popup window.
         */

        AboutPopupViewModel aboutPopupViewModel = new AboutPopupViewModel(this);
        aboutPopup = new PopupWindow(aboutPopupViewModel.createView(), LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        /**
         * Set MainMenuViewModel as the view model
         * for the options menu.
         */
        MainMenuViewModel mainMenu = new MainMenuViewModel(this);
        mainMenu.inflate(menu);
        return true;
    }

    public void showAboutPopup() {
        aboutPopup.showAtLocation(rootView, Gravity.CENTER, 0, 0);
    }

    public void hideAboutPopup() {
        aboutPopup.dismiss();
    }
}

@@ -1902,16 +1902,58 @@
 mlist;%0A%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -2160,16 +2160,88 @@
 indow;%0A%0A
+import java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0A
 %0A/**%0A * 
@@ -2522,24 +2522,146 @@
 Activity %7B%0A%0A
+    private static final String PREFERENCES_ID = %22robomvvm_prefs%22;%0A    private static final String ITEMS_PREF = %22items%22;%0A%0A
     private 
@@ -2711,16 +2711,61 @@
 otView;%0A
+    private MainActivityViewModel viewModel;%0A
 %0A%0A    @O
@@ -2870,24 +2870,25 @@
 nceState);%0A%0A
+%0A
         /**%0A
@@ -2991,38 +2991,16 @@
         
-MainActivityViewModel 
 viewMode
@@ -3457,303 +3457,1243 @@
     
-%7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A%0A        /**%0A         * Set MainMenuViewModel as the view model%0A         * for the options menu.%0A         */%0A        MainMenuViewModel mainMenu = new MainMenuViewModel(this);%0A        mainMenu.inflate(menu);%0A        return true
+    /**%0A         * Restore state.%0A         */%0A%0A        SharedPreferences prefs = getSharedPreferences(PREFERENCES_ID, MODE_PRIVATE);%0A%0A        Set%3CString%3E stringItems = prefs.getStringSet(ITEMS_PREF, null);%0A%0A        if(stringItems != null) %7B%0A            for(String item : stringItems) %7B%0A                viewModel.getStrings().add(new StringViewModel(this, item));%0A            %7D%0A        %7D%0A%0A    %7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A%0A        /**%0A         * Set MainMenuViewModel as the view model%0A         * for the options menu.%0A         */%0A        MainMenuViewModel mainMenu = new MainMenuViewModel(this);%0A        mainMenu.inflate(menu);%0A        return true;%0A    %7D%0A%0A    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A%0A        super.onSaveInstanceState(outState);%0A%0A        SharedPreferences.Editor prefs = getSharedPreferences(PREFERENCES_ID, MODE_PRIVATE).edit();%0A%0A        List%3CStringViewModel%3E items = viewModel.getStrings().getBackingStore();%0A        HashSet%3CString%3E stringItems = new HashSet%3CString%3E();%0A%0A        for(StringViewModel item : items) %7B%0A            stringItems.add(item.getString());%0A        %7D%0A%0A        prefs.putStringSet(ITEMS_PREF, stringItems);%0A        prefs.commit()
 ;%0A  

store items persistently in SharedPreferences