38
38
39
39
public class Anagrafe extends Fragment {
40
40
41
- private List <Person > listaIndividui ;
42
- public AdattatoreAnagrafe adattatore ;
41
+ List <Person > people ;
42
+ AdattatoreAnagrafe adapter ;
43
43
private Order order ;
44
44
private boolean gliIdsonoNumerici ;
45
45
@@ -59,45 +59,45 @@ public Order prev() {
59
59
};
60
60
61
61
@ Override
62
- public View onCreateView ( LayoutInflater inflater , ViewGroup container , Bundle bandolo ) {
63
- View vista = inflater .inflate ( R .layout .ricicla_vista , container , false );
62
+ public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle bundle ) {
63
+ View vista = inflater .inflate (R .layout .ricicla_vista , container , false );
64
64
if ( gc != null ) {
65
- listaIndividui = gc .getPeople ();
65
+ people = gc .getPeople ();
66
66
arredaBarra ();
67
- RecyclerView vistaLista = vista .findViewById ( R .id .riciclatore );
68
- vistaLista .setPadding ( 12 , 12 , 12 , vistaLista .getPaddingBottom () );
69
- adattatore = new AdattatoreAnagrafe ();
70
- vistaLista .setAdapter ( adattatore );
67
+ RecyclerView vistaLista = vista .findViewById (R .id .riciclatore );
68
+ vistaLista .setPadding (12 , 12 , 12 , vistaLista .getPaddingBottom ());
69
+ adapter = new AdattatoreAnagrafe ();
70
+ vistaLista .setAdapter (adapter );
71
71
gliIdsonoNumerici = verificaIdNumerici ();
72
- vista .findViewById ( R .id .fab ).setOnClickListener ( v -> {
73
- Intent intento = new Intent ( getContext (), EditaIndividuo .class );
74
- intento .putExtra ( "idIndividuo" , "TIZIO_NUOVO" );
75
- startActivity ( intento );
72
+ vista .findViewById (R .id .fab ).setOnClickListener (v -> {
73
+ Intent intento = new Intent (getContext (), EditaIndividuo .class );
74
+ intento .putExtra ("idIndividuo" , "TIZIO_NUOVO" );
75
+ startActivity (intento );
76
76
});
77
77
}
78
78
return vista ;
79
79
}
80
80
81
81
void arredaBarra () {
82
- ((AppCompatActivity )getActivity ()).getSupportActionBar ().setTitle ( listaIndividui .size () + " "
83
- + getString (listaIndividui .size ()== 1 ? R .string .person : R .string .persons ).toLowerCase () );
84
- if ( listaIndividui .size () > 1 )
82
+ ((AppCompatActivity )getActivity ()).getSupportActionBar ().setTitle (people .size () + " "
83
+ + getString (people .size () == 1 ? R .string .person : R .string .persons ).toLowerCase ());
84
+ if ( people .size () > 1 )
85
85
setHasOptionsMenu (true );
86
86
else
87
87
setHasOptionsMenu (false );
88
88
}
89
89
90
90
public class AdattatoreAnagrafe extends RecyclerView .Adapter <GestoreIndividuo > implements Filterable {
91
91
@ Override
92
- public GestoreIndividuo onCreateViewHolder ( ViewGroup parent , int tipo ) {
93
- View vistaIndividuo = LayoutInflater .from ( parent .getContext () )
92
+ public GestoreIndividuo onCreateViewHolder (ViewGroup parent , int tipo ) {
93
+ View vistaIndividuo = LayoutInflater .from (parent .getContext ())
94
94
.inflate (R .layout .pezzo_individuo , parent , false );
95
- registerForContextMenu ( vistaIndividuo );
96
- return new GestoreIndividuo ( vistaIndividuo );
95
+ registerForContextMenu (vistaIndividuo );
96
+ return new GestoreIndividuo (vistaIndividuo );
97
97
}
98
98
@ Override
99
99
public void onBindViewHolder (GestoreIndividuo gestore , int posizione ) {
100
- Person person = listaIndividui .get (posizione );
100
+ Person person = people .get (posizione );
101
101
View vistaIndi = gestore .vista ;
102
102
103
103
String label = null ;
@@ -147,19 +147,19 @@ public Filter getFilter() {
147
147
protected FilterResults performFiltering (CharSequence charSequence ) {
148
148
String query = charSequence .toString ();
149
149
if (query .isEmpty ()) {
150
- listaIndividui = gc .getPeople ();
150
+ people = gc .getPeople ();
151
151
} else {
152
152
List <Person > filteredList = new ArrayList <>();
153
153
for (Person pers : gc .getPeople ()) {
154
154
if ( U .epiteto (pers ).toLowerCase ().contains (query .toLowerCase ()) ) {
155
155
filteredList .add (pers );
156
156
}
157
157
}
158
- listaIndividui = filteredList ;
158
+ people = filteredList ;
159
159
}
160
160
sortPeople ();
161
161
FilterResults filterResults = new FilterResults ();
162
- filterResults .values = listaIndividui ;
162
+ filterResults .values = people ;
163
163
return filterResults ;
164
164
}
165
165
@ Override
@@ -170,7 +170,7 @@ protected void publishResults(CharSequence cs, FilterResults fr) {
170
170
}
171
171
@ Override
172
172
public int getItemCount () {
173
- return listaIndividui .size ();
173
+ return people .size ();
174
174
}
175
175
}
176
176
@@ -256,7 +256,7 @@ boolean verificaIdNumerici() {
256
256
}
257
257
258
258
private void sortPeople () {
259
- Collections .sort (listaIndividui , (p1 , p2 ) -> {
259
+ Collections .sort (people , (p1 , p2 ) -> {
260
260
switch ( order ) {
261
261
case ID_ASC : // Sort for GEDCOM ID
262
262
if ( gliIdsonoNumerici )
@@ -392,41 +392,98 @@ private int calcAge(Person person) {
392
392
return days ;
393
393
}
394
394
395
- // riceve una persona e restitusce i due luoghi: iniziale – finale
396
- static String dueLuoghi ( Person p ) {
397
- String luoghi = "" ;
398
- List <EventFact > fatti = p .getEventsFacts ();
399
- for ( EventFact unFatto : fatti ) {
400
- if ( unFatto .getPlace () != null ) {
401
- luoghi = togliVirgole ( unFatto .getPlace () );
402
- break ;
395
+ // Write the two main places of a person (initial – final) or null
396
+ static String twoPlaces (Person person ) {
397
+ List <EventFact > facts = person .getEventsFacts ();
398
+ // One single event
399
+ if ( facts .size () == 1 ) {
400
+ String place = facts .get (0 ).getPlace ();
401
+ if ( place != null )
402
+ return stripCommas (place );
403
+ } // Sex and another event
404
+ else if ( facts .size () == 2 && ("SEX" .equals (facts .get (0 ).getTag ()) || "SEX" .equals (facts .get (1 ).getTag ())) ) {
405
+ String place ;
406
+ if ( "SEX" .equals (facts .get (0 ).getTag ()) )
407
+ place = facts .get (1 ).getPlace ();
408
+ else
409
+ place = facts .get (0 ).getPlace ();
410
+ if ( place != null )
411
+ return stripCommas (place );
412
+ } // Multiple events
413
+ else if ( facts .size () >= 2 ) {
414
+ String [] places = new String [7 ];
415
+ for ( EventFact ef : facts ) {
416
+ String place = ef .getPlace ();
417
+ if ( place != null ) {
418
+ switch ( ef .getTag () ) {
419
+ case "BIRT" :
420
+ places [0 ] = place ;
421
+ break ;
422
+ case "BAPM" :
423
+ places [1 ] = place ;
424
+ break ;
425
+ case "DEAT" :
426
+ places [4 ] = place ;
427
+ break ;
428
+ case "CREM" :
429
+ places [5 ] = place ;
430
+ break ;
431
+ case "BURI" :
432
+ places [6 ] = place ;
433
+ break ;
434
+ default :
435
+ if ( places [2 ] == null ) // First of other events
436
+ places [2 ] = place ;
437
+ if ( !place .equals (places [2 ]) )
438
+ places [3 ] = place ; // Last of other events
439
+ }
440
+ }
403
441
}
404
- }
405
- for (int i =fatti .size ()-1 ; i >=0 ; i -- ) {
406
- String secondoLuogo = fatti .get (i ).getPlace ();
407
- if ( secondoLuogo != null ) {
408
- secondoLuogo = togliVirgole (secondoLuogo );
409
- if ( !secondoLuogo .equals (luoghi ) )
410
- luoghi = luoghi .concat (" – " ).concat (secondoLuogo );
411
- break ;
442
+ String text = null ;
443
+ int i ;
444
+ // Write initial place
445
+ for ( i = 0 ; i < places .length ; i ++ ) {
446
+ String place = places [i ];
447
+ if ( place != null ) {
448
+ text = stripCommas (place );
449
+ break ;
450
+ }
451
+ }
452
+ // Priority to death event as final place
453
+ if ( text != null && i < 4 && places [4 ] != null ) {
454
+ String place = stripCommas (places [4 ]);
455
+ if ( !place .equals (text ) )
456
+ text += " – " + place ;
457
+ } else {
458
+ for ( int j = places .length - 1 ; j > i ; j -- ) {
459
+ String place = places [j ];
460
+ if ( place != null ) {
461
+ place = stripCommas (place );
462
+ if ( !place .equals (text ) ) {
463
+ text += " – " + place ;
464
+ break ;
465
+ }
466
+ }
467
+ }
412
468
}
469
+ return text ;
413
470
}
414
- return luoghi ;
471
+ return null ;
415
472
}
416
473
417
474
// riceve un luogo stile Gedcom e restituisce il primo nome tra le virgole
418
- private static String togliVirgole ( String luogo ) {
475
+ private static String stripCommas ( String place ) {
419
476
// salta le virgole iniziali per luoghi tipo ',,,England'
420
- int iniz = 0 ;
421
- for ( char c : luogo .toCharArray () ) {
422
- if ( c != ',' && c != ' ' )
477
+ int start = 0 ;
478
+ for ( char c : place .toCharArray () ) {
479
+ if ( c != ',' && c != ' ' )
423
480
break ;
424
- iniz ++;
481
+ start ++;
425
482
}
426
- luogo = luogo .substring (iniz );
427
- if ( luogo .indexOf ("," ) > 0 )
428
- luogo = luogo .substring ( 0 , luogo .indexOf ("," ) );
429
- return luogo ;
483
+ place = place .substring (start );
484
+ if ( place .indexOf ("," ) > 0 )
485
+ place = place .substring (0 , place .indexOf ("," ));
486
+ return place ;
430
487
}
431
488
432
489
/** Count how many near relatives a person has: parents, siblings, step-siblings, spouses and children.
@@ -491,7 +548,7 @@ public void onCreateOptionsMenu( Menu menu, MenuInflater inflater ) {
491
548
vistaCerca .setOnQueryTextListener ( new SearchView .OnQueryTextListener () {
492
549
@ Override
493
550
public boolean onQueryTextChange ( String query ) {
494
- adattatore .getFilter ().filter (query );
551
+ adapter .getFilter ().filter (query );
495
552
return true ;
496
553
}
497
554
@ Override
@@ -513,7 +570,7 @@ else if( order == Order.values()[id * 2] )
513
570
else
514
571
order = Order .values ()[id * 2 - 1 ];
515
572
sortPeople ();
516
- adattatore .notifyDataSetChanged ();
573
+ adapter .notifyDataSetChanged ();
517
574
//U.salvaJson( false ); // dubbio se metterlo per salvare subito il riordino delle persone...
518
575
return true ;
519
576
}
@@ -551,7 +608,7 @@ public boolean onContextItemSelected( MenuItem item ) {
551
608
new AlertDialog .Builder (getContext ()).setMessage ( R .string .really_delete_person )
552
609
.setPositiveButton ( R .string .delete , (dialog , i ) -> {
553
610
Family [] famiglie = eliminaPersona (getContext (), idIndi );
554
- adattatore .notifyDataSetChanged ();
611
+ adapter .notifyDataSetChanged ();
555
612
arredaBarra ();
556
613
U .controllaFamiglieVuote (getContext (), null , false , famiglie );
557
614
}).setNeutralButton ( R .string .cancel , null ).show ();
@@ -603,4 +660,4 @@ static Family[] eliminaPersona(Context contesto, String idEliminando) {
603
660
U .salvaJson ( true , (Object [])famiglie );
604
661
return famiglie ;
605
662
}
606
- }
663
+ }
0 commit comments