Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Once you have created register activity, next step is to create the fragment that is actually reponsible for showing list records i.e. main grid. The example is here.

As shown above there are different parts of Main grid which can be controlled by implementing SecuredNativeSmartRegisterFragment.

  1. Register Label
  2. Sort Option
  3. Filter Option
  4. Service Mode
  5. Client Action Handler
  6. Search Filter Option
  7. Search Type
  8. Default Options
  9. Navigation Options
  10. Custom Form Handler
  11. New Record Button
  12. Adapter


Code Block
languagejava
themeEclipse
titleExample Base Register Fragment
public class MyRegisterFragment extends SecuredNativeSmartRegisterFragment {
    private final ClientActionHandler clientActionHandler = new ClientActionHandler();

    public MyRegisterFragment() {
        super(null);
    }

    @SuppressLint("ValidFragment")
    public MyRegisterFragment(FormController formController) {
        super(formController);
    }

	// Default settings for Sort, Main Filter, Location, Service Mode, Search Filter and Search Type
    @Override
    protected SecuredNativeSmartRegisterActivity.DefaultOptionsProvider getDefaultOptionsProvider() {
        return new SecuredNativeSmartRegisterActivity.DefaultOptionsProvider() {

            @Override
            public SearchFilterOption searchFilterOption() {
                return new MySearchOption("");
            }

            @Override
            public ServiceModeOption serviceMode() {
                return new MyServiceModeOption(null, "My Service Mode Label", 
						new int[]{/* Resource ids for each header */ R.id.column1, R.id.column2, /*.....*/}, 
						// Weight for each header. Should be equal to weight of columns defined in client_record_layout.xml
						new int[]{6,2,3,2,1});
            }
            @Override
            public FilterOption villageFilter() {
                return new CursorCommonObjectFilterOption("no village filter", "");
            }
            @Override
            public SortOption sortOption() {
                return new CursorCommonObjectSort(getResources().getString(R.string.alphabetical_sort_text), "sort_column DESC");
            }
            @Override
            public String nameInShortFormForTitle() {
                return Context.getInstance().getStringResource(R.string.my_register_title);
            }
            @Override
            public SecuredNativeSmartRegisterActivity.SearchType searchType() {
                return SecuredNativeSmartRegisterActivity.SearchType.PASSIVE;
            }
        };
    }

    @Override
    protected SecuredNativeSmartRegisterActivity.NavBarOptionsProvider getNavBarOptionsProvider() {
        return new SecuredNativeSmartRegisterActivity.NavBarOptionsProvider() {

            @Override
            public DialogOption[] filterOptions() {
                return new DialogOption[]{};
            }
            @Override
            public DialogOption[] serviceModeOptions() {
                return new DialogOption[]{};
            }
            @Override
            public DialogOption[] sortingOptions() {
                return new DialogOption[]{
                        new CursorCommonObjectSort(getResources().getString(R.string.sort_type1), "my_column1 ASC"),
                        new CursorCommonObjectSort(getResources().getString(R.string.sort_type2), "my_column2 DESC"),
                        new CursorCommonObjectSort(getResources().getString(R.string.sort_dob_age), "my_dob_column DESC")
                };
            }
            @Override
            public String searchHint() {
                return Context.getInstance().getStringResource(R.string.text_to_display_searchbox);
            }
        };
    }

    @Override
    protected void onInitialization(){
		// A handler where form submission would be routed for further processing after successful save
        context.formSubmissionRouter().getHandlerMap().put("my_form1", new MyCustomForm1Handler(getActivity()));
    }

    @Override
    protected void onCreation() {
		// Custom tasks after fragment creation
	}

    // What app should do on click of New Record Button. It can launch QR Code activity or can also open a form directly
    @Override
    protected void startRegistration() {
		// Any fields in form needs to be overriden.. A usecase could be load data from DB and send to form
		Map<String, String> overrides = new HashMap<>();		
		overrides.put("my_form_field1", "abc");
        startForm("my_form1", "", overrides);
    }

    @Override
    protected SmartRegisterPaginatedAdapter adapter() {
        /*if(VaccinatorUtils.providerRolesList().toLowerCase().contains("vaccinator")){
            return new SmartRegisterPaginatedCursorAdapter(getActivity(),
                    new SmartRegisterCursorBuilder("client", null, (CursorSortOption) getDefaultOptionsProvider().sortOption(), "baseEntityId")
                    , clientsProvider(), SmartRegisterCursorBuilder.DB.OPENSRP);
        } else {*/
            return new SmartRegisterPaginatedCursorAdapter(getActivity(),
                    new SmartRegisterCursorBuilder("pkhousehold", null, "H",
                            new String[]{"(SELECT count(1) FROM pkindividual WHERE household_id=H.household_id) registeredMembers"},
                            (CursorSortOption) getDefaultOptionsProvider().sortOption(), "").limit(5)
                    , clientsProvider(), SmartRegisterCursorBuilder.DB.DRISHTI);
        //}
    }

    @Override
    protected SmartRegisterClientsProvider clientsProvider() {
        return new HouseholdSmartClientsProvider(getActivity(), clientActionHandler, context.alertService());
    }

    private SmartRegisterClients filterHousehold(String filterString) {
        setCurrentSearchFilter(new HouseholdIDSearchOption(filterString));
        onFilterManual(filterString);
        return getClientsAdapter().currentPageList();
    }

    private List<CommonPersonObject> filterHouseholdMembers(String householdId) {
        String memberExistQuery = "select * from pkindividual where household_id = '"+householdId+"' ";

        return context.allCommonsRepositoryobjects("pkindividual").customQueryForCompleteRow(memberExistQuery, new String[]{}, "pkindividual");
    }

    public CommonPersonObject filterHouseholdMember(String hhMemberId){
        String memberExistQuery = "select * from pkindividual where program_client_id = '"+hhMemberId+"' " +
                " OR id = '"+hhMemberId+"' OR household_member_id = '"+hhMemberId+"'";

        List<CommonPersonObject> memberData = context.allCommonsRepositoryobjects("pkindividual").customQueryForCompleteRow(memberExistQuery, new String[]{}, "pkindividual");
        CommonPersonObject householdMember;
        if (memberData.size() == 0) {
            return null;
        } else {
            householdMember = memberData.get(0);
            String householdId = householdMember.getColumnmaps().get("household_id");
            setCurrentSearchFilter(new HouseholdIDSearchOption(householdId));
            onFilterManual(householdId);
        }

        return householdMember;
    }

    public CommonPersonObject vaccinatorTables(String qrCode, String entity){
        String q = "select * from "+entity+" where program_client_id = " + qrCode;
        List<CommonPersonObject> memberData = context.allCommonsRepositoryobjects(entity).customQueryForCompleteRow(q, new String[]{}, entity);
        if (memberData.size() == 0) {
            return null;
        }
        return memberData.get(0);
    }

    private class ClientActionHandler implements View.OnClickListener {
        private HouseholdSmartRegisterFragment householdSmartRegisterFragment;

        public ClientActionHandler() {
            this.householdSmartRegisterFragment = householdSmartRegisterFragment;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.household_profile_info_layout:
                    ((SmartRegisterSecuredActivity)getActivity()).showDetailFragment((CommonPersonObjectClient) view.getTag(), false);
                    break;
                case R.id.household_add_member:
                    // change the below contains value according to your requirement
                    //if(!Utils.userRoles.contains("Vaccinator")) {
                    final CommonPersonObjectClient client = (CommonPersonObjectClient) view.getTag();
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                    LinearLayout ly = new LinearLayout(getActivity());
                    ly.setOrientation(LinearLayout.VERTICAL);

                    final RadioButton hasQRCode = new RadioButton(getActivity());
                    final RadioButton noQRCode = new RadioButton(getActivity());
                    hasQRCode.setText("Yes, member has a QR code ID");
                    noQRCode.setText("No, member doesnot have QR code ID");

                    RadioGroup rG = new RadioGroup(getActivity());
                    rG.setPadding(10, 10, 10, 5);
                    rG.addView(hasQRCode);
                    rG.addView(noQRCode);

                    final LinearLayout layout = new LinearLayout(getActivity());
                    layout.setOrientation(LinearLayout.HORIZONTAL);
                    TextView memberCodeQuestion = new TextView(getActivity());
                    memberCodeQuestion.setText("Has this member ever been registered in any other OpenSRP program and assigned a QR code (A QR code scanner screen will be launched to scan QR Code ID)?");
                    memberCodeQuestion.setTextSize(20);
                    layout.addView(memberCodeQuestion);

                    ly.addView(layout);
                    ly.addView(rG);

                    builder.setView(ly);

                    final AlertDialog alertDialog = builder.setPositiveButton("OK", null).setNegativeButton("Cancel", null).create();
                    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

                        @Override
                        public void onShow(final DialogInterface dialog) {
                            Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                            b.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View view) {
                                    if (noQRCode.isChecked()) {
                                        HashMap<String, String> map = new HashMap<>();
                                        Map<String, String> m = memberRegistrationOverrides(client, null, filterHouseholdMembers(client.getColumnmaps().get("household_id")));
                                        map.putAll(m);
                                        startNewMemberEnrollmentForm(map, client);
                                    }
                                    else if (hasQRCode.isChecked()) {
                                        startMemberRegistration(client.entityId());
                                    }

                                    dialog.dismiss();
                                }
                            });

                        }
                    });
                    alertDialog.show();
                    //}
                    break;
            }
        }
    }//end of method

    private Map<String, String> memberRegistrationOverrides(CommonPersonObjectClient client
            , CommonPersonObject existingClient, List<CommonPersonObject> otherMembers){
        Map<String, String> map = new HashMap<>();

        map.put("relationalid", client.getCaseId());
        map.put("existing_full_name_hhh", getValue(client.getColumnmaps(), "first_name", true));
        map.put("existing_household_id", getValue(client.getColumnmaps(), "household_id", true));
        map.put("existing_num_members", (otherMembers.size()+1)+"");
        map.put("existing_num_household_members", getValue(client.getColumnmaps(), "num_household_members", false));

        map.put("province", getValue(client.getColumnmaps(), "province", false));
        map.put("city_village", getValue(client.getColumnmaps(), "city_village", false));
        map.put("town", getValue(client.getColumnmaps(), "town", false));
        map.put("union_council", getValue(client.getColumnmaps(), "union_council", false));
        map.put("address1", getValue(client.getColumnmaps(), "address1", false));

        map.put("existing_full_address", getValue(client.getColumnmaps(), "address1", true)
                +", UC: "+ getValue(client.getColumnmaps(), "union_council", true).replace("Uc", "UC")
                +", Town: "+ getValue(client.getColumnmaps(), "town", true)
                +", City: "+ getValue(client.getColumnmaps(), "city_village", true)
                +", Province: "+ getValue(client.getColumnmaps(), "province", true));

        if (existingClient != null) {
            map.put("first_name", getValue(existingClient.getColumnmaps(), "first_name", false));
            map.put("gender", existingClient.getColumnmaps().get("gender"));
            map.put("birth_date", getValue(existingClient.getColumnmaps(), "dob", false));
            map.put("contact_phone_number", getValue(existingClient.getColumnmaps(), "contact_phone_number", false));
            map.put("ethnicity", getValue(existingClient.getDetails(), "ethnicity", false));
            map.put("ethnicity_other", getValue(existingClient.getDetails(), "ethnicity_other", false));
        }
        return map;
    }
}