lottie: added new loadfromdata() api in lottie player. 78/188378/1
authorsubhransu mohanty <sub.mohanty@samsung.com>
Tue, 4 Sep 2018 09:41:47 +0000 (18:41 +0900)
committersubhransu mohanty <sub.mohanty@samsung.com>
Tue, 4 Sep 2018 09:41:47 +0000 (18:41 +0900)
Change-Id: Iadc6e57953190bdb860415cb3a4d4d02a6ac3cf6

inc/lotplayer.h
src/lottie/lottieloader.cpp
src/lottie/lottieloader.h
src/lottie/lottieplayer.cpp

index 0aab90d..a225478 100644 (file)
@@ -18,6 +18,8 @@ public:
     ~LOTPlayer();
     LOTPlayer();
 
+    bool loadFromData(const char *jsonData, const char *key);
+
     bool setFilePath(const char *filePath);
 
     float playTime() const;
index e819e3c..289876f 100644 (file)
@@ -1,4 +1,4 @@
-#include "lottieloader.h"
+#include "lottieloader.h"
 #include "lottieparser.h"
 
 #include <fstream>
@@ -14,7 +14,7 @@ public:
 
         return CACHE;
     }
-    std::shared_ptr<LOTModel> find(std::string &key);
+    std::shared_ptr<LOTModel> find(const std::string &key);
     void add(std::string &key, std::shared_ptr<LOTModel> value);
 
 private:
@@ -23,7 +23,7 @@ private:
     std::unordered_map<std::string, std::shared_ptr<LOTModel>> mHash;
 };
 
-std::shared_ptr<LOTModel> LottieFileCache::find(std::string &key)
+std::shared_ptr<LOTModel> LottieFileCache::find(const std::string &key)
 {
     auto search = mHash.find(key);
     if (search != mHash.end()) {
@@ -65,6 +65,21 @@ bool LottieLoader::load(std::string &path)
     return true;
 }
 
+bool LottieLoader::loadFromData(const char *jsonData, const char *key)
+{
+    LottieFileCache &fileCache = LottieFileCache::get();
+    std::string keyString(key);
+
+    mModel = fileCache.find(std::string(keyString));
+    if (mModel) return true;
+
+    LottieParser parser(const_cast<char *>(jsonData));
+    mModel = parser.model();
+    fileCache.add(keyString, mModel);
+
+    return true;
+}
+
 std::shared_ptr<LOTModel> LottieLoader::model()
 {
     return mModel;
index e87cd79..32ec5aa 100644 (file)
@@ -9,6 +9,7 @@ class LottieLoader
 {
 public:
    bool load(std::string &filePath);
+   bool loadFromData(const char *jsonData, const char *key);
    std::shared_ptr<LOTModel> model();
 private:
    std::shared_ptr<LOTModel>    mModel;
index 089e28a..551f56a 100644 (file)
@@ -14,6 +14,8 @@ public:
     LOTPlayerPrivate();
     bool                          update(float pos);
     bool                          setFilePath(std::string path);
+    bool                          loadFromData(const char *jsonData,
+                                               const char *key);
     void                          setSize(const VSize &sz);
     void                          setPos(float pos);
     VSize                         size() const;
@@ -151,6 +153,22 @@ bool LOTPlayerPrivate::setFilePath(std::string path)
     return false;
 }
 
+bool LOTPlayerPrivate::loadFromData(const char *jsonData, const char *key)
+{
+    if (!jsonData) {
+        vWarning << "jason data is empty";
+        return false;
+    }
+
+    LottieLoader loader;
+    if (loader.loadFromData(jsonData, key)) {
+        mModel = loader.model();
+        mCompItem = std::make_unique<LOTCompItem>(mModel.get());
+        return true;
+    }
+    return false;
+}
+
 /*
  * Implement a task stealing schduler to perform render task
  * As each player draws into its own buffer we can delegate this
@@ -250,6 +268,11 @@ LOTPlayer::~LOTPlayer()
  * @param path  add the details
  */
 
+bool LOTPlayer::loadFromData(const char *jsonData, const char *key)
+{
+    return d->loadFromData(jsonData, key);
+}
+
 bool LOTPlayer::setFilePath(const char *filePath)
 {
     return d->setFilePath(filePath);