Click here to Skip to main content
15,885,309 members
Home / Discussions / C#
   

C#

 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 1:13
professionalEddy Vluggen9-Mar-20 1:13 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 1:23
Exoskeletor9-Mar-20 1:23 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 1:33
Exoskeletor9-Mar-20 1:33 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 2:26
professionalEddy Vluggen9-Mar-20 2:26 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 2:38
Exoskeletor9-Mar-20 2:38 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 2:41
professionalEddy Vluggen9-Mar-20 2:41 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 3:02
Exoskeletor9-Mar-20 3:02 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 3:51
Exoskeletor9-Mar-20 3:51 
I tried with this code but im getting an out of memory exception

static class DatabaseHelper
    {
        public static string GetMD5Hash(string content)
        {
            using (var md5 = MD5.Create())
            {
                byte[] computedHash = md5.ComputeHash(Encoding.UTF8.GetBytes(content));
                return new System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary(computedHash).ToString();
            }
        }

        public static string GetMD5Hash(byte[] content)
        {
            using (var md5 = MD5.Create())
            {
                byte[] computedHash = md5.ComputeHash(Encoding.UTF8.GetBytes(BitConverter.ToString(content).Replace("-", "")));
                return new System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary(computedHash).ToString();
            }
        }
        public static string DrawableToMD5(int drawableId)
        {
            var templateDB = new TemplateDB();
            return GetMD5Hash(templateDB.DrawableToByteArray(drawableId));
        }
        private static SQLiteConnection instance;
        public static SQLiteConnection db()
        {
            if (instance == null)
                instance = new SQLiteConnection(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TemplatesData.db"));

            return instance;
        }
        public static void CloseConnection()
        {
            if (instance != null)
            {
                instance.Close();
                instance.Dispose();
                instance = null;
            }
        }

    }
    public class TemplateDB
    {
        public enum TemplateCategories
        {
            Emojis,
            Stars,
            Hearts,
            Characters,
            Emotions,
        }

        public static byte[] ConcatByteArrays(params byte[][] arrays)
        {
            return arrays.SelectMany(x => x).ToArray();
        }

        public static byte[] ConcatByteList(List<byte[]> list)
        {
            return list
                .SelectMany(a => a)
                .ToArray();
        }

        public static Bitmap GetBitmapFromVectorDrawable(Context context, int drawableId)
        {
            Drawable drawable = ContextCompat.GetDrawable(context, drawableId);
            if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop)
            {
                drawable = (DrawableCompat.Wrap(drawable)).Mutate();
            }

            Bitmap bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth,
                    drawable.IntrinsicWidth, Bitmap.Config.Argb8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
            drawable.Draw(canvas);

            return bitmap;
        }

        public byte[] DrawableToByteArray(int resourceId)
        {
            var context = AppState.ApplicationState;
            using (var bitmap = GetBitmapFromVectorDrawable(context, resourceId))
            {
                int size = bitmap.ByteCount;
                byte[] byteArray = new byte[size];
                ByteBuffer byteBuffer = ByteBuffer.Allocate(size);
                bitmap.CopyPixelsToBuffer(byteBuffer);
                byteBuffer.Rewind();
                byteBuffer.Get(byteArray);
                return byteArray;
            }
        }
        public static void AddTemplate(int category, List<int> images)
        {
            var templateDB = new TemplateDB();
            var imageByteList = new List<byte[]>();

            foreach (int image in images)
            {
                imageByteList.Add(templateDB.DrawableToByteArray(image));
            }
            var tmpl = new Template()
            {
                Category = category,
            };
            var img1 = new TemplateImage()
            {
                Category = category,
                Image = images[0],
                ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[0]),
            };
            var img2 = new TemplateImage()
            {
                Category = category,
                Image = images[1],
                ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[1]),
            };
            var img3 = new TemplateImage()
            {
                Category = category,
                Image = images[2],
                ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[2]),
            };
            var img4 = new TemplateImage()
            {
                Category = category,
                Image = images[3],
                ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[3]),
            };
            var img5 = new TemplateImage()
            {
                Category = category,
                Image = images[4],
                ImagesHash = DatabaseHelper.GetMD5Hash(imageByteList[4]),
            };
            tmpl.TemplateImages = new List<TemplateImage>() { img1, img2, img3, img4, img5 };
            tmpl.ImagesHash = DatabaseHelper.GetMD5Hash(ConcatByteList(imageByteList));
            var result = DatabaseHelper.db().Query<TemplateImage>("Select * from Templates where ImagesHash=?", tmpl.ImagesHash);
            if (result.Count == 0)
            {
            DatabaseHelper.db().InsertAll(tmpl.TemplateImages);
            DatabaseHelper.db().Insert(tmpl);
            DatabaseHelper.db().UpdateWithChildren(tmpl);
            }
            //var some = new List<Template>();
            //some = db.Table<Template>().Where(record => record.Images == images).ToList();
            //var alreadyExist = db.Query<Templates>("select * from Templates where Images = ?", images);
            //var notFound = alreadyExist.ToList();
            //if (some?.Count() == 0)
            //{
            // var template = new Template()
            //{
            //    Category = category,
            //    Images = images,
            //};
            //db.InsertWithChildren(template, recursive: true);
            // }
        }

GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 4:05
Exoskeletor9-Mar-20 4:05 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 4:11
professionalEddy Vluggen9-Mar-20 4:11 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 4:28
Exoskeletor9-Mar-20 4:28 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 4:44
professionalEddy Vluggen9-Mar-20 4:44 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 4:47
Exoskeletor9-Mar-20 4:47 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 4:51
professionalEddy Vluggen9-Mar-20 4:51 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 5:00
Exoskeletor9-Mar-20 5:00 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 5:28
Exoskeletor9-Mar-20 5:28 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 9:00
professionalEddy Vluggen9-Mar-20 9:00 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 9:03
Exoskeletor9-Mar-20 9:03 
QuestionRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 9:13
professionalEddy Vluggen9-Mar-20 9:13 
AnswerRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 9:16
Exoskeletor9-Mar-20 9:16 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 9:53
professionalEddy Vluggen9-Mar-20 9:53 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 8:58
professionalEddy Vluggen9-Mar-20 8:58 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 4:14
professionalEddy Vluggen9-Mar-20 4:14 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 9:09
Exoskeletor9-Mar-20 9:09 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 9:15
professionalEddy Vluggen9-Mar-20 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.