添加项目文件。
This commit is contained in:
84
XXCpzs/Services/AzureDataStore.cs
Normal file
84
XXCpzs/Services/AzureDataStore.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Xamarin.Essentials;
|
||||
using XXCpzs.Models;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public class AzureDataStore : IDataStore<Item>
|
||||
{
|
||||
HttpClient client;
|
||||
IEnumerable<Item> items;
|
||||
|
||||
public AzureDataStore()
|
||||
{
|
||||
client = new HttpClient();
|
||||
client.BaseAddress = new Uri($"{App.AzureBackendUrl}/");
|
||||
|
||||
items = new List<Item>();
|
||||
}
|
||||
|
||||
bool IsConnected => Connectivity.NetworkAccess == NetworkAccess.Internet;
|
||||
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
|
||||
{
|
||||
if (forceRefresh && IsConnected)
|
||||
{
|
||||
var json = await client.GetStringAsync($"api/item");
|
||||
items = await Task.Run(() => JsonConvert.DeserializeObject<IEnumerable<Item>>(json));
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public async Task<Item> GetItemAsync(string id)
|
||||
{
|
||||
if (id != null && IsConnected)
|
||||
{
|
||||
var json = await client.GetStringAsync($"api/item/{id}");
|
||||
return await Task.Run(() => JsonConvert.DeserializeObject<Item>(json));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<bool> AddItemAsync(Item item)
|
||||
{
|
||||
if (item == null || !IsConnected)
|
||||
return false;
|
||||
|
||||
var serializedItem = JsonConvert.SerializeObject(item);
|
||||
|
||||
var response = await client.PostAsync($"api/item", new StringContent(serializedItem, Encoding.UTF8, "application/json"));
|
||||
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateItemAsync(Item item)
|
||||
{
|
||||
if (item == null || item.Id == null || !IsConnected)
|
||||
return false;
|
||||
|
||||
var serializedItem = JsonConvert.SerializeObject(item);
|
||||
var buffer = Encoding.UTF8.GetBytes(serializedItem);
|
||||
var byteContent = new ByteArrayContent(buffer);
|
||||
|
||||
var response = await client.PutAsync(new Uri($"api/item/{item.Id}"), byteContent);
|
||||
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteItemAsync(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id) && !IsConnected)
|
||||
return false;
|
||||
|
||||
var response = await client.DeleteAsync($"api/item/{id}");
|
||||
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
XXCpzs/Services/CustomerDataService.cs
Normal file
25
XXCpzs/Services/CustomerDataService.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using XXCpzs.Models;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public class CustomerDataService
|
||||
{
|
||||
public static List<Customer> Fruits { get; set; } = new List<Customer>
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
public static List<Customer> GetSearchResults(string queryString)
|
||||
{
|
||||
var normalizedQuery = queryString?.ToLower() ?? "";
|
||||
return Fruits.Where<Customer>(f => f.company.ToLowerInvariant().Contains(normalizedQuery) || f.contacts.ToLowerInvariant().Contains(normalizedQuery) || f.phone.ToLowerInvariant().Contains(normalizedQuery)).ToList();
|
||||
}
|
||||
public static void AddItem(Customer item)
|
||||
{
|
||||
Fruits.Add(item);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
15
XXCpzs/Services/IDataStore.cs
Normal file
15
XXCpzs/Services/IDataStore.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public interface IDataStore<T>
|
||||
{
|
||||
Task<bool> AddItemAsync(T item);
|
||||
Task<bool> UpdateItemAsync(T item);
|
||||
Task<bool> DeleteItemAsync(string id);
|
||||
Task<T> GetItemAsync(string id);
|
||||
Task<IEnumerable<T>> GetItemsAsync(bool forceRefresh = false);
|
||||
}
|
||||
}
|
||||
37
XXCpzs/Services/IDeviceOrientationService.cs
Normal file
37
XXCpzs/Services/IDeviceOrientationService.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Xamarin.Forms.Internals;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public enum DeviceOrientation
|
||||
{
|
||||
Undefined,
|
||||
Landscape,
|
||||
Portrait
|
||||
}
|
||||
public interface ICommonService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取本地版本号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
string GetVersion();
|
||||
|
||||
void CloseApp();
|
||||
|
||||
/// <summary>
|
||||
/// 升级App
|
||||
/// </summary>
|
||||
/// <param name="DownloadUrl">升级地址</param>
|
||||
void UpdateApp(string DownloadUrl);
|
||||
|
||||
/// <summary>
|
||||
/// 发送通知
|
||||
/// </summary>
|
||||
/// <param name="title">标题</param>
|
||||
/// <param name="content">内容</param>
|
||||
void SendNotity(string title, string content);
|
||||
}
|
||||
}
|
||||
13
XXCpzs/Services/INetworkConnection.cs
Normal file
13
XXCpzs/Services/INetworkConnection.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public interface INetworkConnection
|
||||
{
|
||||
bool IsConnected { get; }
|
||||
void CheckNetworkConnection();
|
||||
|
||||
}
|
||||
}
|
||||
9
XXCpzs/Services/ITextToSpeechService.cs
Normal file
9
XXCpzs/Services/ITextToSpeechService.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public interface ITextToSpeechService
|
||||
{
|
||||
Task SpeakAsync(string text);
|
||||
}
|
||||
}
|
||||
60
XXCpzs/Services/MockDataStore.cs
Normal file
60
XXCpzs/Services/MockDataStore.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using XXCpzs.Models;
|
||||
|
||||
namespace XXCpzs.Services
|
||||
{
|
||||
public class MockDataStore : IDataStore<Item>
|
||||
{
|
||||
readonly List<Item> items;
|
||||
|
||||
public MockDataStore()
|
||||
{
|
||||
items = new List<Item>()
|
||||
{
|
||||
new Item { Id = Guid.NewGuid().ToString(), Text = "First item", Description="This is an item description." },
|
||||
new Item { Id = Guid.NewGuid().ToString(), Text = "Second item", Description="This is an item description." },
|
||||
new Item { Id = Guid.NewGuid().ToString(), Text = "Third item", Description="This is an item description." },
|
||||
new Item { Id = Guid.NewGuid().ToString(), Text = "Fourth item", Description="This is an item description." },
|
||||
new Item { Id = Guid.NewGuid().ToString(), Text = "Fifth item", Description="This is an item description." },
|
||||
new Item { Id = Guid.NewGuid().ToString(), Text = "Sixth item", Description="This is an item description." }
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<bool> AddItemAsync(Item item)
|
||||
{
|
||||
items.Add(item);
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateItemAsync(Item item)
|
||||
{
|
||||
var oldItem = items.Where((Item arg) => arg.Id == item.Id).FirstOrDefault();
|
||||
items.Remove(oldItem);
|
||||
items.Add(item);
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteItemAsync(string id)
|
||||
{
|
||||
var oldItem = items.Where((Item arg) => arg.Id == id).FirstOrDefault();
|
||||
items.Remove(oldItem);
|
||||
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
|
||||
public async Task<Item> GetItemAsync(string id)
|
||||
{
|
||||
return await Task.FromResult(items.FirstOrDefault(s => s.Id == id));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
|
||||
{
|
||||
return await Task.FromResult(items);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user