Browse Source

Fixed fragments to use arguments

develop
Robin Thoni 6 years ago
parent
commit
c5796a3714

+ 15
- 14
app/src/main/java/com/rthoni/camotion/ui/AddCommandShortcutActivity.java View File

14
 import com.rthoni.camotion.dbo.LocationDbo;
14
 import com.rthoni.camotion.dbo.LocationDbo;
15
 import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
15
 import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
16
 import com.rthoni.camotion.ui.dialogs.LoginDialog;
16
 import com.rthoni.camotion.ui.dialogs.LoginDialog;
17
-import com.rthoni.camotion.ui.fragments.AbstractCommandsFragment;
17
+import com.rthoni.camotion.ui.fragments.CommandsFragment;
18
 
18
 
19
 import io.fabric.sdk.android.Fabric;
19
 import io.fabric.sdk.android.Fabric;
20
 
20
 
21
-public class AddCommandShortcutActivity extends Activity {
21
+public class AddCommandShortcutActivity extends Activity implements CommandsFragment.OnCommandClickedInterface {
22
+
23
+    private LocationDbo _currentLocation = null;
22
 
24
 
23
     @Override
25
     @Override
24
     protected void onCreate(Bundle savedInstanceState) {
26
     protected void onCreate(Bundle savedInstanceState) {
52
         dlg.loginFull(location).then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
54
         dlg.loginFull(location).then(new LuPromise.LuConsumer<LuFullLoginDbo>() {
53
             @Override
55
             @Override
54
             public void execute(LuFullLoginDbo loginDbo) {
56
             public void execute(LuFullLoginDbo loginDbo) {
55
-                showCommands(loginDbo, location);
57
+                _currentLocation = location;
58
+                showCommands(loginDbo);
56
             }
59
             }
57
         }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
60
         }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
58
             @Override
61
             @Override
63
         });
66
         });
64
     }
67
     }
65
 
68
 
66
-    private void showCommands(final LuFullLoginDbo loginDbo, final LocationDbo location)
69
+    private void showCommands(final LuFullLoginDbo loginDbo)
67
     {
70
     {
68
-        AbstractCommandsFragment fragment = new AbstractCommandsFragment() {
69
-            @Override
70
-            protected void onCommandClicked(CommandDbo command) {
71
-                addShortcut(location, command);
72
-            }
73
-        };
74
-        fragment.setCamotionDbo(loginDbo, location);
71
+        CommandsFragment fragment = new CommandsFragment();
72
+        Bundle args = new Bundle();
73
+        args.putString("CAMOTION_LOGIN_DBO", loginDbo.toString());
74
+        args.putString("CAMOTION_LOCATION_DBO", _currentLocation.toString());
75
+        fragment.setArguments(args);
75
         getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
76
         getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
76
     }
77
     }
77
 
78
 
78
-    private void addShortcut(LocationDbo location, CommandDbo command)
79
-    {
79
+    @Override
80
+    public void onCommandClicked(CommandDbo command) {
80
         Intent shortcutIntent = new Intent(this, CommandExecActivity.class);
81
         Intent shortcutIntent = new Intent(this, CommandExecActivity.class);
81
-        shortcutIntent.putExtra("LOCATION_PSEUDO_MAIL", location.getPseudoMail());
82
+        shortcutIntent.putExtra("LOCATION_PSEUDO_MAIL", _currentLocation.getPseudoMail());
82
         shortcutIntent.putExtra("COMMAND", command.toString());
83
         shortcutIntent.putExtra("COMMAND", command.toString());
83
         Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_command_bg);
84
         Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_command_bg);
84
 
85
 

+ 13
- 4
app/src/main/java/com/rthoni/camotion/ui/MainActivity.java View File

4
 import android.content.res.Configuration;
4
 import android.content.res.Configuration;
5
 import android.os.Bundle;
5
 import android.os.Bundle;
6
 import android.os.PersistableBundle;
6
 import android.os.PersistableBundle;
7
-import android.provider.Settings;
8
 import android.support.design.widget.NavigationView;
7
 import android.support.design.widget.NavigationView;
9
 import android.support.v4.view.GravityCompat;
8
 import android.support.v4.view.GravityCompat;
10
 import android.support.v4.widget.DrawerLayout;
9
 import android.support.v4.widget.DrawerLayout;
23
 import com.luticate.utils.business.LuRequest;
22
 import com.luticate.utils.business.LuRequest;
24
 import com.rthoni.camotion.R;
23
 import com.rthoni.camotion.R;
25
 import com.rthoni.camotion.business.CamotionBusiness;
24
 import com.rthoni.camotion.business.CamotionBusiness;
25
+import com.rthoni.camotion.dbo.CommandDbo;
26
 import com.rthoni.camotion.dbo.LocationDbo;
26
 import com.rthoni.camotion.dbo.LocationDbo;
27
+import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
27
 import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
28
 import com.rthoni.camotion.ui.dialogs.LocationPickerDialog;
28
 import com.rthoni.camotion.ui.dialogs.LoginDialog;
29
 import com.rthoni.camotion.ui.dialogs.LoginDialog;
30
+import com.rthoni.camotion.ui.fragments.CommandsFragment;
29
 import com.rthoni.camotion.ui.fragments.CamerasFragment;
31
 import com.rthoni.camotion.ui.fragments.CamerasFragment;
30
 import com.rthoni.camotion.ui.fragments.CamotionFragment;
32
 import com.rthoni.camotion.ui.fragments.CamotionFragment;
31
-import com.rthoni.camotion.ui.fragments.CommandsFragment;
32
 import com.rthoni.camotion.ui.fragments.SensorsFragment;
33
 import com.rthoni.camotion.ui.fragments.SensorsFragment;
33
 
34
 
34
 import io.fabric.sdk.android.Fabric;
35
 import io.fabric.sdk.android.Fabric;
35
 import java.util.List;
36
 import java.util.List;
36
 
37
 
37
 public class MainActivity extends AppCompatActivity
38
 public class MainActivity extends AppCompatActivity
38
-        implements NavigationView.OnNavigationItemSelectedListener {
39
+        implements NavigationView.OnNavigationItemSelectedListener, CommandsFragment.OnCommandClickedInterface {
39
 
40
 
40
     private LuFullLoginDbo _fullLoginDbo = null;
41
     private LuFullLoginDbo _fullLoginDbo = null;
41
     private LocationDbo _currentLocation = null;
42
     private LocationDbo _currentLocation = null;
212
         } */else {
213
         } */else {
213
             return;
214
             return;
214
         }
215
         }
215
-        fragment.setCamotionDbo(_fullLoginDbo, _currentLocation);
216
+        Bundle args = new Bundle();
217
+        args.putString("CAMOTION_LOGIN_DBO", _fullLoginDbo.toString());
218
+        args.putString("CAMOTION_LOCATION_DBO", _currentLocation.toString());
219
+        fragment.setArguments(args);
216
         getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
220
         getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
217
     }
221
     }
218
 
222
 
247
     {
251
     {
248
         super.onConfigurationChanged(newConfig);
252
         super.onConfigurationChanged(newConfig);
249
     }
253
     }
254
+
255
+    @Override
256
+    public void onCommandClicked(CommandDbo command) {
257
+        CommandExecDialog.showDialog(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), this, command);
258
+    }
250
 }
259
 }

+ 0
- 41
app/src/main/java/com/rthoni/camotion/ui/fragments/AbstractCommandsFragment.java View File

1
-package com.rthoni.camotion.ui.fragments;
2
-
3
-import com.luticate.utils.business.LuPromise;
4
-import com.luticate.utils.dbo.LuMultipleDbo;
5
-import com.rthoni.camotion.business.CommandsBusiness;
6
-import com.rthoni.camotion.dbo.CommandDbo;
7
-import com.rthoni.camotion.ui.views.CommandView;
8
-
9
-/**
10
- *
11
- * Created by robin on 11/29/15.
12
- */
13
-public abstract class AbstractCommandsFragment extends CamotionFragment<CommandDbo, CommandView> {
14
-
15
-    @Override
16
-    protected LuPromise<LuMultipleDbo<CommandDbo>> getLoadPagePromise(int page, int perPage, String query) {
17
-        return CommandsBusiness.getAll(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), page, perPage)
18
-                .map(new LuPromise.LuConverter<CommandDbo.MultipleCommandDbo, LuMultipleDbo<CommandDbo>>() {
19
-                    @Override
20
-                    public LuMultipleDbo<CommandDbo> convert(CommandDbo.MultipleCommandDbo data) {
21
-                        return data;
22
-                    }
23
-                });
24
-    }
25
-
26
-    @Override
27
-    protected CommandView getDboView(final CommandDbo command) {
28
-        CommandView commandView = new CommandView(getActivity());
29
-        commandView.setCamotionDbo(_fullLoginDbo, _currentLocation);
30
-        commandView.setCommand(command);
31
-        commandView.setOnClickedListener(new LuPromise.LuConsumer<CommandDbo>() {
32
-            @Override
33
-            public void execute(CommandDbo command) {
34
-                onCommandClicked(command);
35
-            }
36
-        });
37
-        return commandView;
38
-    }
39
-
40
-    protected abstract void onCommandClicked(CommandDbo command);
41
-}

+ 23
- 4
app/src/main/java/com/rthoni/camotion/ui/fragments/CamotionFragment.java View File

1
 package com.rthoni.camotion.ui.fragments;
1
 package com.rthoni.camotion.ui.fragments;
2
 
2
 
3
+import android.content.Context;
4
+import android.os.Bundle;
3
 import android.view.View;
5
 import android.view.View;
4
 
6
 
5
 import com.luticate.auth.dbo.LuFullLoginDbo;
7
 import com.luticate.auth.dbo.LuFullLoginDbo;
7
 import com.luticate.utils.ui.fragments.ViewPaginationFragment;
9
 import com.luticate.utils.ui.fragments.ViewPaginationFragment;
8
 import com.rthoni.camotion.dbo.LocationDbo;
10
 import com.rthoni.camotion.dbo.LocationDbo;
9
 
11
 
12
+import org.json.JSONException;
13
+
10
 /**
14
 /**
11
  * Created by robin on 12/3/15.
15
  * Created by robin on 12/3/15.
12
  */
16
  */
14
     protected LuFullLoginDbo _fullLoginDbo = null;
18
     protected LuFullLoginDbo _fullLoginDbo = null;
15
     protected LocationDbo _currentLocation = null;
19
     protected LocationDbo _currentLocation = null;
16
 
20
 
17
-    public void setCamotionDbo(LuFullLoginDbo fullLoginDbo, LocationDbo currentLocation)
18
-    {
19
-        _fullLoginDbo = fullLoginDbo;
20
-        _currentLocation = currentLocation;
21
+    @Override
22
+    public void onAttach(Context context) {
23
+        super.onAttach(context);
24
+        try {
25
+            _fullLoginDbo = new LuFullLoginDbo();
26
+            _fullLoginDbo.fromString(getArguments().getString("CAMOTION_LOGIN_DBO"));
27
+
28
+            _currentLocation = new LocationDbo();
29
+            _currentLocation.fromString(getArguments().getString("CAMOTION_LOCATION_DBO"));
30
+        } catch (JSONException e) {
31
+            e.printStackTrace();
32
+        }
33
+    }
34
+
35
+    @Override
36
+    public void onDetach() {
37
+        super.onDetach();
38
+        _fullLoginDbo = null;
39
+        _currentLocation = null;
21
     }
40
     }
22
 }
41
 }

+ 51
- 4
app/src/main/java/com/rthoni/camotion/ui/fragments/CommandsFragment.java View File

1
 package com.rthoni.camotion.ui.fragments;
1
 package com.rthoni.camotion.ui.fragments;
2
 
2
 
3
+import android.app.Activity;
4
+import android.content.Context;
5
+
6
+import com.luticate.utils.business.LuPromise;
7
+import com.luticate.utils.dbo.LuMultipleDbo;
8
+import com.rthoni.camotion.business.CommandsBusiness;
3
 import com.rthoni.camotion.dbo.CommandDbo;
9
 import com.rthoni.camotion.dbo.CommandDbo;
4
-import com.rthoni.camotion.ui.dialogs.CommandExecDialog;
10
+import com.rthoni.camotion.ui.views.CommandView;
5
 
11
 
6
 /**
12
 /**
7
  *
13
  *
8
  * Created by robin on 11/29/15.
14
  * Created by robin on 11/29/15.
9
  */
15
  */
10
-public class CommandsFragment extends AbstractCommandsFragment {
16
+public class CommandsFragment extends CamotionFragment<CommandDbo, CommandView> {
17
+
18
+    public interface OnCommandClickedInterface {
19
+        void onCommandClicked(CommandDbo command);
20
+    }
21
+
22
+    private OnCommandClickedInterface onCommandClickedInterface;
23
+
24
+    @Override
25
+    public void onAttach(Context context) {
26
+        super.onAttach(context);
27
+
28
+        if (context instanceof Activity) {
29
+            onCommandClickedInterface = (OnCommandClickedInterface) context;
30
+        }
31
+    }
32
+
33
+    @Override
34
+    public void onDetach() {
35
+        super.onDetach();
36
+        onCommandClickedInterface = null;
37
+    }
38
+
39
+    @Override
40
+    protected LuPromise<LuMultipleDbo<CommandDbo>> getLoadPagePromise(int page, int perPage, String query) {
41
+        return CommandsBusiness.getAll(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), page, perPage)
42
+                .map(new LuPromise.LuConverter<CommandDbo.MultipleCommandDbo, LuMultipleDbo<CommandDbo>>() {
43
+                    @Override
44
+                    public LuMultipleDbo<CommandDbo> convert(CommandDbo.MultipleCommandDbo data) {
45
+                        return data;
46
+                    }
47
+                });
48
+    }
11
 
49
 
12
     @Override
50
     @Override
13
-    protected void onCommandClicked(CommandDbo command) {
14
-        CommandExecDialog.showDialog(_currentLocation.getConfig(_fullLoginDbo.getLoginDbo()), getActivity(), command);
51
+    protected CommandView getDboView(final CommandDbo command) {
52
+        CommandView commandView = new CommandView(getActivity());
53
+        commandView.setCamotionDbo(_fullLoginDbo, _currentLocation);
54
+        commandView.setCommand(command);
55
+        commandView.setOnClickedListener(new LuPromise.LuConsumer<CommandDbo>() {
56
+            @Override
57
+            public void execute(CommandDbo command) {
58
+                onCommandClickedInterface.onCommandClicked(command);
59
+            }
60
+        });
61
+        return commandView;
15
     }
62
     }
16
 }
63
 }

+ 2
- 2
luticateauth/src/main/java/com/luticate/auth/dbo/LuFullLoginDbo.java View File

53
     public HashMap<String, Object> toArray() {
53
     public HashMap<String, Object> toArray() {
54
         HashMap<String, Object> array = new HashMap<>();
54
         HashMap<String, Object> array = new HashMap<>();
55
         array.put("LoginDbo", _loginDbo.toArray());
55
         array.put("LoginDbo", _loginDbo.toArray());
56
-        JSONArray permissions = new JSONArray();
56
+        List<HashMap<String, Object>> permissions = new Vector<>();
57
         for (LuPermissionDbo perm : _permissions) {
57
         for (LuPermissionDbo perm : _permissions) {
58
-            permissions.put(perm.toArray());
58
+            permissions.add(perm.toArray());
59
         }
59
         }
60
         array.put("Permissions", permissions);
60
         array.put("Permissions", permissions);
61
         return array;
61
         return array;

+ 6
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java View File

14
 
14
 
15
     public abstract HashMap<String, Object> toArray();
15
     public abstract HashMap<String, Object> toArray();
16
 
16
 
17
+    public void fromString(String data) throws JSONException
18
+    {
19
+        JSONObject json = new JSONObject(data);
20
+        fromJson(json);
21
+    }
22
+
17
     public JSONObject toJson()
23
     public JSONObject toJson()
18
     {
24
     {
19
         return new JSONObject(toArray());
25
         return new JSONObject(toArray());

Loading…
Cancel
Save