Skip to content

Commit

Permalink
Memory:: Perbaikan memory leak ketika show popup image di grid
Browse files Browse the repository at this point in the history
  • Loading branch information
bachtiarpanjaitan committed Sep 28, 2024
1 parent f0b405f commit ec7a348
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 38 deletions.
1 change: 1 addition & 0 deletions Bepe/Components/DetailPreviewPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public DetailPreviewPopup()
{
_btnClose.Clicked += (sender, args) =>
{
_datagrid.ItemsSource = null;
Close(true);
};

Expand Down
56 changes: 33 additions & 23 deletions Bepe/Components/ImagePreviewPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,52 @@ public class ImagePreviewPopup : Popup
WidthRequest = 100,
Margin = new Thickness(5, 0)
};

private Grid grid = new()
{
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto},
new RowDefinition { Height = 50}
},
Padding = 10
};

private Image image = new()
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
BackgroundColor = Colors.Transparent,
Opacity = 0.9
};

public ImagePreviewPopup()
{
CanBeDismissedByTappingOutsideOfPopup = false;
_btnClose.Clicked += (sender, args) =>
{
image.Source = null;
Close(true);
};

Color = Colors.Transparent;
grid.Add(image,0,0);
grid.Add(_btnClose,0,1);
Content = grid;
}

public void SetImage(string path, int width = 200, int height = 200)
{
Grid grid = new()
{
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto},
new RowDefinition { Height = 50}
},
Padding = 10
};

var image = new Image
{
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
WidthRequest = width,
HeightRequest = height,
BackgroundColor = Colors.Transparent,
Opacity = 0.9
};
image.Source = null;
image.WidthRequest = width;
image.HeightRequest = height;
image.Source = ImageSource.FromFile(path);

grid.Add(image,0,0);
grid.Add(_btnClose,0,1);
Content = grid;

}

public void ResetImage()
{
image.Source = null;
}

}
2 changes: 2 additions & 0 deletions Bepe/Controllers/PageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void OnClickSideMenuItemAsync(object obj, EventHandlerPageArgs e)
{
try
{
if (_contentView != null) _contentView = null;
_layout.SetContent(null);
_contentView = (ContentView)Activator.CreateInstance(type);
_layout.SetContent(_contentView);
}
Expand Down
17 changes: 6 additions & 11 deletions Bepe/Types/ColumnType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using CommunityToolkit.Maui.Views;
using IhandCashier.Bepe.Components;
using IhandCashier.Bepe.Constants;
using IhandCashier.Bepe.Helpers;
Expand All @@ -21,7 +22,7 @@ public class ColumnType : IDisposable
public int ImageWidth = 20;
public string Format { get; set; } = "";

private readonly PopupManager _popupManager = new();
// private readonly PopupManager _popupManager = new();
private TapGestureRecognizer _tapGestureRecognizer;
private EventHandler<TappedEventArgs> _imageTappedHandler;
private EventHandler _buttonClickedHandler;
Expand Down Expand Up @@ -162,7 +163,7 @@ private void ShowDetail(object sender, EventArgs e)
Device.BeginInvokeOnMainThread(() =>
{
popup.SetData(context.Views);
_popupManager.ShowPopup(popup);
Application.Current.MainPage.ShowPopup(popup);
});
}

Expand All @@ -173,15 +174,12 @@ private void Image_Tapped(object sender, EventArgs e)
{
if (sender is Image img)
{
var popup = new ImagePreviewPopup()
{
CanBeDismissedByTappingOutsideOfPopup = false
};
if (img.Source is FileImageSource source)
{
string path = source.File.Replace("/Thumbnails","");
popup.SetImage(path,500,500);
_popupManager.ShowPopupAsync(popup).ConfigureAwait(true);
var ImagePopupInstance = new ImagePreviewPopup();
ImagePopupInstance.SetImage(path, 500, 500);
Application.Current.MainPage.ShowPopup(ImagePopupInstance);
}
}
}
Expand All @@ -196,9 +194,6 @@ public void Dispose()
}

if (_buttonClickedHandler != null) _buttonClickedHandler = null;

// If PopupManager holds resources, dispose it too
_popupManager?.Dispose();
}
}
}
10 changes: 10 additions & 0 deletions IhandCashier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<Title>Ihand Cashier</Title>
<Authors>Bachtiar Panjaitan</Authors>
<Copyright>HMP Basapadi</Copyright>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>basapadi.snk</AssemblyOriginatorKeyFile>
<Configurations>Debug;Release;Relaase Core</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-maccatalyst|AnyCPU'">
Expand All @@ -51,9 +55,15 @@
<WarningLevel>4</WarningLevel>
<PlatformTarget>ARM64</PlatformTarget>
<MtouchLink>SdkOnly</MtouchLink>
<CodesignKey>Apple Development: [email protected] (53TNH3S72W)</CodesignKey>
<MtouchDebug>true</MtouchDebug>
<Optimize>true</Optimize>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<MtouchEnableSGenConc>true</MtouchEnableSGenConc>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<MtouchLink>SdkOnly</MtouchLink>
<CodesignKey>Apple Development: [email protected] (53TNH3S72W)</CodesignKey>
</PropertyGroup>
<ItemGroup>
<MauiImage Remove="Resources\Images\document.png" />
Expand Down
5 changes: 4 additions & 1 deletion IhandCashier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Relaase Core|Any CPU = Relaase Core|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4F2570B0-6B36-4040-8272-C9E0FBC401C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F2570B0-6B36-4040-8272-C9E0FBC401C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F2570B0-6B36-4040-8272-C9E0FBC401C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F2570B0-6B36-4040-8272-C9E0FBC401C5}.Release|Any CPU.Build.0 = Release|Any CPU
{4F2570B0-6B36-4040-8272-C9E0FBC401C5}.Release|Any CPU.Deploy.0 = Release|Any CPU
{4F2570B0-6B36-4040-8272-C9E0FBC401C5}.Relaase Core|Any CPU.ActiveCfg = Relaase Core|Any CPU
{D930DFC6-C684-4182-BF52-8D4614304F6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D930DFC6-C684-4182-BF52-8D4614304F6D}.Release|Any CPU.Build.0 = Release|Any CPU
{D930DFC6-C684-4182-BF52-8D4614304F6D}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{D930DFC6-C684-4182-BF52-8D4614304F6D}.Debug|Any CPU.Build.0 = Release|Any CPU
{D930DFC6-C684-4182-BF52-8D4614304F6D}.Relaase Core|Any CPU.ActiveCfg = Relaase Core|Any CPU
{D930DFC6-C684-4182-BF52-8D4614304F6D}.Relaase Core|Any CPU.Build.0 = Relaase Core|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 5 additions & 3 deletions Layouts/MainLayout.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace IhandCashier.Layouts;

public partial class MainLayout : ContentPage
{
public Dictionary<string, MenuFlyoutItem> ListMenu = new ();

private System.Timers.Timer _timer;
private readonly CultureInfo _cultureInfo = new("id-ID");
private UserSession userSession = new SessionManager().GetSession();
public Dictionary<string, MenuFlyoutItem> ListMenu = new ();
private IList<MenuBarItem> menuBar;

public MainLayout()
{
InitializeComponent();
Expand Down Expand Up @@ -46,7 +48,7 @@ public MainLayout()
new Button.ButtonContentLayout(Button.ButtonContentLayout.ImagePosition.Left, 10);
DatagridProvider.NextButton.ContentLayout =
new Button.ButtonContentLayout(Button.ButtonContentLayout.ImagePosition.Right, 10);
Container.Content = new PageHome();
Container.Content = new PageDataBarang();

if (Application.Current.MainPage is AppShell shell)
{
Expand All @@ -72,7 +74,7 @@ public MainLayout()
};
}
}

}

[Obsolete]
Expand Down
1 change: 1 addition & 0 deletions MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Maui;
using IhandCashier.Bepe.Components;
using IhandCashier.Bepe.Configs;
using IhandCashier.Bepe.Database;
using IhandCashier.Bepe.Interfaces;
Expand Down
Binary file added basapadi.snk
Binary file not shown.

0 comments on commit ec7a348

Please sign in to comment.