Ejemplo n.º 1
0
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            // Inflate the CardView for the task:
            View itemView;

            switch (viewType)
            {
            case TaskScan:
            case TaskListen:
            case TaskLocationHunt:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_Btn, parent, false);
                TaskViewHolderBtn bvh = new TaskViewHolderBtn(itemView, OnSpeakClick, OnClick);
                return(bvh);

            case TaskInfo:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_Info, parent, false);
                TaskViewHolderInfo ivh = new TaskViewHolderInfo(itemView, OnSpeakClick, OnClick);
                ivh.Button.Text = context.GetString(Resource.String.ExternalBtn);
                return(ivh);

            case TaskDrawing:
            case TaskPhoto:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_ResultList, parent, false);
                TaskViewHolderResultList pvh = new TaskViewHolderResultList(itemView, OnSpeakClick, OnClick, OnMediaClick);
                return(pvh);

            case TaskMultipleChoice:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_MultipleChoice, parent, false);
                TaskViewHolderMultipleChoice vmh = new TaskViewHolderMultipleChoice(itemView, OnSpeakClick);
                return(vmh);

            case TaskLocationMarker:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_Map, parent, false);
                TaskViewHolderMap mvh = new TaskViewHolderMap(itemView, OnSpeakClick, OnClick);
                return(mvh);

            case TaskTextEntry:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_TextEntry, parent, false);
                TaskViewHolderTextEntry tvh = new TaskViewHolderTextEntry(itemView, OnSpeakClick, OnText);
                return(tvh);

            case TaskVideo:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_ResultList, parent, false);
                TaskViewHolderRecordVideo vvh = new TaskViewHolderRecordVideo(itemView, OnSpeakClick, OnClick, OnMediaClick);
                return(vvh);

            case TaskAudio:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_ResultList, parent, false);
                TaskViewHolderRecordAudio avh = new TaskViewHolderRecordAudio(itemView, OnSpeakClick, OnClick, OnMediaClick);
                return(avh);

            case Finish:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_Finish, parent, false);
                ButtonViewHolder finishViewHolder = new ButtonViewHolder(itemView, OnClick);
                return(finishViewHolder);

            case Curate:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.CuratorCard, parent, false);
                CuratorViewHolder curateCard = new CuratorViewHolder(itemView, OnCurate);
                return(curateCard);

            case Names:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard_Name, parent, false);
                TaskViewHolderName nameCard = new TaskViewHolderName(itemView, OnSpeakClick, OnChangeNameClick);
                return(nameCard);

            default:
                itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.TaskCard, parent, false);
                TaskViewHolder vh = new TaskViewHolder(itemView, null);
                return(vh);
            }
        }
Ejemplo n.º 2
0
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            TaskViewHolder vh;

            Type thisType = holder.GetType();

            OnBind = true;

            if (Curator)
            {
                if (position == 0)
                {
                    return;
                }
            }

            if (position == 0 || (Curator && position == 1))
            {
                // Activity description + entered names (if required)
                vh = holder as TaskViewHolderName;
                if (vh == null)
                {
                    return;
                }

                vh.Title.Text       = description;
                vh.Description.Text = context.Resources.GetString(Resource.String.TasksTitle);

                ((TaskViewHolderName)vh).NameSection.Visibility =
                    (reqName) ? ViewStates.Visible : ViewStates.Gone;
                ((TaskViewHolderName)vh).EnteredNames.Text = names;

                return;
            }

            if (position == Items.Count - 1)
            {
                // Finish button
                if (holder is ButtonViewHolder bvh)
                {
                    bvh.Button.Enabled = true;
                }

                return;
            }

            string taskType = Items[position].TaskType.IdName;

            if (thisType == typeof(TaskViewHolderInfo))
            {
                AdditionalInfoData taskInfo = JsonConvert.DeserializeObject <AdditionalInfoData>(Items[position].JsonData);
                vh = holder as TaskViewHolderInfo;
                Items[position].IsCompleted = true;

                if (!string.IsNullOrWhiteSpace(taskInfo.ImageUrl))
                {
                    Items[position].ImageUrl = taskInfo.ImageUrl;
                }

                TaskViewHolderInfo taskViewHolderInfo = (TaskViewHolderInfo)vh;
                if (taskViewHolderInfo != null)
                {
                    taskViewHolderInfo.Button.Visibility =
                        (string.IsNullOrWhiteSpace(taskInfo.ExternalUrl)) ? ViewStates.Gone : ViewStates.Visible;
                }
            }
            else if (thisType == typeof(TaskViewHolderRecordAudio))
            {
                vh = holder as TaskViewHolderRecordAudio;
                if (vh != null)
                {
                    ((TaskViewHolderRecordAudio)vh).StartTaskButton.Text =
                        context.Resources.GetString(Resource.String.StartBtn);

                    if (!string.IsNullOrWhiteSpace(Items[position].CompletionData.JsonData))
                    {
                        List <string> audioPaths =
                            JsonConvert.DeserializeObject <List <string> >(Items[position].CompletionData.JsonData);

                        ((TaskViewHolderRecordAudio)vh).ShowResults(audioPaths, context);
                        Items[position].IsCompleted = true;
                    }
                    else
                    {
                        Items[position].IsCompleted = false;
                        ((TaskViewHolderRecordAudio)vh).ClearResults();
                    }
                }
            }
            else if (thisType == typeof(TaskViewHolderRecordVideo))
            {
                vh = holder as TaskViewHolderRecordVideo;
                if (vh != null)
                {
                    ((TaskViewHolderRecordVideo)vh).StartTaskButton.Text =
                        context.Resources.GetString(Resource.String.RecBtn);

                    if (!string.IsNullOrWhiteSpace(Items[position].CompletionData.JsonData))
                    {
                        List <string> videoPaths =
                            JsonConvert.DeserializeObject <List <string> >(Items[position].CompletionData.JsonData);

                        ((TaskViewHolderRecordVideo)vh).ShowResults(videoPaths, context);
                        Items[position].IsCompleted = true;
                    }
                    else
                    {
                        Items[position].IsCompleted = false;
                        ((TaskViewHolderRecordVideo)vh).ClearResults();
                    }
                }
            }
            else if (thisType == typeof(TaskViewHolderResultList))
            {
                vh = holder as TaskViewHolderResultList;

                bool   btnEnabled = true;
                string btnText;

                if (taskType == "DRAW" || taskType == "DRAW_PHOTO")
                {
                    btnText = context.Resources.GetString(Resource.String.StartDrawBtn);

                    if (taskType == "DRAW_PHOTO")
                    {
                        btnEnabled = !int.TryParse(Items[position].JsonData, out var idResult) || GetTaskWithId(idResult).IsCompleted;

                        if (!btnEnabled)
                        {
                            btnText = context.Resources.GetString(Resource.String.TaskBtnNotReady);
                        }
                    }
                }
                else
                {
                    btnText = context.Resources.GetString(Resource.String.TakePhotoBtn);
                }

                if (vh != null)
                {
                    ((TaskViewHolderResultList)vh).StartTaskButton.Text    = btnText;
                    ((TaskViewHolderResultList)vh).StartTaskButton.Enabled = btnEnabled;

                    if (!string.IsNullOrWhiteSpace(Items[position].CompletionData.JsonData))
                    {
                        List <string> photoPaths =
                            JsonConvert.DeserializeObject <List <string> >(Items[position].CompletionData.JsonData);

                        ((TaskViewHolderResultList)vh).ShowResults(photoPaths, context);
                        Items[position].IsCompleted = true;
                    }
                    else
                    {
                        Items[position].IsCompleted = false;
                        ((TaskViewHolderResultList)vh).ClearResults();
                    }
                }
            }
            else if (thisType == typeof(TaskViewHolderBtn))
            {
                vh = holder as TaskViewHolderBtn;
                string btnText = context.Resources.GetString(Resource.String.TaskBtn);

                if (taskType == "LISTEN_AUDIO")
                {
                    btnText = context.Resources.GetString(Resource.String.ListenBtn);
                }

                TaskViewHolderBtn viewHolderBtn = (TaskViewHolderBtn)vh;
                if (viewHolderBtn != null)
                {
                    viewHolderBtn.Button.Text = btnText;
                }
            }
            else if (thisType == typeof(TaskViewHolderTextEntry))
            {
                vh = holder as TaskViewHolderTextEntry;
                if (vh != null)
                {
                    ((TaskViewHolderTextEntry)vh).TextField.Text = Items[position].CompletionData.JsonData;
                    Items[position].IsCompleted =
                        !string.IsNullOrWhiteSpace(((TaskViewHolderTextEntry)vh).TextField.Text);
                }
            }
            else
            {
                if (thisType == typeof(TaskViewHolderMultipleChoice))
                {
                    vh = holder as TaskViewHolderMultipleChoice;
                    RadioGroup radios = ((TaskViewHolderMultipleChoice)vh)?.RadioGroup;

                    string[] choices = JsonConvert.DeserializeObject <string[]>(Items[position].JsonData);
                    int.TryParse(Items[position].CompletionData.JsonData, out var answeredInd);

                    if (radios != null && radios.ChildCount == 0)
                    {
                        int index = 0;
                        foreach (string option in choices)
                        {
                            RadioButton rad = new RadioButton(context)
                            {
                                Text = option
                            };
                            rad.SetPadding(0, 0, 0, 5);
                            rad.TextSize = 16;
                            radios.AddView(rad);

                            if (Items[position].IsCompleted && answeredInd == index)
                            {
                                ((RadioButton)radios.GetChildAt(answeredInd)).Checked = true;
                            }
                            index++;
                        }
                    }

                    if (answeredInd == -1)
                    {
                        Items[position].IsCompleted = false;
                    }

                    if (radios != null)
                    {
                        radios.CheckedChange += (sender, e) =>
                        {
                            Items[position].IsCompleted = true;
                            int  radioButtonId = radios.CheckedRadioButtonId;
                            View radioButton   = radios.FindViewById(radioButtonId);
                            int  idx           = radios.IndexOfChild(radioButton);
                            Items[position].CompletionData.JsonData = idx.ToString();
                            NotifyItemChanged(Items.Count - 1);
                        };
                    }
                }
                else if (thisType == typeof(TaskViewHolderMap))
                {
                    vh = holder as TaskViewHolderMap;
                    TaskViewHolderMap mapHolder = ((TaskViewHolderMap)vh);

                    if (taskType == "MAP_MARK")
                    {
                        List <Map_Location> points = JsonConvert.DeserializeObject <List <Map_Location> >(Items[position].CompletionData.JsonData);

                        if (points != null && points.Count > 0)
                        {
                            if (mapHolder != null)
                            {
                                mapHolder.EnteredLocationsView.Visibility = ViewStates.Visible;
                                mapHolder.EnteredLocationsView.Text       = string.Format(
                                    context.Resources.GetString(Resource.String.ChosenLocations),
                                    points.Count,
                                    (points.Count > 1) ? "s" : "");
                            }

                            Items[position].IsCompleted = true;
                        }
                        else
                        {
                            if (mapHolder != null)
                            {
                                mapHolder.EnteredLocationsView.Visibility = ViewStates.Gone;
                            }

                            Items[position].IsCompleted = false;
                        }
                    }
                }
                else
                {
                    vh = holder as TaskViewHolder;
                }
            }

            // These apply to all task types:
            AndroidUtils.LoadTaskTypeIcon(Items[position].TaskType, ((TaskViewHolder)holder).TaskTypeIcon);

            if (!string.IsNullOrWhiteSpace(Items[position].ImageUrl))
            {
                AndroidUtils.LoadActivityImageIntoView(vh.TaskImage, Items[position].ImageUrl, activityId, 350);
            }
            else
            {
                if (vh != null)
                {
                    vh.TaskImage.Visibility = ViewStates.Gone;
                }
            }

            vh.Description.Text = Items[position].Description;
            vh.Title.Text       = Items[position].TaskType.DisplayName;

            bool hasChildren = Items[position].ChildTasks != null && Items[position].ChildTasks.Any();

            if (Items[position].TaskType.IdName == "ENTER_TEXT")
            {
                vh.LockedChildrenTease.Visibility = ViewStates.Gone;
            }
            else if (hasChildren && !Items[position].IsCompleted)
            {
                vh.LockedChildrenTease.Visibility = ViewStates.Visible;
                int childCount = Items[position].ChildTasks.Count();
                vh.LockedChildrenTease.Text = string.Format(
                    context.GetString(Resource.String.taskLockedParent),
                    childCount,
                    (childCount > 1) ? "s" : "");
            }
            else if (hasChildren && Items[position].IsCompleted)
            {
                vh.LockedChildrenTease.Visibility = ViewStates.Visible;
                vh.LockedChildrenTease.Text       = context.GetString(Resource.String.taskUnlockedParent);
            }
            else
            {
                vh.LockedChildrenTease.Visibility = ViewStates.Gone;
            }

            OnBind = false;
        }