public async Task <int> CreatePrompt(StoryPartViewModel prompt) { var story = new Story { StoryId = prompt.StoryId, StoryName = prompt.StoryName, Parts = new List <StoryPart>() }; var part = new StoryPart { PartId = prompt.PartId, CreatedDateTime = DateTime.Now, PartText = prompt.PartText, Author = prompt.Author, IsEnd = false, StoryId = prompt.StoryId }; _storyContext.Stories.Add(story); await _storyContext.SaveChangesAsync(); story.Parts.Add(part); part.PartNumber = story.Parts.Count; part.StoryName = story.StoryName; part.Story = story; _storyContext.StoryParts.Add(part); return(await _storyContext.SaveChangesAsync()); }
private void OnDestroy() { if (Instance == this) { Instance = null; } }
public MessageContainer getMessageContainer(StoryPart storyPart) { switch (storyPart) { case StoryPart.INTRODUCTION: return(introductionMC); default: Debug.Log("Неизвестная часть"); return(null); } }
private void Awake() { if (Instance == null) { Instance = this; } else { Destroy(this); } }
public bool TryAddStory(StoryPart story) { if (!story) { return(false); } if (!findInStories(story)) { stories.Add(story); Debug.LogFormat("Story '{0}' added to stories", story.index); Destroy(story.gameObject); } OnPropertyChanged("stories"); return(true); }
bool findInStories(StoryPart story) { bool _res = false; if (stories.Count == 0) { return(_res); } foreach (StoryPart _story in stories) { if (_story.index == story.index) { _res = true; break; } } return(_res); }
/// <summary> /// helper method to generate the story pages that belong to a parent story /// </summary> /// <param name="storyId"></param> /// <param name="listOfPages"></param> /// <returns></returns> private IEnumerable <StoryPart> GenerateStoryPages(Realm realmFile, string storyId, int[] endTimes, params string[] listOfPages) { var storyPages = new List <StoryPart>(); for (int i = 0; i < listOfPages.Length; i++) { var page = new StoryPart() { Image = listOfPages[i], Order = i + 1, StoryId = storyId, EndTimeInSeconds = endTimes[i], //set the start time to end time of the previous page plus 1 (this helps for faster sorting later) StartTimeInSeconds = i > 0 ? (endTimes[i - 1]) : 0 }; realmFile.Add <StoryPart>(page); storyPages.Add(page); } return(storyPages); }
public async Task <int> AddPart(StoryPartViewModel part) { var sp = new StoryPart { PartId = Guid.NewGuid(), CreatedDateTime = DateTime.Now, PartText = part.PartText, Author = part.Author, IsEnd = part.IsEnd, StoryId = part.StoryId, StoryName = part.StoryName }; var story = _storyContext.Stories.Find(part.StoryId); sp.Story = story; story.Parts.Add(sp); _storyContext.StoryParts.Add(sp); return(await _storyContext.SaveChangesAsync()); }
public StoryPage(Story story) { Story = story; StoryId = story.StoryId; StorySet = story.StorySetAsEnum; //pull the corresponding images out of the database var realmFile = Realm.GetInstance(RealmConfiguration.DefaultConfiguration); StoryPages = realmFile.All <RealmObjects.StoryPart>().Where(x => x.StoryId.Equals(story.StoryId)) .OrderBy(x => x.Order).ToList(); //get the current user's ID - if we ever want multiple users per device, we'll have to store the user's id in RAM var userId = realmFile.All <User>().FirstOrDefault().UserId; //init user story transaction using (var writer = realmFile.BeginWrite()) { UserStoryTransaction = new UserStoryReads(); UserStoryTransaction.StoryId = StoryId; UserStoryTransaction.UserId = userId; UserStoryTransaction.StartReadTime = DateTime.UtcNow; //add to the db realmFile.Add <UserStoryReads>(UserStoryTransaction); writer.Commit(); } InitializeComponent(); PlayButton.Source = PAUSE_ICON; //set at the pause icon because this page auto-starts PlayButton.HeightRequest = 40; PlayButton.WidthRequest = 50; PlayButton.BorderColor = Color.Transparent; PlayButton.BackgroundColor = Color.Transparent; PlayButton.Margin = 20; QuizButton.Source = "Quizzes.png"; QuizButton.BackgroundColor = Color.Green; QuizButton.IsVisible = false; DurationLabel.Text = "0:00"; DurationLabel.FontFamily = Device.RuntimePlatform == Device.Android ? "Comic.ttf#Comic" : "Comic"; DurationLabel.Margin = 20; CurrentStoryPage = StoryPages.First(); //story content StoryImage.Source = CurrentStoryPage.Image; StoryImage.MinimumWidthRequest = DeviceDisplay.MainDisplayInfo.Width; StoryImage.Aspect = Aspect.AspectFit; player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current; player.Load(story.AudioClip); //find the story duration if we haven't already if (story.DurationInSeconds <= 0) { using (var transaction = realmFile.BeginWrite()) { story.DurationInSeconds = player.Duration; transaction.Commit(); } } //kill the realm file to help with memory consumption realmFile.Dispose(); realmFile = null; //slider init StoryPageSlider.Maximum = story.DurationInSeconds; StoryPageSlider.Minimum = 0; StoryPageSlider.Value = 0; StoryPageSlider.HorizontalOptions = LayoutOptions.FillAndExpand; //StoryPageSlider.MinimumWidthRequest = DeviceDisplay.MainDisplayInfo.Width - (PlayButton.Width * 4); StoryPageSlider.HeightRequest = 50; // Controls size of area that can grab the slider //use drag completed instead of value changed to avoid "stuttering" audio StoryPageSlider.DragCompleted += UserDraggedSlider; //register action to be taken once the story ends player.PlaybackEnded += EndPlayback; player.Loop = false; //start the player in a different thread var playerThread = new Thread(new ThreadStart(() => { //this starts the audio player.Play(); })); playerThread.Start(); PlayButton.Clicked += (sender, args) => { //toggle play/pause if (player.IsPlaying) { player.Pause(); PlayButton.Source = PLAY_ICON; } else { player.Play(); PlayButton.Source = PAUSE_ICON; } }; RefreshStoryPagesTimer(); QuizButton.Clicked += (sender, args) => { //Navigation.PushAsync(new QuizPage(story.Quizzes[quizNum], story.AudioClip)); }; }
private void RefreshStoryPagesTimer() { Device.BeginInvokeOnMainThread(() => { //once the desired story % is read, mark this boolean true bool storyMarkedRead = false; //timer to track story progress and swap pages Device.StartTimer(new TimeSpan(0, 0, 1), () => { if (player != null) { var audioPosition = player.CurrentPosition; var progressionTime = new TimeSpan(0, 0, (int)audioPosition); //check if the audio position has moved forward or backward - then see if we need to make sure the image should progress or regress if (audioPosition > PreviousTime) { //after 90% of the story has been read, mark as read if not marked as read already if (!storyMarkedRead && audioPosition >= (Story.DurationInSeconds * COMPLETION_THRESHOLD)) { MarkAsRead(); storyMarkedRead = true; } //if the page has been completed, go to the next one else if (audioPosition >= CurrentStoryPage.EndTimeInSeconds) { //progress to the next story page CurrentStoryPage = StoryPages.Where(x => x.EndTimeInSeconds >= audioPosition && x.StartTimeInSeconds <= audioPosition).FirstOrDefault(); StoryImage.Source = CurrentStoryPage.Image; } } else if (audioPosition < PreviousTime) { //if the current position is less than the previous one - the user has moved the slider backwards var pageEndTime = -1; int pageNumber = 1; StoryPart currentPage; //loop through the ordered storypages list until we get the first one where the end time is after our current time do { currentPage = StoryPages.Where(x => x.Order == pageNumber).FirstOrDefault(); pageEndTime = currentPage.EndTimeInSeconds; pageNumber++; } while (pageEndTime <= audioPosition); CurrentStoryPage = currentPage; StoryImage.Source = CurrentStoryPage.Image; } StoryPageSlider.Value = audioPosition; //update the timestamp text DurationLabel.Text = String.Format("{0}:{1}", progressionTime.Minutes, progressionTime.Seconds.ToString("D2")); //log the previous time PreviousTime = audioPosition; return(true); } else { //stop the timer when the player object is null return(false); } }); }); }
public void RemoveStory(StoryPart story) { stories.Remove(story); OnPropertyChanged("stories"); }