/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.android.authenticator.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * SharedPreferences Util
 */
public class SharedPrefsUtils {
    public final static String SETTING = "Setting";

    public static void putValue(Context context, String key, int value) {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        sp.putInt(key, value);
        sp.commit();
    }

    public static void putValue(Context context, String key, boolean value) {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        sp.putBoolean(key, value);
        sp.commit();
    }

    public static void putValue(Context context, String key, String value) {
        Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
        sp.putString(key, value);
        sp.commit();
    }

    public static int getValue(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        int value = sp.getInt(key, defValue);
        return value;
    }

    public static boolean getValue(Context context, String key, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        boolean value = sp.getBoolean(key, defValue);
        return value;
    }

    public static String getValue(Context context, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        String value = sp.getString(key, defValue);
        return value;
    }

    public static void putArrayList(Context context, String key, List<String> list) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        Editor edit = sp.edit();
        Set<String> set = new HashSet<String>();
        set.addAll(list);
        edit.putStringSet(key, set);
        edit.commit();
    }

    public static List<String> getArrayList(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        Set<String> set = sp.getStringSet(key, null);
        if (set == null) return null;
        ArrayList<String> list = new ArrayList<>(set);
        return list;
    }

    public static void clearAll(Context context) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    public static void removeKeyValue(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        sp.edit().remove(key).commit();
    }

}


@@ -1474,38 +1474,37 @@
 ue);%0A        sp.
-commit
+apply
 ();%0A    %7D%0A%0A    p
@@ -1702,38 +1702,37 @@
 ue);%0A        sp.
-commit
+apply
 ();%0A    %7D%0A%0A    p
@@ -1936,22 +1936,21 @@
      sp.
-commit
+apply
 ();%0A    
@@ -2132,19 +2132,14 @@
     
-int value =
+return
  sp.
@@ -2161,38 +2161,16 @@
 Value);%0A
-        return value;%0A
     %7D%0A%0A 
@@ -2356,23 +2356,14 @@
     
-boolean value =
+return
  sp.
@@ -2389,38 +2389,16 @@
 Value);%0A
-        return value;%0A
     %7D%0A%0A 
@@ -2582,22 +2582,14 @@
     
-String value =
+return
  sp.
@@ -2618,30 +2618,8 @@
 e);%0A
-        return value;%0A
     
@@ -2874,37 +2874,9 @@
 Set%3C
-String%3E();%0A        set.addAll
+%3E
 (lis
@@ -2929,22 +2929,21 @@
    edit.
-commit
+apply
 ();%0A    
@@ -3216,32 +3216,14 @@
     
-ArrayList%3CString%3E list =
+return
  new
@@ -3245,29 +3245,8 @@
 t);%0A
-        return list;%0A
     
@@ -3417,22 +3417,21 @@
 clear().
-commit
+apply
 ();%0A    
@@ -3628,14 +3628,13 @@
 ey).
-commit
+apply
 ();%0A

fix SharedPrefs commits calls