示例#1
0
        public async Task LoadCodes()
        {
            try
            {
                Status = "";

                if (CodeGroups != null)
                {
                    await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
                                                                         () =>
                    {
                        CodeGroups.Clear();
                    }));
                }


                await Task.Factory.StartNew(async() =>
                {
                    using (var conn = new SQLiteConnection(Connection.ConnString))
                    {
                        conn.Open();

                        using (var trns = conn.BeginTransaction())
                        {
                            using (var comm = new SQLiteCommand(conn))
                            {
                                comm.CommandText =
                                    "SELECT code_lib_groups.rowid, code_lib_groups.title, code_lib_groups.desc, code_lib_groups.desc_prev, code_lib_groups.star " +
                                    "FROM code_lib_groups " +
                                    "JOIN code_lib_langs ON code_lib_groups.langid = code_lib_langs.rowid " +
                                    "WHERE code_lib_groups.userid = (SELECT userid FROM users WHERE active) " +
                                    "AND code_lib_groups.langid = (SELECT active_langid FROM code_lib_settings WHERE active_langid)";

                                using (var reader = comm.ExecuteReader())
                                {
                                    if (reader.HasRows)
                                    {
                                        var i = 0;
                                        while (reader.Read())
                                        {
                                            var groupadd = new CodeGroups()
                                            {
                                                RowId    = Convert.ToInt64(reader["rowid"]),
                                                Title    = reader["title"].ToString(),
                                                Desc     = reader["desc"].ToString(),
                                                DescPrev = reader["desc_prev"].ToString(),
                                                Star     = Convert.ToBoolean(Convert.ToInt32(reader["star"] != null ? reader["star"].ToString() : "0")),
                                                Codes    = new ObservableCollection <Codes>()
                                            };

                                            await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
                                                                                                 () =>
                                            {
                                                CodeGroups.Add(groupadd);
                                            }));

                                            using (var commforcodes = new SQLiteCommand(conn))
                                            {
                                                commforcodes.CommandText =
                                                    "SELECT rowid,* FROM code_lib_codes " +
                                                    "WHERE code_lib_codes.userid = (SELECT userid FROM users WHERE active) " +
                                                    "AND code_lib_codes.groupid = '" + reader["rowid"] + "' " +
                                                    "ORDER BY code_lib_codes.date";

                                                using (var readerforcodes = commforcodes.ExecuteReader())
                                                {
                                                    if (readerforcodes.HasRows)
                                                    {
                                                        var icodecount = 0;
                                                        while (readerforcodes.Read())
                                                        {
                                                            icodecount++;

                                                            var codeadd = new Codes()
                                                            {
                                                                RowId       = Convert.ToInt64(readerforcodes["rowid"]),
                                                                OrderNum    = icodecount,
                                                                Title       = readerforcodes["title"].ToString(),
                                                                Desc        = readerforcodes["desc"].ToString(),
                                                                DescPrev    = readerforcodes["desc_prev"].ToString(),
                                                                GroupId     = Convert.ToInt64(readerforcodes["groupid"].ToString()),
                                                                Color       = readerforcodes["color"].ToString(),
                                                                InvertColor = readerforcodes["invert_color"].ToString(),
                                                            };

                                                            var ii = i;
                                                            await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
                                                                                                                 () =>
                                                            {
                                                                CodeGroups[ii].Codes.Add(codeadd);
                                                            }));
                                                        }
                                                    }
                                                }
                                            }

                                            i++;
                                        }
                                    }
                                }
                            }

                            trns.Commit();
                        }

                        SelectedCodeGroup = CodeGroups[0];

                        conn.Close();
                    }
                }).ContinueWith(async(T) =>
                {
                    await LoadColors();
                });
            }
            catch (Exception exp)
            {
                Debug.WriteLine(exp.ToString());
            }
        }
示例#2
0
        public async Task AddCode(object parameter)
        {
            try
            {
                Status = "";

                if (SelectedLang == null)
                {
                    Status = "Kod'un kaydedilebilmesi için bir dil seçilmelidir.";
                    return;
                }
                if (SelectedCodeGroup == null)
                {
                    Status = "Kod'un kaydedilebilmesi için bir grup seçilmelidir.";
                    return;
                }

                using (var conn = new SQLiteConnection(Connection.ConnString))
                {
                    conn.Open();

                    AddCodeIsEnabled = false;

                    using (var comm = new SQLiteCommand(conn))
                    {
                        comm.CommandText = "INSERT INTO code_lib_codes (userid, title, desc, desc_prev, code, code_prev, date, groupid, lenght, color, invert_color, langid) values(?,?,?,?,?,?,?,?,?,?,?,?)";

                        comm.Parameters.AddWithValue("@userid", User.GetUserId());
                        comm.Parameters.AddWithValue("@title", (string)parameter);
                        comm.Parameters.AddWithValue("@desc", !string.IsNullOrEmpty(AddCodeDesc) ? AddCodeDesc : string.Empty);
                        comm.Parameters.AddWithValue("@desc_prev", AddCodeDesc.Length >= 200 ? AddCodeDesc.Substring(0, 200) : AddCodeDesc);
                        comm.Parameters.AddWithValue("@code", !string.IsNullOrEmpty(AddCodeText) ? AddCodeText : string.Empty);
                        comm.Parameters.AddWithValue("@code_prev", AddCodeText.Length >= 300 ? AddCodeText.Substring(0, 300) : AddCodeText);
                        comm.Parameters.AddWithValue("@date", DateTime.Now.ToString("dd-MM-yyyy HH':'mm':'ss"));
                        comm.Parameters.AddWithValue("@groupid", SelectedCodeGroup.RowId);
                        comm.Parameters.AddWithValue("@lenght", !string.IsNullOrEmpty(AddCodeText) ? AddCodeText.Length : 0);
                        comm.Parameters.AddWithValue("@color", SelectedColor.HexCode != null ? SelectedColor.HexCode : "#FFE53935");
                        comm.Parameters.AddWithValue("@invert_color", SelectedColor.InverseColorHex_2 ?? "");
                        comm.Parameters.AddWithValue("@langid", SelectedLang.RowId);


                        var rows = comm.ExecuteNonQuery();
                        if (rows > 0)
                        {
                            var rowid   = conn.LastInsertRowId;
                            var newcode = new Codes()
                            {
                                RowId       = rowid,
                                Title       = (string)parameter,
                                Desc        = AddCodeDesc,
                                DescPrev    = AddCodeDesc.Length >= 200 ? AddCodeDesc.Substring(0, 200) : AddCodeDesc,
                                Code        = AddCodeText,
                                CodePrev    = AddCodeText.Length >= 300 ? AddCodeText.Substring(0, 300) : AddCodeText,
                                Date        = DateTime.Now.ToString("dd-MM-yyyy HH':'mm':'ss"),
                                GroupId     = SelectedCodeGroup.RowId,
                                Star        = false,
                                Lenght      = !string.IsNullOrEmpty(AddCodeText) ? AddCodeText.Length : 0,
                                Color       = SelectedColor.HexCode,
                                InvertColor = SelectedColor.InverseColorHex_2
                            };

                            var i = 0;
                            foreach (var group in CodeGroups)
                            {
                                var ii = i;
                                await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
                                                                                     () =>
                                {
                                    CodeGroups[ii].Codes.Add(newcode);
                                }));

                                i++;
                            }
                        }
                    }

                    AddCodeIsEnabled = true;
                }
            }
            catch (Exception exp)
            {
                AddCodeIsEnabled = true;

                Debug.WriteLine(exp.ToString());
            }
        }