1 /**
2  * @file CloudDriveViewModel.cs
3  * @brief Class for the cloud drive view model.
4  *
5  * (c) 2013-2014 by Mega Limited, Auckland, New Zealand
6  *
7  * This file is part of the MEGA SDK - Client Access Engine.
8  *
9  * Applications using the MEGA API must present a valid application key
10  * and comply with the the rules set forth in the Terms of Service.
11  *
12  * The MEGA SDK is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * @copyright Simplified (2-clause) BSD License.
17  *
18  * You should have received a copy of the license along with this
19  * program.
20  */
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Collections.ObjectModel;
25 using System.Collections.Specialized;
26 using System.ComponentModel;
27 using System.Linq;
28 using System.Text;
29 using System.Threading.Tasks;
30 using System.Windows;
31 using System.Windows.Input;
32 using mega;
33 using MegaApp.Classes;
34 using MegaApp.MegaApi;
35 using MegaApp.Pages;
36 using MegaApp.Resources;
37 using MegaApp.Services;
38 
39 namespace MegaApp.Models
40 {
41     public class CloudDriveViewModel : BaseViewModel
42     {
43         private readonly MegaSDK _megaSdk;
44 
CloudDriveViewModel(MegaSDK megaSdk)45         public CloudDriveViewModel(MegaSDK megaSdk)
46         {
47             this._megaSdk = megaSdk;
48             this.CurrentRootNode = null;
49             this.ChildNodes = new ObservableCollection<NodeViewModel>();
50         }
51 
52         #region Commands
53 
54         public ICommand RemoveItemCommand { get; set; }
55         public ICommand MoveItemCommand { get; set; }
56         public ICommand GetPreviewLinkItemCommand { get; set; }
57         public ICommand DownloadItemCommand { get; set; }
58 
59         #endregion
60 
61         #region Public Methods
62 
GoFolderUp()63         public void GoFolderUp()
64         {
65             MNode parentNode = this._megaSdk.getParentNode(this.CurrentRootNode.GetBaseNode());
66 
67             if (parentNode == null || parentNode.getType() == MNodeType.TYPE_UNKNOWN )
68                 parentNode = this._megaSdk.getRootNode();
69 
70             this.CurrentRootNode = new NodeViewModel(App.MegaSdk, parentNode);
71         }
72 
LoadNodes()73         public void LoadNodes()
74         {
75             this.ChildNodes.Clear();
76 
77             MNodeList nodeList = this._megaSdk.getChildren(this.CurrentRootNode.GetBaseNode());
78 
79             for (int i = 0; i < nodeList.size(); i++)
80             {
81                 ChildNodes.Add(new NodeViewModel(this._megaSdk, nodeList.get(i)));
82             }
83         }
84 
OnNodeTap(NodeViewModel node)85         public void OnNodeTap(NodeViewModel node)
86         {
87             switch (node.Type)
88             {
89                 case MNodeType.TYPE_FOLDER:
90                     {
91                         SelectFolder(node);
92                         break;
93                     }
94             }
95         }
96 
FetchNodes()97         public void FetchNodes()
98         {
99             this.ChildNodes.Clear();
100 
101             var fetchNodesRequestListener = new FetchNodesRequestListener(this);
102             this._megaSdk.fetchNodes(fetchNodesRequestListener);
103         }
104 
SelectFolder(NodeViewModel selectedNode)105         public void SelectFolder(NodeViewModel selectedNode)
106         {
107             this.CurrentRootNode = selectedNode;
108             // Create unique uri string to navigate
109             NavigateService.NavigateTo(typeof(MainPage), NavigationParameter.Browsing, new Dictionary<string, string> {{"Id", Guid.NewGuid().ToString("N")}});
110         }
111 
112         #endregion
113 
114         #region Private Methods
115 
IsUserOnline()116         private bool IsUserOnline()
117         {
118             return Convert.ToBoolean(this._megaSdk.isLoggedIn());
119         }
120 
121         #endregion
122 
123         #region Properties
124 
125         public ObservableCollection<NodeViewModel> ChildNodes { get; set; }
126 
127         public NodeViewModel CurrentRootNode { get; set; }
128 
129         public NodeViewModel FocusedNode { get; set; }
130 
131         #endregion
132 
133     }
134 }
135