How can I use a variable taskId from viewmodel into activity - viewmodel

class MedViewModel #Inject constructor(
private val taskRepository: TaskRepository
) : BaseViewModel() {
private var taskId: Int = -1
fun fetchLastScannedCode() {viewModelScope.launch {
taskRepository.fetchLastScannedTaskItems().collect { taskItems ->
logInCrashlytics(
functionName = "fetchLastScannedTaskItems",
statusName = "TaskItems",
objectName = taskItems.toString()
)
taskItems?.let {
taskId = taskItems.task?.id!!
task = taskItems.task
taskItem = it.taskItemsList.firstOrNull()
}}}}

Related

Thymeleaf : Call Java function in JavaScript Click listener

So I have this controller class with some getters and setters and a function:
#Controller
public class Hotels{
public Hotels(String name) {
this.name = name;
}
public Hotels(String name, String price, String longitude, String latitude, String URL, String photoURL) {
this.name = name;
this.price = price;
this.longitude = longitude;
this.latitude = latitude;
this.URL = URL;
PhotoURL = photoURL;
}
public Hotels() {
}
public List<Hotels> returnHotels(String city) throws IOException, InterruptedException {
double[] lnglat = new double[2];
MapController controller = new MapController();
for (int i = 0; i <controller.coolLocations().size(); i++) {
if(controller.coolLocations().get(i).getDescription().equals(city)){
lnglat = controller.coolLocations().get(i).getLnglat();
}
}
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://booking-com.p.rapidapi.com/v1/hotels/search-by-coordinates?order_by=popularity&adults_number=2&units=metric&room_number=1&checkout_date=2022-11-15&filter_by_currency=RON&locale=en-gb&checkin_date=2022-11-12&latitude="+lnglat[1]+"&longitude="+lnglat[0]))
.header("X-RapidAPI-Key", "myKey")
.header("X-RapidAPI-Host", "booking-com.p.rapidapi.com")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
String[] datasplit = response.body().split("\"");
String url = "";
String hotel_name = "";
String latitude = "";
String longitude = "";
String gross_price= "";
String main_photo_url= "";
for (int i = 0; i < datasplit.length; i++) {
if (datasplit[i].equals("url")) url = datasplit[i+2];
if (datasplit[i].equals("hotel_name")) hotel_name = datasplit[i+2];
if (datasplit[i].equals("latitude")) latitude = datasplit[i+1].substring(1).replace(',',' ');
if (datasplit[i].equals("longitude")) longitude = datasplit[i+1].substring(1).replace(',',' ');
if ((datasplit[i].equals("gross_amount_hotel_currency") && datasplit[i+1].equals(":{") && datasplit[i+2].equals("value"))) gross_price = datasplit[i+3].substring(1).replace('}',' ').replace(',',' ');
if ((datasplit[i].equals("gross_amount_hotel_currency") && datasplit[i+1].equals(":{") && datasplit[i+2].equals("currency") && datasplit[i+3].equals(":") && datasplit[i+4].equals("EUR")&& datasplit[i+5].equals(",") && datasplit[i+6].equals("value"))) gross_price = datasplit[i+7].substring(1).replace('}',' ').replace(',',' ');
if (datasplit[i].equals("max_photo_url")) main_photo_url = datasplit[i+2];
if(datasplit[i].equals("max_1440_photo_url"))hotelsList.add(new Hotels(hotel_name, gross_price, longitude, latitude, url, main_photo_url));
}
hotelsList.forEach(System.out::println);
System.out.println(city);
return hotelsList;
}
}
And this is my get Mapping :
#GetMapping("/")
public String homePage(Model model) throws IOException, InterruptedException {
model.addAttribute("returnHotels", new Hotels());
return "home";
}
And a JavaScript on click listener :
marker.getElement().addEventListener('click', function () {
const city = location.description;
});
I want inside the JS click listener to call my returnHotels function with the const city as my parameter. Is it possible?
PS : I've deleted the class variables so the site let me post

Testable Kotlin objects that uses a dispatcher in init block?

I have a class that looks like this:
object SomeRepository {
private val logger = Logger(this)
private val flow: MutableStateFlow<List<SomeClass>> = MutableStateFlow(listOf())
init {
GlobalScope.launch(Dispatchers.IO) {
// some code
}
}
fun list() = flow.value
fun observe(): StateFlow<List<SomeClass>> = flow
}
It is recommended to inject dispatchers in tests.
Also, there is a risk to get weird problems such as AppNotIdleException if not following this practice.
One option would be to make these into variables and add setters, but not very nice. Also it creates a race condition (init-block vs setters):
object SomeRepository {
private val logger = Logger(this)
private val flow: MutableStateFlow<List<SomeClass>> = MutableStateFlow(listOf())
private var coroutineScope: CoroutineScope = GlobalScope
private var dispatcher: CoroutineDispatcher = Dispatchers.IO
init {
dispatcher.launch(coroutineScope) {
// some code
}
}
fun list() = flow.value
fun observe(): StateFlow<List<SomeClass>> = flow
#VisibleForTesting
fun setDispatcher(dispatcher: CoroutineDispatcher) {
this.dispatcher = dispatcher
}
#VisibleForTesting
fun setCoroutineScope(coroutineScope: CoroutineScope) {
this.coroutineScope = coroutineScope
}
}
Version that avoids the race condition (test needs to explicitly invoke init()):
object SomeRepository {
private val logger = Logger(this)
private val flow: MutableStateFlow<List<SomeClass>> = MutableStateFlow(listOf())
private var coroutineScope: CoroutineScope = GlobalScope
private var dispatcher: CoroutineDispatcher = Dispatchers.IO
init {
// var isInTesting = Build.FINGERPRINT == "robolectric"
if (!isInTesting) {
init()
}
}
#VisibleForTesting
fun init() {
dispatcher.launch(coroutineScope) {
// some code
}
}
fun list() = flow.value
fun observe(): StateFlow<List<SomeClass>> = flow
#VisibleForTesting
fun setDispatcher(dispatcher: CoroutineDispatcher) {
this.dispatcher = dispatcher
}
#VisibleForTesting
fun setCoroutineScope(coroutineScope: CoroutineScope) {
this.coroutineScope = coroutineScope
}
}
How can I inject a coroutine scope and dispatcher to my tests and avoid variables?

how to test IQueryable of strongly typed using xUnit in .NET CORE

I am working on a test project for .NET CORE Web API project. I have SchoolService class that implements numbers of methods as some of them below
Service Class
public class SchoolService : ISchoolService
{
private readonly ISchoolEntity schoolEntity;
public SchoolService(ISchoolEntity schoolEntity)
{
this.schoolEntity = schoolEntity;
}
public IQueryable<SchoolDataView> GetAllSchools()
{
var query = this.schoolEntity.GetAllSchool();
return query;
}
public SchoolDataView GetSchoolById(Guid Id)
{
var query = this.schoolEntity.GetSchoolById(Id);
return query;
}
I want to test
1- GetAllSchools return object type is of IQueryable?
2- How I use autofix or by another way for schoolEntity.GetAllSchool() return fake IQueryable?
Service Test
public class SchoolServiceTests
{
private readonly ISchoolService schoolService;
private readonly ISchoolEntity schoolEntity = Substitute.For<ISchoolEntity>();
public SchoolServiceTests()
{
schoolService = new SchoolService(schoolEntity);
}
[Fact]
public void GetAllSchool_ShouldReturn_IQueryableOfSchoolDataView()
{
//Arrange
//Act
var a = schoolEntity.GetAllSchool();
//Assert
Assert.??
}
}
I have written following test to achieve behaviour that I have stated in my question. Open to hearing more feedback and suggestions on it. Thanks
[Fact]
public void GetAllSchool_ShouldReturn_IQueryableOfSchoolDataView()
{
//Arrange
var schoolDataViewList = new List<SchoolDataView>
{
new SchoolDataView { SchoolID = Guid.NewGuid(), Name = "London Primary School"},
new SchoolDataView { SchoolID = Guid.NewGuid(), Name = "Windsor Gramer School"},
new SchoolDataView { SchoolID = Guid.NewGuid(), Name = "Kings College"},
new SchoolDataView { SchoolID = Guid.NewGuid(), Name = "Reading School"}
}.AsQueryable();
schoolEntity.GetAllSchool().Returns(schoolDataViewList);
//Act
var actualSchoolList = sut.GetAllSchools();
//Assert
Assert.IsAssignableFrom<IQueryable<SchoolDataView>>(actualSchoolList);
}
OR Using AutoFixture
[Fact]
public void GetAllSchool_ShouldReturn_IQueryableOfSchoolDataView()
{
//Arrange
var fixture = new Fixture();
var schoolDataViewMock = fixture.CreateMany<SchoolDataView>();
schoolEntity.GetAllSchool().Returns(schoolDataViewMock.AsQueryable());
//Act
var actualSchoolDataList = sut.GetAllSchools();
//Assert
Assert.IsAssignableFrom<IQueryable<SchoolDataView>>(actualSchoolDataList);
}

Prism 5 to 6.2 - InteractionRequest - InvocationList not null anymore when navigate to new object

PRISM 6.2 / EntityFramework 6.3.1 / StockTrader UI / UnityContainer
I acutally having a project with PRISM 5.0.0 and want to update to PRISM 6.2. The project runs fine on 5.0.0, but when I'm updating to 6.2 I got the following problem with the InteractionRequest.
When I navigate to a view/viewmodel with Notifications for the first time, everything works and I can handle the InteractionRequests as usual. If I navigate back and navigate to the view again with a new object, the InteractionsRequest raised the notification twice. (...navigating back and go to again -> raised three times and so on).
In some reasons, the message "This Visual is not connected to a PresentationSource" will occur.
I figure out, that the _invocationCount and _invocationList on the InteractionRequest will not be set to "0"/"null" with PRISM 6.2. So, i think the InteractionRequest will call the notification more than one time. Attached, are screenshots from PRISM 5 and PRISM 6.2.
How can I handle this and solve the problem. In my opinion, it's not a big thing but I actually spent a lot of time to find a solution. Thanks...
PRISM 5.0.0 - working fine
PRISM 6.2 - issue
2017.02.22 Added Sourccode. Software is used to handle devices in datacenters. I deleted all unnecessary sourcecode, but with these files the problem still occur. Perhaps this is a try to find my issue....
Rackmodule.cs
-> Initialize Module Rack
public class RackModule : IModule
{
private readonly IRegionManager _regionManager;
private readonly IUnityContainer _container;
public RackModule(IRegionManager regionManager, IUnityContainer container)
{
_regionManager = regionManager;
_container = container;
}
public void Initialize()
{
_container.RegisterType<IRackViewModel, RackViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<IRackToolbarViewModel, RackToolbarViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<IRackStatusbarViewModel, RackStatusbarViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<IRackSummaryViewModel, RackSummaryViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<IGeneralDataViewModel, GeneralDataViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<IPlanDataViewModel, PlanDataViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<IRackDataViewModel, RackDataViewModel>(new ContainerControlledLifetimeManager());
_container.RegisterType<Object, GeneralDataView>(typeof(GeneralDataView).FullName);
IRegion region = this._regionManager.Regions["MainRegion"];
var rackView = _container.Resolve<RackView>();
region.Add(rackView, "RackView");
region.Activate(rackView);
IRegion toolbarregion = this._regionManager.Regions["RackToolbarRegion"];
var toolbarView = _container.Resolve<RackToolbarView>();
toolbarregion.Add(toolbarView, "RackToolbarView");
toolbarregion.Activate(toolbarView);
IRegion statusbarregion = this._regionManager.Regions["RackStatusbarRegion"];
var statusbarView = _container.Resolve<RackStatusbarView>();
statusbarregion.Add(statusbarView, "RackStatusbarView");
statusbarregion.Activate(statusbarView);
_container.RegisterType<Object, RackSummaryView>(typeof(RackSummaryView).FullName);
_regionManager.RequestNavigate(RegionNames.RackContentRegion, typeof(RackSummaryView).FullName);
}
}
RackSummaryViewModel.cs
-> Overview of racks. Go to RackDataView, when click on object
public class RackSummaryViewModel : BindableBase, IRackSummaryViewModel
{
private readonly IRegionManager _regionManager;
private readonly IEventAggregator _eventAggregator;
private readonly IUnityContainer _container;
public DelegateCommand<SearchEventArgs> OnSearch { get; private set; }
public DelegateCommand AdvancedRackSearchCommand { get; private set; }
public InteractionRequest<AdvancedRackSearchNotification> AdvancedSearchRequest { get; private set; }
private ObservableCollection<RackSummaryEntry> _racks;
public ObservableCollection<RackSummaryEntry> Racks
{
get { return _racks; }
private set {SetProperty(ref _racks, value);}
}
private RackSummaryEntry _currentRack;
public RackSummaryEntry CurrentRack
{
get { return _currentRack; }
set
{
if (SetProperty(ref _currentRack, value))
{
if (_currentRack != null)
{
var parameters = new NavigationParameters();
parameters.Add("RackID", _currentRack.PrimaryKey.ToString(GuidNumericFormatSpecifier));
_container.RegisterType<Object, RackDataView>(typeof(RackDataView).FullName);
_regionManager.RequestNavigate(RegionNames.RackContentRegion, new Uri(typeof(RackDataView).FullName + parameters, UriKind.Relative));
}
}
}
}
private const string GuidNumericFormatSpecifier = "N";
public RackSummaryViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
{
_regionManager = regionManager;
_eventAggregator = eventAggregator;
_container = container;
ISessionFactory factory = new SessionFactory();
container.RegisterType<IRepository, Repository>(new InjectionConstructor(factory.CurrentUoW));
IUnitOfWork unitOfWork = factory.CurrentUoW;
IRepository localrepository = new Repository(unitOfWork);
var query = localrepository.GetList<DMS.Domain.Domain.Rack>();
Racks = new ObservableCollection<RackSummaryEntry>(query
.Select(x => new RackSummaryEntry
{
PrimaryKey = x.PrimaryKey,
Country = x.Location.Address.Country,
City = x.Location.Address.City,
Street = x.Location.Address.Street,
Building = x.Location.BuildingName,
RoomName = x.Location.RoomName,
RackName = x.RackName,
RackHeight = x.RackHeight
}).ToList());
}
}
RackDataViewModel.cs
-> Only Button "Save" und "Go Back"
public class RackDataViewModel : BindableBase, IRackDataViewModel, INavigationAware, IRegionMemberLifetime
{
private IRegionNavigationJournal _navigationJournal;
private readonly IRegionManager _regionManager;
private readonly IUnityContainer _container;
private readonly IEventAggregator _eventAggregator;
public DelegateCommand GoBackCommand { get; private set; }
public DelegateCommand SaveCommand { get; private set; }
public InteractionRequest<INotification> SaveNotificationRequest { get; private set; }
private const string RackIdKey = "RackID";
private const string EType = "EditType";
private const string GuidNumericFormatSpecifier = "N";
public DMS.Domain.Domain.Rack rack;
// [InjectionConstructor] check if necessary
public RackDataViewModel(IRegionManager regionManager, IRegionNavigationJournal navigationJournal, IUnityContainer container, IEventAggregator eventAggregator, ILoggerFactory logFactory)
{
_regionManager = regionManager;
_navigationJournal = navigationJournal;
_container = container;
_eventAggregator = eventAggregator;
GoBackCommand = new DelegateCommand(OnGoBackExecute);
SaveCommand = new DelegateCommand(OnSaveExecute);
SaveNotificationRequest = new InteractionRequest<INotification>();
}
private void OnGoBackExecute()
{
if (_navigationJournal != null)
{
while (_navigationJournal.CanGoBack)
_navigationJournal.GoBack();
_regionManager.Regions.Remove(RegionNames.RackGeneralDataRegion);
}
}
private void OnSaveExecute()
{
SaveNotificationRequest.Raise(new Notification { Content = "Save changes submitted", Title = "Save changes" });
}
public bool KeepAlive
{
get { return false; }
}
private Guid? GetRequestedRackId(NavigationContext navigationContext)
{
var rack = navigationContext.Parameters[RackIdKey];
Guid rackId;
if (rack != null)
{
if (rack is Guid)
rackId = (Guid)rack;
else
rackId = Guid.Parse(rack.ToString());
return rackId;
}
return null;
}
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
{
var type = navigationContext.Parameters[EType];
if (rack == null || ((string)type) == "New")
return true;
var requestedRackId = GetRequestedRackId(navigationContext);
return requestedRackId.HasValue && requestedRackId.Value == rack.PrimaryKey;
}
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
}
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
Guid? rackId;
NavigationParameters parameters = new NavigationParameters();
string key = navigationContext.Parameters[RackIdKey].ToString();
rackId = GetRequestedRackId(navigationContext);
parameters = navigationContext.Parameters;
_regionManager.RequestNavigate(RegionNames.RackGeneralDataRegion, new Uri(typeof(GeneralDataView).FullName + parameters, UriKind.Relative));
_navigationJournal = navigationContext.NavigationService.Journal;
}
}
GeneralDataViewModel.cs
-> Is in region of RackDataView with the data of the racks
public class GeneralDataViewModel : BindableBase, IGeneralDataViewModel, INavigationAware
{
private IRegionNavigationJournal _navigationJournal;
private readonly IRegionManager _regionManager;
private readonly IRepository _repository;
private const string RackIdKey = "RackID";
public DMS.Domain.Domain.Rack Rack { get; set; }
public List<Location> Locations { get; set; }
public GeneralDataViewModel(IRegionManager regionManager, IRepository repository)
{
_regionManager = regionManager;
_repository = repository;
}
private Guid? GetRequestedRackId(NavigationContext navigationContext)
{
var rack = navigationContext.Parameters[RackIdKey];
Guid rackId;
if (rack != null)
{
if (rack is Guid)
rackId = (Guid)rack;
else
rackId = Guid.Parse(rack.ToString());
return rackId;
}
return null;
}
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
{
if (Rack == null)
return true;
var requestedRackId = GetRequestedRackId(navigationContext);
return requestedRackId.HasValue && requestedRackId.Value == Rack.PrimaryKey;
}
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
// Intentionally not implemented.
}
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
var rackId = GetRequestedRackId(navigationContext);
Rack = _repository.GetEntity<DMS.Domain.Domain.Rack>(rackId);
_navigationJournal = navigationContext.NavigationService.Journal;
}
}

SQL Server CE - Inheritance mapping - column cannot containt null values exception

I am designing a simple db. Two tables were quite similar so I've used inheritance mapping technique.
[Table]
[InheritanceMapping(Code = "P", Type = typeof(ATable),
IsDefault = true)]
[InheritanceMapping(Code = "E", Type = typeof(BTable))]
public class ATable: TableBase
{
[Column(IsDiscriminator = true)]
public string DiscKey;
private int _Id;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int Id
{
get { return _Id; }
set
{
if (_Id != value)
{
NotifyPropertyChanging("Id");
_Id = value;
NotifyPropertyChanged("Id");
}
}
}
....
}
public class BTable : ATable
{
private int _Property;
[Column]
public int Property
{
get { return _Property; }
set
{
if (_Property!= value)
{
NotifyPropertyChanging("Property");
_DeviceDetailsId = value;
NotifyPropertyChanged("Property");
}
}
}
...
}
//DBDATACONTEXT
public class DBDataContext : DataContext
{
public DBDataContext(string connectionString) : base(connectionString) {}
public Table<ATable> As;
}
when I try:
var db = DBDataContext("Data Source=isostore:/xDB.sdf;Password='testpass';");
var atable = new ATable();
db.As.InsertOnSubmit(atable);
db.SubmitChanges();
I get an exception: The column cannot contain null values. [ Column name = Property,Table name = ATable]
What's wrong?!
The row on the table will always contains all the properties of all the inherited entities... so marking child columns(properties) with CanBeNull = true will solve the problem.

Resources