Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would you setup defaults? #1

Open
IsaiChristian opened this issue May 17, 2022 · 8 comments
Open

How would you setup defaults? #1

IsaiChristian opened this issue May 17, 2022 · 8 comments

Comments

@IsaiChristian
Copy link

Hi! i've tried your widget and it is great thanks for the great work.

I'm wondering how would you do to have a default or set position for widgets ?
I would like to use the dashboard to be able to swap pieces of the UI and make my app more customizable, but when I make the window smaller and bigger again, the order I setup wont be respected.

Do you think this is something I can modify being very new in dart/flutter?

@Mehmetyaz
Copy link
Owner

First of all I'm wondering if you are using storageDelegate.
But for now, I can say this. If you specify its location while constructing a dashboard item (DashboardItem(startX,startY)), it will try that location first for relocations.
Performs a reposition when the slot count is changed or when it is first opened (if slideToTop is true). Here is the sequence followed.
First, those whose location value is specified specifically are tried to be placed in the position. Later others. Those who could not be placed in this process are the last ones.
If an item was originally created by entering its location(startX,startY), they have the haveLocation property and take precedence in relocation.
Can you share the relevant part of the code so that I can better assist you with your problem.

@UlisesJHR
Copy link

Hi, I'm part of the team of the previous comment, Yes, we are using storageDelegate, Yes, we are specifying the location while constructing.

We have five widgets per row, 12 widgets in total, one widget is sized as 3x5 and goes after the first 5 widgets,
so it goes something like this:
1-1-1-1-1
3-3-3-1-1
3-3-3-1-1
3-3-3-1-1

but, each time the page or a widget changes its size, everything relocates without control and the big widget keeps being relocated at the middle..
We tried constructing the dashboardItem again with set startX and startY, and recreating the item from onItemsUpdated method of delegate

beyond that, the code is almost identical to the example in your repo.

@Mehmetyaz
Copy link
Owner

Hello. I will try this myself as soon as possible. If you share the relevant parts of the code, I can better understand where I went wrong.

@UlisesJHR
Copy link

UlisesJHR commented Jun 9, 2022

Hi, yes of course, it's pretty much the same as your example's code, but with set startX and startY on the items, so when an item is added or the whole grid changed slots count, we expect for this items to return to their original place

class MyItemStorage extends DashboardItemStorageDelegate<ColoredDashboardItem> {
  late SharedPreferences _preferences;

  Map<String, Map<String, ColoredDashboardItem>>? _localItems;

  @override
  FutureOr<List<ColoredDashboardItem>> getAllItems(int slotCount) {
    try {
      if (_localItems != null) {
        return _localItems!['items']!.values.toList();
      } else {
        return Future.microtask(() async {
          _preferences = await SharedPreferences.getInstance();

          var init = _preferences.getBool("init") ?? false;

          if (!init) {
            _localItems = {
              "items": defaultColoredDashboardItems
                  .asMap()
                  .map((key, value) => MapEntry(value.identifier, value))
            };

            _localItems;

            _preferences.setString(
                "items",
                json.encode(defaultColoredDashboardItems.asMap().map(
                    (int key, DashboardItem value) =>
                        MapEntry(value.identifier, value.toMap()))));

            await _preferences.setBool("init", true);
          }

          Map js = json.decode(_preferences.getString("items")!);

          js;

          return js.values
              .map<ColoredDashboardItem>(
                  (value) => ColoredDashboardItem.fromMap(value))
              .toList();
        });
      }
    } on Exception {
      rethrow;
    }
  }

  @override
  FutureOr<void> onItemsUpdated(
      List<ColoredDashboardItem> items, int slotCount) async {
    try {
      if (_localItems == null) {
        final result = await getAllItems(slotCount);
        _localItems = {
          'items': result
              .asMap()
              .map((key, value) => MapEntry(value.identifier, value))
        };
        _localItems;
      }

      for (var item in items) {
        _localItems!['items']![item.identifier] = item;
      }
      _localItems;

      await _preferences.setString(
          'items',
          jsonEncode(_localItems!['items']!.values.toList().asMap().map(
              (int key, DashboardItem value) =>
                  MapEntry(value.identifier, value.toMap()))));
    } on Exception catch (e) {
      print(e);
    }
  }

  @override
  FutureOr<void> onItemsAdded(
      List<ColoredDashboardItem> items, int slotCount) async {
    try {
      if (_localItems == null) {
        final result = await getAllItems(slotCount);
        _localItems = {
          'items': result
              .asMap()
              .map((key, value) => MapEntry(value.identifier, value))
        };
        _localItems;
      }

      for (var item in items) {
        _localItems!['items']![item.identifier] = item;
      }
      _localItems;

      await _preferences.setString(
          'items',
          jsonEncode(_localItems!['items']!.values.toList().asMap().map(
              (int key, DashboardItem value) =>
                  MapEntry(value.identifier, value.toMap()))));
    } on Exception catch (e) {
      print(e);
    }
  }

  @override
  FutureOr<void> onItemsDeleted(
      List<ColoredDashboardItem> items, int slotCount) async {
    try {
      for (var items in items) {
        _localItems!['items']?.remove(items.identifier);
      }
      _preferences.setString(
          'items',
          jsonEncode(_localItems!['items']!.values.toList().asMap().map(
              (int key, DashboardItem value) =>
                  MapEntry(value.identifier, value.toMap()))));

      _localItems;
      _preferences;
    } on Exception catch (e) {
      print(e);
    }
  }

  Future<void> clear() async {
    _localItems?.clear();
    await _preferences.setString(
        "items",
        json.encode(defaultColoredDashboardItems.asMap().map(
            (int key, DashboardItem value) =>
                MapEntry(value.identifier, value.toMap()))));
    _localItems = null;
    await _preferences.setBool("init", false);
  }

  @override
  bool get layoutsBySlotCount => true;

  @override
  bool get cacheItems => true;
}

@scalz
Copy link

scalz commented Jun 15, 2022

Hi,
@Mehmetyaz thx for sharing your work, it is really a neat widget.

I think I noticed something similar, I planned to make a small a reproducible example but didn't get time yet.
When I added widgets with startx and starty they were relocated too.
I didn't dig a lot where the issue really comes from, but fixed it by doing a (probably dirty?) workaround as I didn't want to do many changes which could lead to side effects without digging.

In dashboardcontroller.dart, in mountToTop function, I added this before the while(true) loop, for test and it fixed my issue of unwanted relocation when startx/starty are set and valid. If not valid, then the while loop does its job of relocating to a free area. But the real issue is probably somewhere else, it's just a workaround I think..

Capture

I hope this will help you to debug.. :)

@Mehmetyaz
Copy link
Owner

Thanks everyone. First of all, I opened a special project for this problem and I will try every possibility one by one. Can you try as seen in this example until the issue is resolved? (I don't know if there is a solution for you.)

An update that fixes this issue, along with a few new features, is coming soon.

The setting in this example is: Dashboard.slideToTop:false , Dashboard.shrinkToPlace:true

https://youtu.be/zQeVPoynXZY

@IsaiChristian
Copy link
Author

@Mehmetyaz Hi! do you have any estimated time for the new version? in our app we are going to start messing with it and I would like to start after you do your last changes.

@Mehmetyaz
Copy link
Owner

Hi! Sorry for late. I have a job to deliver on September 15th. I added the Dashboard to my program. I have a task related to Dashboard that will be finished in a few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants