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

@ -57,57 +57,57 @@ static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLEle
do
{
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
*doc = xmlDoc;
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
if (xmlBuffer.empty())
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
if (xmlBuffer.empty())
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size());
// get root node
*rootNode = xmlDoc->RootElement();
if (nullptr == *rootNode)
{
CCLOG("read root node error");
break;
}
// find the node
curNode = (*rootNode)->FirstChildElement();
while (nullptr != curNode)
{
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
break;
}
// get root node
*rootNode = xmlDoc->RootElement();
if (nullptr == *rootNode)
{
CCLOG("read root node error");
break;
}
// find the node
curNode = (*rootNode)->FirstChildElement();
while (nullptr != curNode)
{
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
break;
}
curNode = curNode->NextSiblingElement();
}
} while (0);
curNode = curNode->NextSiblingElement();
}
} while (0);
return curNode;
return curNode;
}
static void setValueForKey(const char* pKey, const char* pValue)
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the params
if (! pKey || ! pValue)
{
return;
}
// find the node
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the content
if (node)
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
// check the params
if (! pKey || ! pValue)
{
return;
}
// find the node
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// if node exist, change the content
if (node)
{
if (node->FirstChild())
{
node->FirstChild()->SetValue(pValue);
@ -117,24 +117,24 @@ static void setValueForKey(const char* pKey, const char* pValue)
tinyxml2::XMLText* content = doc->NewText(pValue);
node->LinkEndChild(content);
}
}
else
{
if (rootNode)
{
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
tmpNode->LinkEndChild(content);
}
}
}
else
{
if (rootNode)
{
tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
rootNode->LinkEndChild(tmpNode);
tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
tmpNode->LinkEndChild(content);
}
}
// save file and free doc
if (doc)
{
if (doc)
{
doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
delete doc;
}
delete doc;
}
}
/**
@ -161,26 +161,26 @@ bool UserDefault::getBoolForKey(const char* pKey)
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
value = (const char*)(node->FirstChild()->Value());
}
}
bool ret = defaultValue;
bool ret = defaultValue;
if (value)
{
ret = (! strcmp(value, "true"));
}
if (value)
{
ret = (! strcmp(value, "true"));
}
if (doc) delete doc;
return ret;
return ret;
}
int UserDefault::getIntegerForKey(const char* pKey)
@ -190,31 +190,31 @@ int UserDefault::getIntegerForKey(const char* pKey)
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
value = (const char*)(node->FirstChild()->Value());
}
}
int ret = defaultValue;
int ret = defaultValue;
if (value)
{
ret = atoi(value);
}
if (value)
{
ret = atoi(value);
}
if(doc)
{
delete doc;
}
if(doc)
{
delete doc;
}
return ret;
return ret;
}
float UserDefault::getFloatForKey(const char* pKey)
@ -236,27 +236,27 @@ double UserDefault::getDoubleForKey(const char* pKey)
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
value = (const char*)(node->FirstChild()->Value());
}
}
double ret = defaultValue;
double ret = defaultValue;
if (value)
{
ret = utils::atof(value);
}
if (value)
{
ret = utils::atof(value);
}
if (doc) delete doc;
return ret;
return ret;
}
std::string UserDefault::getStringForKey(const char* pKey)
@ -267,26 +267,26 @@ std::string UserDefault::getStringForKey(const char* pKey)
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
const char* value = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
value = (const char*)(node->FirstChild()->Value());
}
}
string ret = defaultValue;
string ret = defaultValue;
if (value)
{
ret = string(value);
}
if (value)
{
ret = string(value);
}
if (doc) delete doc;
return ret;
return ret;
}
Data UserDefault::getDataForKey(const char* pKey)
@ -297,31 +297,31 @@ Data UserDefault::getDataForKey(const char* pKey)
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
{
const char* encodedData = nullptr;
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
tinyxml2::XMLElement* rootNode;
tinyxml2::XMLDocument* doc;
tinyxml2::XMLElement* node;
node = getXMLNodeForKey(pKey, &rootNode, &doc);
// find the node
if (node && node->FirstChild())
{
encodedData = (const char*)(node->FirstChild()->Value());
}
}
Data ret = defaultValue;
Data ret = defaultValue;
if (encodedData)
{
if (encodedData)
{
unsigned char * decodedData = nullptr;
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
if (decodedData) {
ret.fastSet(decodedData, decodedDataLen);
}
}
}
if (doc) delete doc;
return ret;
return ret;
}
@ -466,32 +466,32 @@ void UserDefault::initXMLFilePath()
// create new xml file
bool UserDefault::createXMLFile()
{
bool bRet = false;
bool bRet = false;
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
if (nullptr==pDoc)
{
return false;
}
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
if (nullptr==pDeclaration)
{
return false;
}
pDoc->LinkEndChild(pDeclaration);
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
if (nullptr==pRootEle)
{
return false;
}
pDoc->LinkEndChild(pRootEle);
tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr);
if (nullptr==pDeclaration)
{
return false;
}
pDoc->LinkEndChild(pDeclaration);
tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME);
if (nullptr==pRootEle)
{
return false;
}
pDoc->LinkEndChild(pRootEle);
bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(_filePath).c_str());
if(pDoc)
{
delete pDoc;
}
if(pDoc)
{
delete pDoc;
}
return bRet;
return bRet;
}
const string& UserDefault::getXMLFilePath()
@ -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_