kotlin Firebase Variable path - arrays

Quick question.
While I write away my data to my firebase database I'd like to order it.
since i'm making an Album collection list I'd love to have it like the picture underneath.. but with bands and albums tho.
the only problem is that I can't read my write string.. in this case $Uname
I can only read the data correctly if I hardcode the location in my .getReference()
my data upload code:
//values
val number = Number_tv.text.toString()
val Uname = Username_TV.text.toString()
val ref = FirebaseDatabase.getInstance().getReference("/userdata/$Uname/$number _key")
val user = User(Username_TV.text.toString(), Email_TV.text.toString(), albumimageURL)
//code
if (number.isEmpty()){
Toast.makeText(this, "Number is empty", Toast.LENGTH_SHORT).show()
}
else{
ref.setValue(user)
.addOnSuccessListener {
Log.d("mainacti", "upload succesful")
}
.addOnFailureListener {
Log.d("mainacti", "I do not work")
}}
}
my data download code:
val ref = FirebaseDatabase.getInstance().getReference("/userdata/Heinz")
ref.addListenerForSingleValueEvent(object: ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
val adapter = GroupAdapter<GroupieViewHolder>()
p0.children.forEach {
Log.d("Albumlist", it.toString())
val user = it.getValue(User::class.java)
if (user != null){
adapter.add(UserItem(user))
}
}
recyclerview_album.adapter = adapter
}
override fun onCancelled(error: DatabaseError) {
}
})
}
Both codes are in different classes
here's a Log for the reference ("/userdata")
2021-04-16 13:00:42.080 6914-6914/com.example.firetrier D/Albumlist: DataSnapshot { key = Hans, value = {1 _key={albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Fcb06a4c4-8795-4103-99ef-c2bd55498b2e?alt=media&token=7d3d305e-c266-41fb-b466-cab548fbdd35, email=hans#test.com, username=Hans}} }
2021-04-16 13:00:42.088 6914-6914/com.example.firetrier D/Albumlist: DataSnapshot { key = Heinz, value = {1 _key={albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Ff00f6651-9ccf-4533-8e1c-af103a1a17e6?alt=media&token=b6e4ad2f-cb4f-47d5-8f00-02bdc4765c00, email=Heinz#doof.com, username=Heinz}, 5 _key={albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Fc9bb80aa-15a2-48cb-b6f5-6b7a59b11d8d?alt=media&token=facd516d-c3e3-4b61-aa09-23ea9062ccd3, email=hans#test.com, username=Heinz}} }
is there a way to use the value instead of the key?
if I adapt the reference to one of the usernames this is the log
2021-04-16 13:05:54.028 7629-7629/com.example.firetrier D/Albumlist: DataSnapshot { key = 1 _key, value = {albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Ff00f6651-9ccf-4533-8e1c-af103a1a17e6?alt=media&token=b6e4ad2f-cb4f-47d5-8f00-02bdc4765c00, email=Heinz#doof.com, username=Heinz} }
2021-04-16 13:05:54.040 7629-7629/com.example.firetrier D/Albumlist: DataSnapshot { key = 5 _key, value = {albumimageURL=https://firebasestorage.googleapis.com/v0/b/fireupload-4be26.appspot.com/o/albumcover%2Fc9bb80aa-15a2-48cb-b6f5-6b7a59b11d8d?alt=media&token=facd516d-c3e3-4b61-aa09-23ea9062ccd3, email=hans#test.com, username=Heinz} }

Related

Tenant to tenant user migration in Azure Active Directory using Graph API

Is it possible to migrate users using the MS Graph API in Azure AD?
If so, please explain how to migrate users from one tenant to the other using the MS Graph API.
You can export the users with MS Graph. Note, you can't export the passwords. This means that you have to create a new password and share it with the users. Or choose a random password and let the users reset their password using the self-service password rest feature.
Here is an example how to export the users from a directly
public static async Task ListUsers(GraphServiceClient graphClient)
{
Console.WriteLine("Getting list of users...");
DateTime startTime = DateTime.Now;
Dictionary<string, string> usersCollection = new Dictionary<string, string>();
int page = 0;
try
{
// Get all users
var users = await graphClient.Users
.Request()
.Select(e => new
{
e.DisplayName,
e.Id
}).OrderBy("DisplayName")
.GetAsync();
// Iterate over all the users in the directory
var pageIterator = PageIterator<User>
.CreatePageIterator(
graphClient,
users,
// Callback executed for each user in the collection
(user) =>
{
usersCollection.Add(user.DisplayName, user.Id);
return true;
},
// Used to configure subsequent page requests
(req) =>
{
var d = DateTime.Now - startTime;
Console.WriteLine($"{string.Format(TIME_FORMAT, d.Days, d.Hours, d.Minutes, d.Seconds)} users: {usersCollection.Count}");
// Set a variable to the Documents path.
string filePrefix = "0";
if (usersCollection.Count >= 1000000)
{
filePrefix = usersCollection.Count.ToString()[0].ToString();
}
page++;
if (page >= 50)
{
page = 0;
string docPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), $"users_{filePrefix}.json");
System.IO.File.WriteAllTextAsync(docPath, JsonSerializer.Serialize(usersCollection));
}
Thread.Sleep(200);
return req;
}
);
await pageIterator.IterateAsync();
// Write last page
string docPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), $"users_all.json");
System.IO.File.WriteAllTextAsync(docPath, JsonSerializer.Serialize(usersCollection));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
After you export the users, you can import them back to the other tenant. The following example creates test users. Change the code to set the values from the files you exported earlier. Also, this code uses batch with 20 users in single operation.
public static async Task CreateTestUsers(GraphServiceClient graphClient, AppSettings appSettings, bool addMissingUsers)
{
Console.Write("Enter the from value: ");
int from = int.Parse(Console.ReadLine()!);
Console.Write("Enter the to value: ");
int to = int.Parse(Console.ReadLine()!);
int count = 0;
Console.WriteLine("Starting create test users operation...");
DateTime startTime = DateTime.Now;
Dictionary<string, string> existingUsers = new Dictionary<string, string>();
// Add the missing users
if (addMissingUsers)
{
// Set a variable to the Documents path.
string docPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "users.json");
if (!System.IO.File.Exists(docPath))
{
Console.WriteLine("Can't find the '{docPath}' file.");
}
string usersFile = System.IO.File.ReadAllText(docPath);
existingUsers = JsonSerializer.Deserialize<Dictionary<string, string>>(usersFile);
if (existingUsers == null)
{
Console.WriteLine("Can't deserialize users");
return;
}
Console.WriteLine($"There are {existingUsers.Count} in the directory");
}
List<User> users = new List<User>();
// The batch object
var batchRequestContent = new BatchRequestContent();
for (int i = from; i < to; i++)
{
// 1,000,000
string ID = TEST_USER_PREFIX + i.ToString().PadLeft(7, '0');
if (addMissingUsers)
{
if (existingUsers.ContainsKey(ID))
continue;
}
count++;
try
{
var user = new User
{
DisplayName = ID,
JobTitle = ID.Substring(ID.Length - 1),
Identities = new List<ObjectIdentity>()
{
new ObjectIdentity
{
SignInType = "userName",
Issuer = appSettings.TenantName,
IssuerAssignedId = ID
},
new ObjectIdentity
{
SignInType = "emailAddress",
Issuer = appSettings.TenantName,
IssuerAssignedId = $"{ID}#{TEST_USER_SUFFIX}"
}
},
PasswordProfile = new PasswordProfile
{
Password = "1",
ForceChangePasswordNextSignIn = false
},
PasswordPolicies = "DisablePasswordExpiration,DisableStrongPassword"
};
users.Add(user);
if (addMissingUsers)
{
Console.WriteLine($"Adding missing {ID} user");
}
// POST requests are handled a bit differently
// The SDK request builders generate GET requests, so
// you must get the HttpRequestMessage and convert to a POST
var jsonEvent = graphClient.HttpProvider.Serializer.SerializeAsJsonContent(user);
HttpRequestMessage addUserRequest = graphClient.Users.Request().GetHttpRequestMessage();
addUserRequest.Method = HttpMethod.Post;
addUserRequest.Content = jsonEvent;
if (batchRequestContent.BatchRequestSteps.Count >= BATCH_SIZE)
{
var d = DateTime.Now - startTime;
Console.WriteLine($"{string.Format(TIME_FORMAT, d.Days, d.Hours, d.Minutes, d.Seconds)}, count: {count}, user: {ID}");
// Run sent the batch requests
var returnedResponse = await graphClient.Batch.Request().PostAsync(batchRequestContent);
// Dispose the HTTP request and empty the batch collection
foreach (var step in batchRequestContent.BatchRequestSteps) ((BatchRequestStep)step.Value).Request.Dispose();
batchRequestContent = new BatchRequestContent();
}
// Add the event to the batch operations
batchRequestContent.AddBatchRequestStep(addUserRequest);
// Console.WriteLine($"User '{user.DisplayName}' successfully created.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

Trying to display name of users on support action bar

Please i am new to kotlin and i have been trying to retrive the fullname of my users in the data base after using getStringExtra so i can use each of them for the title of my support action bar but the value it returns an empty value.
val name = intent.getStringExtra("fullname")
val receiverUid = intent.getStringExtra("uid")
val senderUid = FirebaseAuth.getInstance().currentUser?.uid
mDbRef = FirebaseDatabase.getInstance().getReference()
senderRoom = receiverUid + senderUid
receiverRoom = senderUid + receiverUid
supportActionBar?.title = name
Here is how i am sending name and receiverUid to my database
sendButton.setOnClickListener{
val message = messageBox.text.toString()
val messageObject = Message(message, senderUid)
val name = name.toString()
val receiverUid = receiverUid.toString()
val chatlistObject = ModelChatList(name, message, receiverUid)
mDbRef.child("chats").child(senderRoom!!).child("messages").push()
.setValue(messageObject).addOnSuccessListener {
mDbRef.child("chats").child(receiverRoom!!).child("messages").push()
.setValue(messageObject).addOnSuccessListener {
mDbRef.child("Chatlist").push().setValue(chatlistObject)
}
}
messageBox.setText("")
But when i try to retrive my ModelChatlist object it returns only two values leaving out the receivername as shown in the ModelChatlist below
class ModelChatList {
private var receivername:String = ""
private var lastmessage:String = ""
private var uid:String = ""
constructor(){}
constructor(receivername: String, lastmessage:String, uid:String){
this.receivername = receivername
this.lastmessage = lastmessage
this.uid =uid
}
fun getReceivername(): String{
return receivername
}
fun setReceivername(receivername: String){
this.receivername= receivername
}
fun getUid(): String{
return uid
}
fun setUid(uid: String){
this.uid= uid
}
fun getLastmessage(): String{
return lastmessage
}
fun setLastmessage(lastmessage: String){
this.lastmessage= lastmessage
}
}
In case you are wondering, "fullname" is in my database as the name the users enter when they sign up. Thank You

An unexpected null key in HashSet, the first be added not the rest

I'm learning Activiti 7, I drew a BPMN diagram as below:
When the highlight1 UserTask has been completed but the highlight2 UserTask is still pending, I ran the following code to highlight the completed flow element.
private AjaxResponse highlightHistoricProcess(#RequestParam("instanceId") String instanceId,
#AuthenticationPrincipal UserInfo userInfo) {
try {
// Get the instance from the history table
HistoricProcessInstance instance = historyService
.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel(instance.getProcessDefinitionId());
Process process = bpmnModel.getProcesses().get(0);
// Get all process elements, including sequences, events, activities, etc.
Collection<FlowElement> flowElements = process.getFlowElements();
Map<String, String> sequenceFlowMap = Maps.newHashMap();
flowElements.forEach(e -> {
if (e instanceof SequenceFlow) {
SequenceFlow sequenceFlow = (SequenceFlow) e;
String sourceRef = sequenceFlow.getSourceRef();
String targetRef = sequenceFlow.getTargetRef();
sequenceFlowMap.put(sourceRef + targetRef, sequenceFlow.getId());
}
});
// Get all historical Activities, i.e. those that have been executed and those that are currently being executed
List<HistoricActivityInstance> actList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.list();
// Each history Activity is combined two by two
Set<String> actPairSet = new HashSet<>();
for (HistoricActivityInstance actA : actList) {
for (HistoricActivityInstance actB : actList) {
if (actA != actB) {
actPairSet.add(actA.getActivityId() + actB.getActivityId());
}
}
}
// Highlight Link ID
Set<String> highSequenceSet = Sets.newHashSet();
actPairSet.forEach(actPair -> {
logger.info("actPair:{}, seq:{}", actPair, sequenceFlowMap.get(actPair));
highSequenceSet.add(sequenceFlowMap.get(actPair));
logger.info("{}",highSequenceSet.toString());
});
// Get the completed Activity
List<HistoricActivityInstance> finishedActList = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.finished()
.list();
// Highlight the completed Activity
Set<String> highActSet = Sets.newHashSet();
finishedActList.forEach(point -> highActSet.add(point.getActivityId()));
// Get the pending highlighted node, i.e. the currently executing node
List<HistoricActivityInstance> unfinishedActList = historyService
.createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.unfinished()
.list();
Set<String> unfinishedPointSet = Sets.newHashSet();
unfinishedActList.forEach(point -> unfinishedPointSet.add(point.getActivityId()));
...
return AjaxResponse.ajax(ResponseCode.SUCCESS.getCode(),
ResponseCode.SUCCESS.getDesc(),
null);
} catch (Exception e) {
e.printStackTrace();
return AjaxResponse.ajax(ResponseCode.ERROR.getCode(),
"highlight failure",
e.toString());
}
}
Please see this piece of code:
// Highlight Link ID
Set<String> highSequenceSet = Sets.newHashSet();
actPairSet.forEach(actPair -> {
logger.info("actPair:{}, seq:{}", actPair, sequenceFlowMap.get(actPair));
highSequenceSet.add(sequenceFlowMap.get(actPair));
logger.info("{}",highSequenceSet.toString());
});
It was expected to get 2 elements in the highSequenceSet, but it got 3, with a unexpected null.
The log printed in the console was:
Why is the first null added to the HashSet but not the rest?
Why is the first null added to the HashSet but not the rest?
HashSet implements the Set interface, duplicate values are not allowed.

Efficient Data Structure for TimeZone.knownTimeZoneIdentifiers?

I have an array of Strings returned from TimeZone.knownTimeZoneIdentifiers that I would like to parse into a nested dictionary (or another data structure).
The format of the [String] is
["Africa/Abidjan", "Africa/Accra",..,"America/Araguaina","America/Argentina/Buenos_Aires", ...,"Pacific/Marquesas", "Pacific/Midway", "Pacific/Nauru"]
The end result would be something like
["Africa":["Abidjan":[], "Accra":[]], "America": ["Araguaina": [], "Argentina": ["Buenos_Aires"]], "Pacific": ["Marquesas":[], "Midway":[], "Nauru":[]]]
I will need to query all of the keys at each level of the dictionary.
There must be another way to do this functionally or with recursion, rather than split(seperator: "/") and several for loops to build the data structure manually.
Don't use a dictionary for this. Make a custom type. In particular, you are describing a tree. Let's make one (largely based on https://github.com/raywenderlich/swift-algorithm-club/tree/master/Tree, and if there is more that you need to do, modify the Node class itself to meet your needs):
class Node<T> {
var value: T
weak var parent: Node?
var children = [Node<T>]()
init(_ value: T) {
self.value = value
}
func add(_ node: Node<T>) {
children.append(node)
node.parent = self
}
}
Now it's trivial:
let ids = TimeZone.knownTimeZoneIdentifiers
let root = Node("")
for id in ids {
var node = root
let splits = id.split(separator: "/").map(String.init)
for split in splits {
if let child = node.children.first(where:{$0.value == split}) {
node = child
} else {
let newnode = Node(split)
node.add(newnode)
node = newnode
}
}
}
Okay, let's see what we've got. It will be useful to have a nice way of printing a node:
extension Node: CustomStringConvertible {
private func display(level:Int) -> String {
let offset = String(repeating: " ", count: level * 4)
var s = offset + String(describing: value)
if children.isEmpty { return s }
s += " {\n"
s += children.map { $0.display(level:level+1) }.joined(separator: ",\n")
s += "\n\(offset)}"
return s
}
var description: String { return display(level:0) }
}
Now:
root.children.forEach {print($0)}
Result:
Africa {
Abidjan,
Accra,
Addis_Ababa,
Algiers,
Asmara,
Bamako,
Bangui,
Banjul,
Bissau,
Blantyre,
Brazzaville,
Bujumbura,
Cairo,
Casablanca,
Ceuta,
Conakry,
Dakar,
Dar_es_Salaam,
Djibouti,
Douala,
El_Aaiun,
Freetown,
Gaborone,
Harare,
Johannesburg,
Juba,
Kampala,
Khartoum,
Kigali,
Kinshasa,
Lagos,
Libreville,
Lome,
Luanda,
Lubumbashi,
Lusaka,
Malabo,
Maputo,
Maseru,
Mbabane,
Mogadishu,
Monrovia,
Nairobi,
Ndjamena,
Niamey,
Nouakchott,
Ouagadougou,
Porto-Novo,
Sao_Tome,
Tripoli,
Tunis,
Windhoek
}
America {
Adak,
Anchorage,
Anguilla,
Antigua,
Araguaina,
Argentina {
Buenos_Aires,
Catamarca,
Cordoba,
Jujuy,
La_Rioja,
Mendoza,
Rio_Gallegos,
Salta,
San_Juan,
San_Luis,
Tucuman,
Ushuaia
},
Aruba,
Asuncion,
Atikokan,
Bahia,
Bahia_Banderas,
Barbados,
Belem,
Belize,
Blanc-Sablon,
Boa_Vista,
Bogota,
Boise,
Cambridge_Bay,
Campo_Grande,
Cancun,
Caracas,
Cayenne,
Cayman,
Chicago,
Chihuahua,
Costa_Rica,
Creston,
Cuiaba,
Curacao,
Danmarkshavn,
Dawson,
Dawson_Creek,
Denver,
Detroit,
Dominica,
Edmonton,
Eirunepe,
El_Salvador,
Fort_Nelson,
Fortaleza,
Glace_Bay,
Godthab,
Goose_Bay,
Grand_Turk,
Grenada,
Guadeloupe,
Guatemala,
Guayaquil,
Guyana,
Halifax,
Havana,
Hermosillo,
Indiana {
Indianapolis,
Knox,
Marengo,
Petersburg,
Tell_City,
Vevay,
Vincennes,
Winamac
},
Inuvik,
Iqaluit,
Jamaica,
Juneau,
Kentucky {
Louisville,
Monticello
},
Kralendijk,
La_Paz,
Lima,
Los_Angeles,
Lower_Princes,
Maceio,
Managua,
Manaus,
Marigot,
Martinique,
Matamoros,
Mazatlan,
Menominee,
Merida,
Metlakatla,
Mexico_City,
Miquelon,
Moncton,
Monterrey,
Montevideo,
Montreal,
Montserrat,
Nassau,
New_York,
Nipigon,
Nome,
Noronha,
North_Dakota {
Beulah,
Center,
New_Salem
},
Nuuk,
Ojinaga,
Panama,
Pangnirtung,
Paramaribo,
Phoenix,
Port-au-Prince,
Port_of_Spain,
Porto_Velho,
Puerto_Rico,
Punta_Arenas,
Rainy_River,
Rankin_Inlet,
Recife,
Regina,
Resolute,
Rio_Branco,
Santa_Isabel,
Santarem,
Santiago,
Santo_Domingo,
Sao_Paulo,
Scoresbysund,
Shiprock,
Sitka,
St_Barthelemy,
St_Johns,
St_Kitts,
St_Lucia,
St_Thomas,
St_Vincent,
Swift_Current,
Tegucigalpa,
Thule,
Thunder_Bay,
Tijuana,
Toronto,
Tortola,
Vancouver,
Whitehorse,
Winnipeg,
Yakutat,
Yellowknife
}
Antarctica {
Casey,
Davis,
DumontDUrville,
Macquarie,
Mawson,
McMurdo,
Palmer,
Rothera,
South_Pole,
Syowa,
Troll,
Vostok
}
Arctic {
Longyearbyen
}
Asia {
Aden,
Almaty,
Amman,
Anadyr,
Aqtau,
Aqtobe,
Ashgabat,
Atyrau,
Baghdad,
Bahrain,
Baku,
Bangkok,
Barnaul,
Beirut,
Bishkek,
Brunei,
Calcutta,
Chita,
Choibalsan,
Chongqing,
Colombo,
Damascus,
Dhaka,
Dili,
Dubai,
Dushanbe,
Famagusta,
Gaza,
Harbin,
Hebron,
Ho_Chi_Minh,
Hong_Kong,
Hovd,
Irkutsk,
Jakarta,
Jayapura,
Jerusalem,
Kabul,
Kamchatka,
Karachi,
Kashgar,
Kathmandu,
Katmandu,
Khandyga,
Krasnoyarsk,
Kuala_Lumpur,
Kuching,
Kuwait,
Macau,
Magadan,
Makassar,
Manila,
Muscat,
Nicosia,
Novokuznetsk,
Novosibirsk,
Omsk,
Oral,
Phnom_Penh,
Pontianak,
Pyongyang,
Qatar,
Qostanay,
Qyzylorda,
Rangoon,
Riyadh,
Sakhalin,
Samarkand,
Seoul,
Shanghai,
Singapore,
Srednekolymsk,
Taipei,
Tashkent,
Tbilisi,
Tehran,
Thimphu,
Tokyo,
Tomsk,
Ulaanbaatar,
Urumqi,
Ust-Nera,
Vientiane,
Vladivostok,
Yakutsk,
Yangon,
Yekaterinburg,
Yerevan
}
Atlantic {
Azores,
Bermuda,
Canary,
Cape_Verde,
Faroe,
Madeira,
Reykjavik,
South_Georgia,
St_Helena,
Stanley
}
Australia {
Adelaide,
Brisbane,
Broken_Hill,
Currie,
Darwin,
Eucla,
Hobart,
Lindeman,
Lord_Howe,
Melbourne,
Perth,
Sydney
}
Europe {
Amsterdam,
Andorra,
Astrakhan,
Athens,
Belgrade,
Berlin,
Bratislava,
Brussels,
Bucharest,
Budapest,
Busingen,
Chisinau,
Copenhagen,
Dublin,
Gibraltar,
Guernsey,
Helsinki,
Isle_of_Man,
Istanbul,
Jersey,
Kaliningrad,
Kiev,
Kirov,
Lisbon,
Ljubljana,
London,
Luxembourg,
Madrid,
Malta,
Mariehamn,
Minsk,
Monaco,
Moscow,
Oslo,
Paris,
Podgorica,
Prague,
Riga,
Rome,
Samara,
San_Marino,
Sarajevo,
Saratov,
Simferopol,
Skopje,
Sofia,
Stockholm,
Tallinn,
Tirane,
Ulyanovsk,
Uzhgorod,
Vaduz,
Vatican,
Vienna,
Vilnius,
Volgograd,
Warsaw,
Zagreb,
Zaporozhye,
Zurich
}
GMT
Indian {
Antananarivo,
Chagos,
Christmas,
Cocos,
Comoro,
Kerguelen,
Mahe,
Maldives,
Mauritius,
Mayotte,
Reunion
}
Pacific {
Apia,
Auckland,
Bougainville,
Chatham,
Chuuk,
Easter,
Efate,
Enderbury,
Fakaofo,
Fiji,
Funafuti,
Galapagos,
Gambier,
Guadalcanal,
Guam,
Honolulu,
Johnston,
Kiritimati,
Kosrae,
Kwajalein,
Majuro,
Marquesas,
Midway,
Nauru,
Niue,
Norfolk,
Noumea,
Pago_Pago,
Palau,
Pitcairn,
Pohnpei,
Ponape,
Port_Moresby,
Rarotonga,
Saipan,
Tahiti,
Tarawa,
Tongatapu,
Truk,
Wake,
Wallis
}
You can use reduce(into:) and populate your dictionary using Key-based subscript default value:
let dictionary: [String: [String]] = TimeZone.knownTimeZoneIdentifiers.reduce(into: [:]) {
if let index = $1.firstIndex(of: "/") {
$0[.init($1[..<index]), default: []].append(.init($1[$1.index(after: index)...]))
}
}
Then you can map your dictionary values. If you find the slash append to the array otherwise set an empty collection to the key.
let result = dictionary.mapValues { string -> [String: [String]] in
string.reduce(into: [:]) {
if let index = $1.firstIndex(of: "/") {
$0[.init($1[..<index]), default: []].append(.init($1[$1.index(after: index)...]))
} else {
$0[$1] = []
}
}
}
If you would like to do that in a single pass:
let result: [String: [String:[String]]] = TimeZone.knownTimeZoneIdentifiers.reduce(into: [:]) {
if let index = $1.firstIndex(of: "/") {
let key = String($1[..<index])
let value = String($1[$1.index(after: index)...])
if let index = value.firstIndex(of: "/") {
let country = String(value[..<index])
let city = String(value[value.index(after: index)...])
$0[key, default: [:]][country, default: []].append(city)
} else {
$0[key, default: [:]][value] = []
}
}
}
result.forEach({print("key:",$0.key, "values:", $0.value)})

How to do CRUD operations on multiple tables in SQLite database in Flutter?

I am building a money budgeting app where the user can split their monthly salary in 5 areas: food, social, education, travel, savings. I have a register and log in screen. This is in Flutter/dart and I'm using sqlite.
Some of the data to store:
id, username, password, email, monthly_salary, food_initial, food_final, social_initial, social_final, education_initial, education_final, travel_initial, travel_final, savings_initial, savings_final
The 'initial' refers to a value that the user will input for themselves based on the division of the monthly salary. (ie. $5000 a month/5 containers = 1000 for each container. but, the user can edit this value of 1000 as their initial budgeted money). The 'final' refers to the user's remaining money (under the assumption they don't go over) for that container.
The initial and final values get reset at the end of the month. However, I would like to save their previous month's final values so the user can see a backlog of what they did.
I initially had 2 tables for my database. One was for the USER_DATA and the other was BUDGETS table. However, after struggling to figure out how to do CRUD operations for two tables, I decided to make it all into one table. But, after experiencing trouble with one table, I want to move back to more than one table. I'm confused now on:
1. How to use CRUD operations when having more than one table on DB?
2. Do I have to build a new model class in order to have more than one table?
3. How to save some of the user's data on database, but have to wait for the rest of the information to come in (later on while they're using the app)?
When making the constructor for the model class, and then referring to the model class in the UI, it requires me to bring in all of the parameters I have, but I don't have all of those values ready yet. The user still needs to register before they can input their salary and etc. Would I have to use the Future class to get over this hurdle?
I've watched a lot of videos on others building their databases in sqlite and with Flutter but all of them usually do a simple To-Do list app where they will need to fill in every column of the table and I haven't seen one yet that includes several tables. (ie. columns: id, description, priority, date)
--
I think have a good basis now on the UI side and using TextFormFields to access the information, but connecting the database/creating it properly is confusing me.
I am also considering writing the database in a .sql file now before proceeding. I have been teaching myself Flutter through tutorials (as background knowledge).
I know this is a bit long and all over the place, but I would greatly appreciate anyone who has experience with the sqlite environment in Flutter to please help me understand how to do this correctly.
Thank you. Also, if there is any more clarification/code needed, let me know.
Model class:
import 'dart:core';
class UserData {
//user table
int _userid;
String _username;
String _password;
String _email;
String _first_name;
String _last_name;
String _date;
int _month_id;
bool _subtract; //did the user subtract money from their funds? true/false
double _month_salary;
String _log_details;
/////////////budget table
double _edu_budget_initial;
double _edu_budget_final;
double _travel_budget_initial;
double _travel_budget_final;
double _living_budget_initial;
double _living_budget_final;
double _savings_budget_initial;
double _savings_budget_final;
double _social_budget_initial;
double _social_budget_final;
// using [___] around your user's data entry makes it optional in the table
// otherwise, these are all required fields
UserData(
this._username,
this._email,
this._first_name,
this._last_name,
this._password,
this._date,
this._month_id,
this._subtract,
this._month_salary,
this._log_details,
this._edu_budget_final,
this._edu_budget_initial,
this._living_budget_final,
this._living_budget_initial,
this._savings_budget_final,
this._savings_budget_initial,
this._social_budget_final,
this._social_budget_initial,
this._travel_budget_final,
this._travel_budget_initial);
//created a Named Constructor for the ability to create multiple constructors in one class
UserData.withId(
this._userid,
this._username,
this._password,
this._first_name,
this._last_name,
this._email,
this._date,
this._month_id,
this._subtract,
this._month_salary,
this._log_details,
this._edu_budget_final,
this._edu_budget_initial,
this._living_budget_final,
this._living_budget_initial,
this._savings_budget_final,
this._savings_budget_initial,
this._social_budget_final,
this._social_budget_initial,
this._travel_budget_final,
this._travel_budget_initial);
//UserData.budgetTable(this.date, this.month_id, this.log_details, this.edu_budget_final, this.edu_budget_initial, this.living_budget_final, this.living_budget_initial,
//this.month_salary, this.savings_budget_final, this.savings_budget_initial, this.social_budget_final,
//this.social_budget_initial, this.travel_budget_final, this.travel_budget_initial);
//getters:
//userdata table
int get userid => _userid;
String get username => _username;
String get password => _password;
String get email => _email;
String get firstname => _first_name;
String get lastname => _last_name;
String get date => _date;
//budget table
int get month_id => _month_id;
bool get subtract => _subtract;
double get month_salary => _month_salary;
String get log_details => _log_details;
double get living_budget_initial => _living_budget_initial;
double get living_budget_final => _living_budget_final;
double get social_budget_initial => _social_budget_initial;
double get social_budget_final => _social_budget_final;
double get edu_budget_initial => _edu_budget_initial;
double get edu_budget_final => _edu_budget_final;
double get travel_budget_initial => _travel_budget_initial;
double get travel_budget_final => _travel_budget_final;
double get savings_budget_initial => _savings_budget_initial;
double get savings_budget_final => _savings_budget_final;
//setters:
/*
set userid(int newUserID) {
//adding condition before storing what user put in
if (newUserID <= 12) {
this._userid = newUserID;
}
}
*/
set username(String newUsername) {
if (newUsername.length <= 30) {
this._username = newUsername;
}
}
set password(String newPassword) {
if (newPassword.length <= 30)
this._password = newPassword;
}
set email(String newEmail) {
if (newEmail.length <= 50)
this._email = newEmail;
}
set first_name(String newFirstName) {
if (newFirstName.length <= 30)
this._first_name = newFirstName;
}
set last_name(String newLastName) {
if (newLastName.length <= 30)
this._last_name = newLastName;
}
set month_id(int newMonthId) {
if (newMonthId <= 12) {
this._month_id = newMonthId;
}
}
set month_salary(double newMonthSalary) {
this._month_salary = newMonthSalary;
}
set date(String newDate) {
this._date = newDate;
}
set log_details(String newLogDetails) {
if (newLogDetails.length <= 255)
this._log_details = newLogDetails;
}
set subtract(bool newSubtract) {
this._subtract = newSubtract;
}
set living_budget_initial(double newLBI) {
if (newLBI <= 10) this._living_budget_initial = newLBI;
}
set living_budget_final(double newLBF) {
if (newLBF <= 10) this._living_budget_final = newLBF;
}
set social_budget_initial(double newSOBI) {
if (newSOBI <= 10) this._social_budget_initial = newSOBI;
}
set social_budget_final(double newSOBF) {
if (newSOBF <= 10) this._social_budget_final = newSOBF;
}
set edu_budget_initial(double newEBI) {
if (newEBI <= 10) this._edu_budget_initial = newEBI;
}
set edu_budget_final(double newEBF) {
if (newEBF <= 10) this._edu_budget_final = newEBF;
}
set travel_budget_initial(double newTBI) {
if (newTBI <= 10) this._travel_budget_initial = newTBI;
}
set travel_budget_final(double newTBF) {
if (newTBF <= 10) this._travel_budget_final = newTBF;
}
set savings_budget_initial(double newSBI) {
if (newSBI <= 10) this._savings_budget_initial = newSBI;
}
set savings_budget_final(double newSBF) {
if (newSBF <= 10) this._savings_budget_final = newSBF;
}
// converting object into Map objects
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
var userid;
if (userid != null) {
map['userid'] = _userid;
}
map['userid'] = _userid;
map['username'] = _username;
map['password'] = _password;
map['email'] = _email;
map['first_name'] = _first_name;
map['last_name'] = _last_name;
//
map['month_id'] = _month_id;
map['month_salary'] = _month_salary;
map['date'] = _date;
map['log_details'] = _log_details;
map['subtract'] = _subtract;
map['living_budget_initial'] = _living_budget_initial;
map['living_budget_final'] = _living_budget_final;
map['social_budget_initial'] = _social_budget_initial;
map['social_budget_final'] = _social_budget_final;
map['edu_budget_initial'] = _edu_budget_initial;
map['edu_budget_final'] = _edu_budget_final;
map['travel_budget_initial'] = _travel_budget_initial;
map['travel_budget_final'] = _travel_budget_final;
map['savings_budget_initial'] = _savings_budget_initial;
map['savings_budget_final'] = _savings_budget_final;
return map;
}
//converts map objects into objects
UserData.fromMapObject(Map<String, dynamic> map) {
this._userid = map['userid'];
this._username = map['username'];
this._password = map['password'];
this._email = map['email'];
this.first_name = map['first_name'];
this.last_name = map['last_name'];
//
this._month_id = map['month_id'];
this._month_salary = map['month_salary'];
this._date = map['date'];
this._log_details = map['log_details'];
this._subtract = map['subtract'];
this._living_budget_initial = map['living_budget_initial'];
this._living_budget_final = map['living_budget_final'];
this._social_budget_initial = map['social_budget_initial'];
this._social_budget_final = map['social_budget_final'];
this._edu_budget_initial = map['edu_budget_initial'];
this._edu_budget_final = map['edu_budget_final'];
this._travel_budget_initial = map['travel_budget_initial'];
this._travel_budget_final = map['travel_budget_final'];
this._savings_budget_initial = map['savings_budget_initial'];
this._savings_budget_final = map['savings_budget_final'];
}
}
Database helper class:
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:jewell_1/user_data.dart';
class DbHelper {
// Create a private instance of the class
static final DbHelper _dbhelper = new DbHelper._internal();
String DB_name = "user_data.db";
static final int DATABASE_VERSION = 2;
String userDataTable = 'user_data_table';
String colUserId = 'userid';
String colUsername = 'username';
String colPassword = 'password';
String colEmail = 'email';
String colFirstName = 'first_name';
String colLastName = 'last_name';
String colDate = 'date';
String colMonthId = 'month_id';
String colSubtract = 'subtract';
String colMonthSalary = 'month_salary';
String colLogDetails = 'log_details';
String colLBI = 'living_budget_initial';
String colLBF = 'living_budget_final';
String colSOBI = 'social_budget_initial';
String colSOBF = 'social_budget_final';
String colEBI = 'edu_budget_initial';
String colEBF = 'edu_budget_final';
String colTBI = 'travel_budget_initial';
String colTBF = 'travel_budget_final';
String colSBI = 'savings_budget_initial';
String colSBF = 'savings_budget_final';
// Create an empty private named constructor
DbHelper._internal();
// Use the factory to always return the same instance
factory DbHelper() {
return _dbhelper;
}
static Database _db;
Future<Database> get db async {
if (_db == null) {
_db = await initializeDb();
}
return _db;
}
Future<Database> initializeDb() async {
Directory dir = await getApplicationDocumentsDirectory();
String path = dir.path + "todos.db";
var dbTodos = await openDatabase(path, version: DATABASE_VERSION, onCreate: _createDb);
return dbTodos;
}
void _createDb(Database db, int newVersion) async {
await db.execute(
"CREATE TABLE $userDataTable($colUserId INETEGER PRIMARY KEY, $colUsername TEXT," +
"$colPassword TEXT, $colEmail TEXT, $colFirstName TEXT,"
"$colLastName TEXT, $colDate TEXT, $colMonthId INTEGER,"
" $colSubtract BOOLEAN, $colMonthSalary DOUBLE, $colLogDetails TEXT,"
" $colEBI DOUBLE, $colEBF DOUBLE, $colTBI DOUBLE, $colTBF DOUBLE, "
" $colLBI DOUBLE, $colLBF DOUBLE, $colSBI DOUBLE, $colSBF DOUBLE,"
" $colSOBI DOUBLE, $colSOBF DOUBLE)");
}
Future<int> insertData(UserData data) async {
Database db = await this.db;
var result = await db.insert(userDataTable, data.toMap());
return result;
}
Future<List> getDatas() async {
Database db = await this.db;
var result =
await db.rawQuery("SELECT * FROM $userDataTable order by $colUserId ASC");
return result;
}
Future<int> getCount() async {
Database db = await this.db;
var result = Sqflite.firstIntValue(
await db.rawQuery("SELECT COUNT (*) FROM $userDataTable"));
return result;
}
Future<int> updateData(UserData data) async {
var db = await this.db;
var result = await db
.update(userDataTable, data.toMap(), where: "$colUserId=?", whereArgs: [data.userid]);
return result;
}
Future<int> deleteData(int id) async {
int result;
var db = await this.db;
result = await db.rawDelete("DELETE FROM $userDataTable WHERE $colUserId=$id");
return result;
}
}
To be more specific, I am confused on not writing the query of a new table, but rather at this point:
var result = await db.insert(userDataTable, data.toMap());
return result;
The result is only containing my userDataTable and if I create another table, then am I supposed to add another var result? I tried to have:
var result2 = await db.insert(budgetsTable, data.toMap());
This seemed wrong. Also, I didn't know how to return both results.
That was specifically where/when I started to scavenge the internet of people who have made CRUD operations using more than one table on sqlite with Flutter.

Resources