Skip to content

Commit

Permalink
Merge pull request #318 from IITA-AKILIMO/fix/null-string
Browse files Browse the repository at this point in the history
fix/null string
  • Loading branch information
masgeek authored Sep 14, 2023
2 parents e479a32 + a451155 commit e323ae9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class BaseStepFragment extends Fragment implements Step {
protected String LOG_TAG = BaseStepFragment.class.getSimpleName();

protected String currency;
protected String countryCode;
protected String countryCode = "";
protected String countryName;
protected String errorMessage = "";

Expand Down Expand Up @@ -113,7 +113,7 @@ protected void showCustomWarningDialog(String titleText, String contentText, Str
title.setText(titleText);
content.setText(contentText);

if (!buttonTitle.isEmpty()) {
if (!buttonTitle.isEmpty() && buttonTitle.length() > 1) {
btnClose.setText(buttonTitle);
}
btnClose.setOnClickListener(view -> dialog.dismiss());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -18,17 +18,20 @@
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import com.blongho.country_data.World;

import com.google.android.gms.common.util.Strings;
import com.akilimo.mobile.R;
import com.akilimo.mobile.databinding.FragmentCountryBinding;
import com.akilimo.mobile.entities.ProfileInfo;
import com.akilimo.mobile.inherit.BaseStepFragment;
import com.akilimo.mobile.utils.enums.EnumCountry;
import com.blongho.country_data.World;
import com.stepstone.stepper.VerificationError;

;import io.sentry.Sentry;
import java.util.HashMap;
import java.util.Map;

import io.sentry.Sentry;

;

/**
* A simple {@link Fragment} subclass.
Expand All @@ -49,11 +52,7 @@ public class CountryFragment extends BaseStepFragment {
private String selectedLanguage = "";
private int selectedCountryIndex = -1;

private String[] countries = new String[]{
EnumCountry.Burundi.name(),
EnumCountry.Ghana.name(),
EnumCountry.Nigeria.name(),
EnumCountry.Tanzania.name(),
private String[] countries = new String[]{EnumCountry.Burundi.name(), EnumCountry.Ghana.name(), EnumCountry.Nigeria.name(), EnumCountry.Tanzania.name(),
// EnumCountry.Rwanda.name(),
};

Expand Down Expand Up @@ -117,13 +116,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

btnPickCountry.setOnClickListener(pickerDialog -> {
if (selectedLanguage.equalsIgnoreCase("sw")) {
countries = new String[]{
EnumCountry.Tanzania.name()
};
} else if (selectedLanguage.equalsIgnoreCase("rw")) {
// countries = new String[]{
// EnumCountry.Rwanda.name()
// };
countries = new String[]{EnumCountry.Tanzania.name()};
}


Expand All @@ -135,46 +128,27 @@ public void onClick(DialogInterface dialogInterface, int i) {
selectedCountryIndex = i;
}
});

Map<String, EnumCountry> countryMap = new HashMap<>();
countryMap.put("kenya", EnumCountry.Kenya);
countryMap.put("tanzania", EnumCountry.Tanzania);
countryMap.put("nigeria", EnumCountry.Nigeria);
countryMap.put("ghana", EnumCountry.Ghana);
countryMap.put("rwanda", EnumCountry.Rwanda);
countryMap.put("burundi", EnumCountry.Burundi);


builder.setPositiveButton(context.getString(R.string.lbl_ok), (dialogInterface, whichButton) -> {
if (selectedCountryIndex >= 0) {
if (selectedCountryIndex >= 0 && countries.length > 0) {
countryName = countries[selectedCountryIndex];
switch (countryName.toLowerCase()) {
case "kenya":
countryName = EnumCountry.Kenya.name();
currency = EnumCountry.Kenya.currency();
countryCode = EnumCountry.Kenya.countryCode();
break;
case "tanzania":
countryName = EnumCountry.Tanzania.name();
currency = EnumCountry.Tanzania.currency();
countryCode = EnumCountry.Tanzania.countryCode();
break;
case "nigeria":
countryName = EnumCountry.Nigeria.name();
currency = EnumCountry.Nigeria.currency();
countryCode = EnumCountry.Nigeria.countryCode();
break;
case "ghana":
countryName = EnumCountry.Ghana.name();
currency = EnumCountry.Ghana.currency();
countryCode = EnumCountry.Ghana.countryCode();
break;
case "rwanda":
countryName = EnumCountry.Rwanda.name();
currency = EnumCountry.Rwanda.currency();
countryCode = EnumCountry.Rwanda.countryCode();
break;
case "burundi":
countryName = EnumCountry.Burundi.name();
currency = EnumCountry.Burundi.currency();
countryCode = EnumCountry.Burundi.countryCode();
break;
default:
countryName = EnumCountry.Other.name();
currency = EnumCountry.Other.currency();
countryCode = EnumCountry.Other.countryCode();
break;
EnumCountry selectedCountry = countryMap.get(countryName.toLowerCase());
if (selectedCountry == null) {
selectedCountry = EnumCountry.Other;
}

countryName = selectedCountry.name();
currency = selectedCountry.currency();
countryCode = selectedCountry.countryCode();
countryImage.setImageResource(World.getFlagOf(countryCode));
txtCountryName.setText(countryName);
dialogInterface.dismiss();
Expand Down Expand Up @@ -203,7 +177,7 @@ private void updateSelectedCountry() {
profileInfo.setCountryName(countryName);
profileInfo.setCurrency(currency);

dataIsValid = !Strings.isEmptyOrWhitespace(countryCode);
dataIsValid = !TextUtils.isEmpty(countryCode);
if (profileInfo.getProfileId() != null) {
int id = profileInfo.getProfileId();
if (id > 0) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private void reloadLocationInfo() {
public VerificationError verifyStep() {
reverseGeoCode(currentLat, currentLon);
//verify the selected country matches the one specified earlier
if (countryCode != null && !userSelectedCountryCode.equalsIgnoreCase(countryCode)) {
if (!countryCode.isEmpty() && !userSelectedCountryCode.equalsIgnoreCase(countryCode)) {
return new VerificationError(String.format(getString(R.string.lbl_unsupported_location), countryName, userSelectedCountryName));
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.akilimo.mobile.databinding.FragmentTillageOperationBinding;
import com.akilimo.mobile.entities.CurrentPractice;
Expand Down Expand Up @@ -76,8 +72,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
chkPloughing = binding.chkPloughing;
chkRidging = binding.chkRidging;

final FragmentManager fm = getActivity().getSupportFragmentManager();

chkPloughing.setOnCheckedChangeListener((buttonView, checked) -> {
operation = checked ? "Plough" : "NA";
performPloughing = checked;
Expand Down Expand Up @@ -142,8 +136,8 @@ private void onCheckboxClicked(boolean checked) {
operationTypeDialogFragment.show(getParentFragmentManager(), OperationTypeDialogFragment.ARG_ITEM_ID);


operationTypeDialogFragment.setOnDismissListener((operation, enumOperationType, cancelled) -> {
switch (operation) {
operationTypeDialogFragment.setOnDismissListener((dialogOperation, enumOperationType, cancelled) -> {
switch (dialogOperation) {
case "Plough":
performPloughing = !cancelled;
chkPloughing.setChecked(!cancelled);
Expand All @@ -156,8 +150,7 @@ private void onCheckboxClicked(boolean checked) {
break;
default:
ridgingMethod = EnumOperationType.NONE.operationName();
performPloughing = false;
performRidging = false;
performPloughing = performRidging = false;
break;
}
saveEntities();
Expand Down Expand Up @@ -219,6 +212,6 @@ public void onSelected() {

@Override
public void onError(@NonNull VerificationError error) {

//Not implemented
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.akilimo.mobile.Locales;
import com.akilimo.mobile.R;
import com.akilimo.mobile.adapters.MySpinnerAdapter;
Expand All @@ -26,6 +23,8 @@
import com.akilimo.mobile.interfaces.IFragmentCallBack;
import com.akilimo.mobile.utils.enums.EnumCountry;
import com.akilimo.mobile.views.activities.HomeStepperActivity;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import com.jakewharton.processphoenix.ProcessPhoenix;
import com.stepstone.stepper.VerificationError;

Expand Down Expand Up @@ -83,12 +82,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
prefs = new SharedPrefsAppLocaleRepository(context);
profileInfo = database.profileInfoDao().findOne();

languagePicker.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
languagePicked = true;
return false;
}
languagePicker.setOnTouchListener((touchView, motionEvent) -> {
languagePicked = true;
touchView.performClick();
return touchView.onTouchEvent(motionEvent);
});
languagePicker.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
Expand All @@ -98,17 +95,18 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
selectedLocale = AppLocale.getSupportedLocales().get(selectedLanguageIndex);

AppLocale.setDesiredLocale(selectedLocale);
SharedPrefsAppLocaleRepository prefs = new SharedPrefsAppLocaleRepository(context);
prefs = new SharedPrefsAppLocaleRepository(context);
prefs.setDesiredLocale(selectedLocale);
AppLocale.setAppLocaleRepository(prefs);

AppLocale.setDesiredLocale(selectedLocale);

//noinspection DataFlowIssue
final View rootView = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
Reword.reword(rootView);
Intent intent = new Intent(context, HomeStepperActivity.class);
Snackbar snackBar = Snackbar
.make(binding.lytParent, getString(R.string.lbl_restart_app_prompt), Snackbar.LENGTH_INDEFINITE)
.make(binding.lytParent, getString(R.string.lbl_restart_app_prompt), BaseTransientBottomBar.LENGTH_INDEFINITE)
.setAction(context.getString(R.string.lbl_ok), snackView -> ProcessPhoenix.triggerRebirth(context, intent));
snackBar.show();
initSpinnerItems();
Expand All @@ -118,6 +116,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long

@Override
public void onNothingSelected(AdapterView<?> parent) {
//not implemented
}
});

Expand Down Expand Up @@ -173,19 +172,15 @@ public VerificationError verifyStep() {

@Override
public void onSelected() {
//not implemented
}

@Override
public void onError(@NonNull VerificationError error) {
//not implemented
}

public void setOnFragmentCloseListener(IFragmentCallBack callBack) {
this.fragmentCallBack = callBack;
}

private void reloadView() {
if (fragmentCallBack != null) {
fragmentCallBack.reloadView();
}
}
}

0 comments on commit e323ae9

Please sign in to comment.