add UserDefault api deleteValueForKey

This commit is contained in:
jianglong0156 2015-07-06 17:01:17 +08:00
parent faab927f1b
commit 6f3b977c1e
10 changed files with 404 additions and 294 deletions

View File

@ -519,6 +519,18 @@ void UserDefault::flush()
{
}
void UserDefault::deleteValueForKey(const char* key)
{
// check the params
if (!key)
{
CCLOG("the key is invalid");
}
deleteValueForKeyJNI(key);
flush();
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

View File

@ -542,6 +542,18 @@ void UserDefault::flush()
[[NSUserDefaults standardUserDefaults] synchronize];
}
void UserDefault::deleteValueForKey(const char* key)
{
// check the params
if (!key)
{
CCLOG("the key is invalid");
}
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithUTF8String:key]];
flush();
}
NS_CC_END

View File

@ -329,6 +329,21 @@ void UserDefault::flush()
{
}
void UserDefault::deleteValueForKey(const char* key)
{
// check the params
if (!key)
{
CCLOG("the key is invalid");
}
ApplicationDataContainer^ localSettings = ApplicationData::Current->LocalSettings;
auto values = localSettings->Values;
values->Remove(PlatformStringFromString(key));
flush();
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)

View File

@ -503,6 +503,39 @@ void UserDefault::flush()
{
}
void UserDefault::deleteValueForKey(const char* key)
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the params
if (!key)
{
CCLOG("the key is invalid");
return;
}
// find the node
node = getXMLNodeForKey(key, &rootNode, &doc);
// if node not exist, don't need to delete
if (!node)
{
return;
}
// save file and free doc
if (doc)
{
doc->DeleteNode(node);
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
delete doc;
}
flush();
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID)

View File

@ -202,6 +202,13 @@ public:
*/
virtual void flush();
/**
* delete any value by key,
* @param key The key to delete value.
* @js NA
*/
virtual void deleteValueForKey(const char* key);
/** Returns the singleton.
* @js NA
* @lua NA
@ -238,7 +245,7 @@ public:
*/
static const std::string& getXMLFilePath();
/** All supported platforms other iOS & Android and CC_PLATFORM_WINRT use xml file to save values. This function checks whether the xml file exists or not.
* @return True if the xml file exists, flase if not.
* @return True if the xml file exists, false if not.
* @js NA
*/
static bool isXMLFileExist();

View File

@ -526,6 +526,13 @@ public class Cocos2dxHelper {
editor.commit();
}
public static void deleteValueForKey(String key) {
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove(key);
editor.commit();
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

View File

@ -400,3 +400,16 @@ void setStringForKeyJNI(const char* key, const char* value)
t.env->DeleteLocalRef(stringArg2);
}
}
void deleteValueForKeyJNI(const char* key)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "deleteValueForKey", "(Ljava/lang/String;)V")) {
jstring stringArg1 = t.env->NewStringUTF(key);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg1);
}
}

View File

@ -53,5 +53,5 @@ extern void setIntegerForKeyJNI(const char* key, int value);
extern void setFloatForKeyJNI(const char* key, float value);
extern void setDoubleForKeyJNI(const char* key, double value);
extern void setStringForKeyJNI(const char* key, const char* value);
extern void deleteValueForKeyJNI(const char* key);
#endif /* __Java_org_cocos2dx_lib_Cocos2dxHelper_H__ */

View File

@ -21,10 +21,16 @@ UserDefaultTests::UserDefaultTests()
UserDefaultTest::UserDefaultTest()
{
auto s = Director::getInstance()->getWinSize();
auto label = Label::createWithTTF("CCUserDefault test see log", "fonts/arial.ttf", 28);
auto label = Label::createWithTTF("CCUserDefault test Log data see console", "fonts/arial.ttf", 22);
addChild(label, 0);
label->setPosition( Vec2(s.width/2, s.height-50) );
this->_label = Label::createWithTTF("result", "fonts/arial.ttf", 12);
addChild(this->_label, 0);
label->setPosition(Vec2(s.width / 2, s.height - 50));
this->_label->setPosition(Vec2(s.width / 2, s.height / 2));
doTest();
}
@ -75,7 +81,7 @@ void setData2(const char* key)
void UserDefaultTest::doTest()
{
CCLOG("********************** init value ***********************");
this->_label->setString(this->_label->getString() + "\n" + "********************** init value ***********************");
// set default value
@ -90,29 +96,7 @@ void UserDefaultTest::doTest()
setData<float>("float_data");
setData<double>("double_data");
// print value
std::string ret = UserDefault::getInstance()->getStringForKey("string");
CCLOG("string is %s", ret.c_str());
double d = UserDefault::getInstance()->getDoubleForKey("double");
CCLOG("double is %f", d);
int i = UserDefault::getInstance()->getIntegerForKey("integer");
CCLOG("integer is %d", i);
float f = UserDefault::getInstance()->getFloatForKey("float");
CCLOG("float is %f", f);
bool b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
CCLOG("bool is true");
}
else
{
CCLOG("bool is false");
}
printValue();
logData<int>("int_data");
logData<float>("float_data");
@ -120,7 +104,7 @@ void UserDefaultTest::doTest()
//CCUserDefault::getInstance()->flush();
CCLOG("********************** after change value ***********************");
this->_label->setString(this->_label->getString() + "\n" + "********************** after change value ***********************");
// change the value
@ -137,35 +121,59 @@ void UserDefaultTest::doTest()
UserDefault::getInstance()->flush();
// print value
ret = UserDefault::getInstance()->getStringForKey("string");
CCLOG("string is %s", ret.c_str());
d = UserDefault::getInstance()->getDoubleForKey("double");
CCLOG("double is %f", d);
i = UserDefault::getInstance()->getIntegerForKey("integer");
CCLOG("integer is %d", i);
f = UserDefault::getInstance()->getFloatForKey("float");
CCLOG("float is %f", f);
b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
CCLOG("bool is true");
}
else
{
CCLOG("bool is false");
}
printValue();
logData<int>("int_data");
logData<float>("float_data");
logData<double>("double_data");
this->_label->setString(this->_label->getString() + "\n" + "********************** after delete value ***********************");
UserDefault::getInstance()->deleteValueForKey("string");
UserDefault::getInstance()->deleteValueForKey("integer");
UserDefault::getInstance()->deleteValueForKey("float");
UserDefault::getInstance()->deleteValueForKey("double");
UserDefault::getInstance()->deleteValueForKey("bool");
// print value
printValue();
}
void UserDefaultTest::printValue()
{
char strTemp[256] = "";
// print value
std::string ret = UserDefault::getInstance()->getStringForKey("string");
sprintf(strTemp, "string is %s", ret.c_str());
this->_label->setString(this->_label->getString() + "\n" + strTemp);
double d = UserDefault::getInstance()->getDoubleForKey("double");
sprintf(strTemp, "double is %f", d);
this->_label->setString(this->_label->getString() + "\n" + strTemp);
int i = UserDefault::getInstance()->getIntegerForKey("integer");
sprintf(strTemp, "integer is %d", i);
this->_label->setString(this->_label->getString() + "\n" + strTemp);
float f = UserDefault::getInstance()->getFloatForKey("float");
sprintf(strTemp, "float is %f", f);
this->_label->setString(this->_label->getString() + "\n" + strTemp);
bool b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
sprintf(strTemp, "bool is true");
this->_label->setString(this->_label->getString() + "\n" + strTemp);
}
else
{
sprintf(strTemp, "bool is false");
this->_label->setString(this->_label->getString() + "\n" + strTemp);
}
}
UserDefaultTest::~UserDefaultTest()
{
}

View File

@ -3,6 +3,7 @@
#include "cocos2d.h"
#include "../BaseTest.h"
#include "2d/CCLabel.h"
DEFINE_TEST_SUITE(UserDefaultTests);
@ -15,6 +16,8 @@ public:
private:
void doTest();
void printValue();
cocos2d::Label* _label;
};
#endif // _USERDEFAULT_TEST_H_