flutter Equatable with GetxController - get

I found an excellent way to make a filter using both Equatable and Block
But I want to convert it to Get. I tried a lot for days, but I couldn't..Can I get help converting it to Get, please ?
The filter method consists of three classes, the first FilterEvent :
abstract class FilterEvent extends Equatable {
const FilterEvent();
#override
List<Object> get props => [];
}
class FilterLoad extends FilterEvent {
#override
List<Object> get props => [];
}
class LocationFilterUpdated extends FilterEvent {
final LocationFilter locationFilter;
const LocationFilterUpdated({required this.locationFilter});
#override
List<Object> get props => [locationFilter];
}
Second FilterState :
class FilterState extends Equatable {
const FilterState();
#override
List<Object> get props => [];
}
class FilterLoading extends FilterState {}
class FilterLoaded extends FilterState {
final Filter filter;
const FilterLoaded({this.filter = const Filter()});
#override
List<Object> get props => [filter];
}
Finally Block:
class FilterBlock extends Block<FilterEvent, FilterState> {
FilterBlock() : super(FilterLoading());
#override
Stream<FilterState> mapEventToState(FilterEvent event) async* {
if (event is FilterLoad) {
yield* _mapFilterLoadToState();
}
if(event is LocationFilterUpdated){
yield* _mapLocationFilterUpdated(event, state)
}
}
Stream<FilterState> _mapFilterLoadToState() async*{
yield* FilterLoaded(filter: Filter(locationsFilter: updatedLocationFilters));
}
Stream<FilterState> _mapLocationFilterUpdated(LocationFilterUpdated event , FilterState state) async*{
if(state is FilterLoaded){
final List<LocationFilter> updatedLocationFilters =
state.filter.locationsFilter.map((locationFilter) {
return locationFilter.id == event.locationFilter.id
? event.locationFilter
: locationFilter;
}).toList();
FilterLoaded(filter: Filter(locationsFilter: updatedLocationFilters));
}
}
}

Related

Trouble with logic flow for filtering search method on CS50 Pokedex

The app compiles fine but initially it shows nothing in the list. When I use the search bar, it doesn't display my filtered information and when I get rid of search, the entire list is finally display. Any help would be really appreciated, this is my first time ever coding in Java.
Here is my adapter code.
public class PokedexAdapter extends RecyclerView.Adapter<PokedexAdapter.PokedexViewHolder> implements Filterable {
public static class PokedexViewHolder extends RecyclerView.ViewHolder {
public LinearLayout containerView;
public TextView textView;
PokedexViewHolder(View view) {
super(view);
containerView = view.findViewById(R.id.pokedex_row);
textView = view.findViewById(R.id.pokedex_row_text_view);
containerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Pokemon current = (Pokemon) containerView.getTag();
Intent intent = new Intent(v.getContext(), PokemonActivity.class);
intent.putExtra("name", current.getName());
intent.putExtra("url", current.getUrl());
v.getContext().startActivity(intent);
}
});
}
}
private List<Pokemon> pokemon = new ArrayList<>();
private RequestQueue requestQueue;
private List<Pokemon> filteredPokemon = new ArrayList<>();
PokedexAdapter(Context context) {
requestQueue = Volley.newRequestQueue(context);
loadPokemon();
}
public void loadPokemon() {
String url = "https://pokeapi.co/api/v2/pokemon?limit=365";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray results = response.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject result = results.getJSONObject(i);
String name = result.getString("name");
pokemon.add(new Pokemon(
name.substring(0, 1).toUpperCase() + name.substring(1),
result.getString("url")
));
}
notifyDataSetChanged();
} catch (JSONException e) {
Log.e("cs50", "Json error", e);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("cs50", "Pokemon list error");
}
});
requestQueue.add(request);
}
#NonNull
#Override
public PokedexViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pokedex_row, parent, false);
return new PokedexViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull PokedexViewHolder viewholder, int position){
Pokemon results = pokemon.get(position);
viewholder.textView.setText(results.getName());
viewholder.containerView.setTag(results);
}
#Override
public int getItemCount() {
return filteredPokemon.size();
}
#Override
public Filter getFilter() {
return new PokemonFilter();
}
private class PokemonFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
// implement your search here
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
//No filter implemented return whole list
results.values = pokemon;
results.count = pokemon.size();
}
else {
List<Pokemon> filtered = new ArrayList<>();
for (Pokemon pokemon : filtered) {
if (pokemon.getName().toUpperCase().startsWith(constraint.toString())) {
filtered.add(pokemon);
}
}
results.values = filtered;
results.count = filtered.size();
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredPokemon = (List<Pokemon>) results.values;
notifyDataSetChanged();
}
}
}
I really am not sure what is going on and given my knowledge of the subject, you could really help with understanding the logic better. Please let me know if there is any other information you would like from me about the code.
You might have already solved it and finished the Android tracks but this was the only thing I changed and it seemed to work after that.
for (Pokemon pokemon : pokemon) {
if (pokemon.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()) {
filtered.add(pokemon);
}
}

How to pass a string value from a class to another in the same activity in Flutter?

// I cant find a way to pass a string from a class to another. They are both in the same screen and i want to pass the index from a pageview on class to another. Any help would be grateful!
//on the first class//
onPageChanged: (int index) {
_sendDataToAnotherClass(context, index);
_currentPageNotifier.value = index;
}),
),
);
}
void _sendDataToAnotherClass(BuildContext context, int index) {
final String texttosend = index.toString();
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return new personaname(text: texttosend);
}
));
}
//on the second class//
final String text ;
personaname({Key key, #required this.text}) : super(key: key);
if your personaname is a class (just fyi if that is indeed a class the proper format is to capitalize the first letters of each word - PersonaName), and that class is a stateful widget you could pass the data down to it and do a call within the state to override the didUpdateWidget method to setState on data changes. This would mean you don't need the value notifier.
Something like this might do what you're looking for:
class PageViewHolder extends StatelessWidget{
PersonaName name = PersonaName(text: 'text',);
#override
Widget build(BuildContext context) {
return PageView(
onPageChanged: (p){
var textToSend = 'do logic to decide name';
name.text = textToSend;
},
);
}
}
class PersonaName extends StatefulWidget{
String text;
PersonaName({Key key, this.text}) : super(key: key);
#override
State<StatefulWidget> createState() =>_PersonaName();
}
class _PersonaName extends State<PersonaName>{
#override
Widget build(BuildContext context) {
return Text(widget.text);
}
#override
void didUpdateWidget(PersonaName oldWidget) {
super.didUpdateWidget(oldWidget);
if(widget.text != oldWidget.text){
setState(() {});
}
}
}

Saving data on postback in Orchard

I'm an Orchard newbie and I'm having difficulty trying to get the form data when a new item is created.
What I have is a module that creates a menu item on the admin dashboard. That menu item will load a page where a user can enter a new "Coach".
There are 3 things needed for a coach, first name, last name and email.
Here's the code I have implemented for this...
migrations.cs
public class SDSDataMigration : DataMigrationImpl
{
public int Create()
{
SchemaBuilder..CreateTable("CoachPartRecord", table => table.ContentPartRecord()
.Column("FirstName", DbType.AnsiString, c => c.WithLength(50))
.Column("LastName", DbType.AnsiString, c => c.WithLength(50))
.Column("Email", DbType.AnsiString, c => c.WithLength(200)))
ContentDefinitionManager.AlterPartDefinition("CoachPart", part => part
.WithField("FirstName", f => f.OfType("TextField"))
.WithField("LastName", f => f.OfType("TextField"))
.WithField("Email", f => f.OfType("TextField"))
);
ContentDefinitionManager.AlterTypeDefinition("Coach", type => type.WithPart("CommonPart")
.WithPart("CoachPart"));
return 1;
}
}
parts/records
public class CoachPartRecord : ContentPartRecord
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
}
public class CoachPart : ContentPart<CoachPartRecord>
{
public string FirstName
{
get { return Record.FirstName; }
set { Record.FirstName = value; }
}
public string LastName
{
get { return Record.LastName; }
set { Record.LastName = value; }
}
public string Email
{
get { return Record.Email; }
set { Record.Email = value; }
}
}
view for creating editor
#{ Layout.Title = T("Add Coach").ToString(); }
#using (Html.BeginFormAntiForgeryPost()) {
// Model is a Shape, calling Display() so that it is rendered using the most specific template for its Shape type
#Display(Model)
}
handler
public class CoachPartHandler : ContentHandler
{
public CoachPartHandler(IRepository<CoachPartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
}
}
driver
protected override DriverResult Editor(CoachPart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
controller (for dashboard menu item)
public ActionResult Create()
{
var coach = _services.ContentManager.New("Coach");
var model = _services.ContentManager.BuildEditor(coach);
return View(model);
}
[HttpPost, ActionName("Create")]
public ActionResult CreatePOST()
{
var contentItem = _services.ContentManager.New("Coach");
_services.ContentManager.Publish(contentItem);
return View("Index");
}
Right now I can get the form to appear to create a new coach. When I hit "Publish" all of the fields (i.e. FirstName, LastName, Email) for the CoachPart parameter in the driver are null.
I can look at the http request and I can see the values I put on the form, but they're not making it to the CoachPart.
Any ideas why the CoachPart fields aren't getting filed in?
Thanks!
First of all, you are defining the properties on your own record. Therefore you don't need new textfields attached to your part, so you should remove this:
ContentDefinitionManager.AlterPartDefinition("CoachPart", part => part
.WithField("FirstName", f => f.OfType("TextField"))
.WithField("LastName", f => f.OfType("TextField"))
.WithField("Email", f => f.OfType("TextField")));
Secondly, because you use your custom controller istead of orchard's content controller, you must implement the IUpdateModel and act on it:
[Admin]
public class MyController : Controller, IUpdateModel {
private readonly IContentManager _contentManager;
private readonly ITransactionManager _transactionManager;
public MyController(IContentManager contentManager,
ITransactionManager transactionManager) {
_contentManager = contentManager;
_transactionManager = transactionManager;
}
[HttpPost, ActionName("Create")]
public ActionResult CreatePOST()
{
var contentItem = _contentManager.New<CoachPart>("Coach");
// The implementation of IUpdateModel is necessary for this next line:
var model = _contentManager.UpdateEditor(contentItem, this);
if (!ModelState.IsValid) {
_transactionManager.Cancel();
return View(model);
}
_contentManager.Publish(contentItem);
return View("Index");
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}
void IUpdateModel.AddModelError(string key, LocalizedString errorMessage) {
ModelState.AddModelError(key, errorMessage.ToString());
}
}

Ignore member of base class in YamlDotNet

I have a class which I want to serialize with YamlDotNet:
public class AwesomeClass : PropertyChangedBase
{
private bool _element1;
private bool _enabled;
public bool Element1
{
get { return _element1; }
set
{
_element1 = value;
NotifyOfPropertyChange(() => Element1);
}
}
public bool Enabled
{
get { return _enabled; }
set
{
_enabled = value;
NotifyOfPropertyChange(() => Enabled);
}
}
}
My problem is, in the base class is an element named: IsNotifying
Is there a way to exclude this element from serialization, without the change of the base class?
You could override the property in the derived class and apply the YamlIgnore attribute there. While the sample below works, I suspect for more complicated class hierarchies you would really need to ensure no behavior changes.
public class AwesomeClass : PropertyChangedBase
{
[YamlIgnore]
public new bool IsNotifying
{
get { return base.IsNotifying; }
set { base.IsNotifying = value; }
}
[YamlIgnore]
public override bool Blah
{
get { return base.Blah; }
set { base.Blah = value; }
}
}
public class PropertyChangedBase
{
public bool IsNotifying
{
get;
set;
}
public virtual bool Blah
{
get;
set;
}
}
I had a similar problem (needed to filter properties of a particular type from classes I couldn't change, so using the attribute was not an option) and is what I came up with:
Create a custom type inspector:
public class MyTypeInspector : TypeInspectorSkeleton
{
private readonly ITypeInspector _innerTypeDescriptor;
public MyTypeInspector(ITypeInspector innerTypeDescriptor)
{
_innerTypeDescriptor = innerTypeDescriptor;
}
public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
{
var props = _innerTypeDescriptor.GetProperties(type, container);
props = props.Where(p => !(p.Type == typeof(Dictionary<string, object>) && p.Name == "extensions"));
props = props.Where(p => p.Name != "operation-id");
return props;
}
}
Create the serializer as follows:
var builder = new SerializerBuilder();
builder.WithTypeInspector(inspector => new MyTypeInspector(inspector));
var serializer = builder.Build();

Ninject summon graphs with argument

Here is my problem. I have a presenter class, lets call it 'Presenter' that takes an IDataSource as a constructor argument. There are different implementations of the IDataSource interface. I would like to be able to pass some argument to Ninject and based on that argument one of several IDataSource implementations should by used. I've provided some sample code below. I think that my solution is really ugly and that there must be a smarter, cleaner way to do this. How are you guys solving this type of problem?
Here is my sample code
public class Presenter
{
public Presenter(IDataSource dataSource)
{
DataSource = dataSource;
}
private IDataSource DataSource { get; set; }
public List<string> GetData()
{
return DataSource.GetAll();
}
}
public class InMemoryDataSource : IDataSource
{
public List<string> GetAll()
{
return new List<string> {"a", "b"};
}
}
public class DbDataSource : IDataSource
{
public List<string> GetAll()
{
return new List<string> { "1", "2" };
}
}
public interface IDataSource
{
List<string> GetAll();
}
public class Module : NinjectModule
{
public override void Load()
{
Bind<Presenter>().To<Presenter>().Named("Db");
Bind<Presenter>().To<Presenter>().Named("InMemory");
Bind<IDataSource>().To<InMemoryDataSource> ().WhenParentNamed("InMemory");
Bind<IDataSource>().To<DbDataSource>().WhenParentNamed("Db");
}
}
[Test]
public void Run()
{
using (var kernel = new StandardKernel(new Module()))
{
var p = kernel.Get<Presenter>(x => x.Name == "InMemory");
foreach(var s in p.GetData())
{
Console.Out.WriteLine(s);
}
}
}
This depends on what you want to do. I assume that you want to use a different db for testing than for production. In this case would create the module with the production configuration in mind and simply Rebind everything for testing:
public class Presenter
{
public Presenter(IDataSource dataSource)
{
DataSource = dataSource;
}
private IDataSource DataSource { get; set; }
public List<string> GetData()
{
return DataSource.GetAll();
}
}
public class InMemoryDataSource : IDataSource
{
public List<string> GetAll()
{
return new List<string> {"a", "b"};
}
}
public class DbDataSource : IDataSource
{
public List<string> GetAll()
{
return new List<string> { "1", "2" };
}
}
public interface IDataSource
{
List<string> GetAll();
}
public class Module : NinjectModule
{
public override void Load()
{
Bind<Presenter>().To<Presenter>();
Bind<IDataSource>().To<DbDataSource>();
}
}
[Test]
public void Run()
{
using (var kernel = new StandardKernel(new Module()))
{
kernel.Rebind<IDataSource>().To<InMemoryDataSource>();
var p = kernel.Get<Presenter>();
foreach(var s in p.GetData())
{
Console.Out.WriteLine(s);
}
}
}

Resources