127 lines
4.5 KiB
C#
127 lines
4.5 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Android.Views;
|
|
using AndroidX.AppCompat.App;
|
|
using Google.Android.Material.Color;
|
|
using Google.Android.Material.FloatingActionButton;
|
|
using Google.Android.Material.Snackbar;
|
|
using Google.Android.Material.TextField;
|
|
|
|
namespace SimpleNotes;
|
|
|
|
[Activity(Label = "@string/note_page",Theme ="@style/Theme.SimpleNotes")]
|
|
public class NoteActivity : AppCompatActivity
|
|
{
|
|
public override bool OnCreateOptionsMenu(IMenu? menu)
|
|
{
|
|
MenuInflater.Inflate(Resource.Menu.menu_note,menu);
|
|
return true;
|
|
}
|
|
Func<Task>? Save;
|
|
public override bool OnOptionsItemSelected(IMenuItem item)
|
|
{
|
|
int id=item.ItemId;
|
|
if(id == Resource.Id.action_save)
|
|
{
|
|
if(Save != null)
|
|
Task.Run(Save).Wait(0);
|
|
}
|
|
return base.OnOptionsItemSelected(item);
|
|
}
|
|
|
|
protected override async void OnCreate(Bundle? savedInstanceState)
|
|
{
|
|
base.OnCreate(savedInstanceState);
|
|
DynamicColors.ApplyToActivityIfAvailable(this);
|
|
// Set our view from the "main" layout resource
|
|
SetContentView(Resource.Layout.activity_note);
|
|
SetSupportActionBar(FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar2));
|
|
var (endpoint,username,password) = MainActivity.GetPrefs(this);
|
|
|
|
//check for id
|
|
var note_title = FindViewById<TextInputEditText>(Resource.Id.note_title);
|
|
var note_body = FindViewById<TextInputEditText>(Resource.Id.note_body);
|
|
|
|
if(note_title == null || note_body == null) return;
|
|
|
|
|
|
long id = Intent?.GetLongExtra("note_id",0) ?? 0;
|
|
|
|
if(id > 0)
|
|
{
|
|
if(!string.IsNullOrWhiteSpace(endpoint) && !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
|
|
{
|
|
var request = new HttpRequestMessage(HttpMethod.Get,$"{endpoint}/api/note?id={id}");
|
|
request.Headers.Add("Authorization",$"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{username}:{password}"))}");
|
|
var response=await MainActivity.Client.SendAsync(request);
|
|
var note = response.StatusCode == HttpStatusCode.OK ? (await response.Content.ReadFromJsonAsync<_NoteResponse>())?.Note : null;
|
|
if(note != null)
|
|
{
|
|
note_title.Text = note.Title;
|
|
this.Title = note.Title;
|
|
note_body.Text = note.Body;
|
|
}
|
|
}
|
|
|
|
}
|
|
note_title.TextChanged += (sender,e)=>{
|
|
this.Title = note_title.Text;
|
|
};
|
|
async Task<long> SaveNote(Note note)
|
|
{
|
|
var request = new HttpRequestMessage(HttpMethod.Post,$"{endpoint}/api/note");
|
|
request.Headers.Add("Authorization",$"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{username}:{password}"))}");
|
|
request.Content = new System.Net.Http.StringContent(System.Text.Json.JsonSerializer.Serialize(note),new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
|
|
|
|
var result=await MainActivity.Client.SendAsync(request);
|
|
if(result.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
var res2=await result.Content.ReadFromJsonAsync<_NoteResponse>();
|
|
return res2?.Note?.Id ?? 0;
|
|
}
|
|
return 0;
|
|
}
|
|
Save = async()=>{
|
|
|
|
var note = new Note();
|
|
note.Id = id;
|
|
this.RunOnUiThread(()=>{
|
|
note.Title = note_title.Text ?? "";
|
|
note.Body = note_body.Text ?? "";
|
|
});
|
|
if(!string.IsNullOrWhiteSpace(endpoint) && !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
|
|
{
|
|
|
|
id=await Task.Run(async()=>await SaveNote(note));
|
|
|
|
|
|
this.RunOnUiThread(()=>{
|
|
|
|
Toast.MakeText(this,"Saved Note",ToastLength.Long)?.Show();
|
|
});
|
|
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|
|
public class _NoteResponse
|
|
{
|
|
[JsonPropertyName("success")]
|
|
public bool Success {get;set;}
|
|
|
|
[JsonPropertyName("note")]
|
|
public Note Note {get;set;}=new Note();
|
|
}
|
|
public class Note
|
|
{
|
|
[JsonPropertyName("id")]
|
|
public long Id {get;set;}
|
|
|
|
[JsonPropertyName("title")]
|
|
public string Title {get;set;}="";
|
|
|
|
[JsonPropertyName("body")]
|
|
public string Body {get;set;}="";
|
|
} |