Skip to content

Commit

Permalink
android add native contact implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
richtwin567 committed Aug 23, 2020
1 parent f53668f commit eaf9892
Show file tree
Hide file tree
Showing 33 changed files with 838 additions and 169 deletions.
14 changes: 11 additions & 3 deletions android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android"
package="com.example.fst_app_flutter">
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android" package="com.example.fst_app_flutter">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<application>
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
14 changes: 14 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android"
package="com.example.fst_app_flutter">

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name="io.flutter.app.FlutterApplication"
android:label="fst_app_flutter"
Expand Down Expand Up @@ -43,5 +47,15 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.fst_app_flutter/contacts";

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(
flutterEngine.getDartExecutor().getBinaryMessenger(),
CHANNEL
)
.setMethodCallHandler(
(call, result) -> {
// Note: this method is invoked on the main thread.
// TODO
}
);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.fst_app_flutter;

public class NativeContact {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.fst_app_flutter

import android.content.Intent
import android.net.Uri
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
Expand Down
4 changes: 4 additions & 0 deletions android/app/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http:https://schemas.android.com/apk/res/android">
<cache-path name="external_files" path="."/>
</paths>
4 changes: 4 additions & 0 deletions android/app/src/profile/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android"
package="com.example.fst_app_flutter">

<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
1 change: 1 addition & 0 deletions android/settings_aar.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':app'
1 change: 0 additions & 1 deletion assets/rive_animated_icons/clear-white-24dp.svg

This file was deleted.

1 change: 0 additions & 1 deletion assets/rive_animated_icons/search-white-24dp.svg

This file was deleted.

Binary file not shown.
2 changes: 2 additions & 0 deletions ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSContactsUsageDescription</key>
<string>Save contacts.</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down
2 changes: 2 additions & 0 deletions ios/podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target 'Runner' do
use_frameworks!
7 changes: 4 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import 'package:flutter/services.dart';
import 'package:fst_app_flutter/models/preferences/theme_model.dart';
import 'package:fst_app_flutter/routing/generate_routes.dart';
import 'package:fst_app_flutter/utils/app_theme.dart';
import 'package:fst_app_flutter/utils/precache_rive.dart';
import 'package:provider/provider.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
ThemeModel themeModel = ThemeModel();
await themeModel.init();
runApp(DevicePreview(
child: FSTApp(
await precacheRive();
runApp(FSTApp(
themeModel: themeModel,
)));
));
}

class FSTApp extends StatelessWidget {
Expand Down
166 changes: 166 additions & 0 deletions lib/models/from_postgres/contact/contact_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter_contact/contact.dart';
import 'package:fst_app_flutter/models/from_postgres/contact/contact_type.dart';
import 'package:fst_app_flutter/models/from_postgres/contact/department.dart';
import 'package:fst_app_flutter/models/from_postgres/contact/platform.dart';

class Contact {
int _id;
String _name;
String _website;
String _email;
String _fax;
String _description;
Department _department;
ContactType _contactType;
List<_PhoneNumber> _phones;

int get id => _id;

String get name => _name;

String get website => _website;

String get email => _email;

String get fax => _fax;

String get description => _description;

Department get department => _department;

ContactType get contactType => _contactType;

List<_PhoneNumber> get phones => _phones;

Contact(dynamic contact) {
List<_PhoneNumber> phones =
List.generate(contact['phone_contact_set'].length, (i) {
return _PhoneNumber(contact['phone_contact_set'][i]);
});
Department department;
switch (contact['department']) {
case 'CHEM':
department = Department.CHEM;
break;
case 'COMP':
department = Department.COMP;
break;
case 'GEO':
department = Department.GEO;
break;
case 'LIFE':
department = Department.LIFE;
break;
case 'MATH':
department = Department.MATH;
break;
case 'PHYS':
department = Department.PHYS;
break;
default:
department = Department.OTHER;
break;
}
ContactType contactType;
switch (contact['contact_type']) {
case 'EMERGENCY':
contactType = ContactType.EMERGENCY;
break;
case 'OFFICE':
contactType = ContactType.OFFICE;
break;
case 'FACULTY_STAFF':
contactType = ContactType.FACULTY_STAFF;
break;
default:
contactType = ContactType.OTHER;
break;
}
_id = contact['id'];
_name = contact['name'];
_website = contact['website'];
_email = contact['email'];
_fax = contact['fax'];
_description = contact['description'];
_department = department;
_contactType = contactType;
_phones = phones;
}

Map<String, Object> toNativeMap() {
int id = Random().nextInt(9999);
return {
//'identifier': id.toString(),
"displayName": name,
'givenName': name.split(' ')[1],
'middleName': name.split(' ').length == 4 ? _name.split(' ')[2] : '',
'familyName': name.split(' ').length == 4
? name.split(' ')[3]
: name.split(' ')[2],
'prefix': name.split(' ')[0],
'suffix': '',
'company': '',
'jobTitle': '',
'avatar': Uint8List(0),
'note': description,
"linkedContactIds": <String>[],
'dates': <ContactDate>[],
'emails': email != ''
? <Map>[
{'label': 'work', 'value': email}
]
: <Map>[],
'phones': List<Map>.generate(phones.length,
(i) => {'label': 'work', 'value': phones[i].phone})..addAll(List<Map>.generate(fax!= ''?1:0, (i) => {'label':'fax work', 'value':fax})),
"socialProfiles": <Map>[],
'postalAddresses': <PostalAddress>[],
"urls": website != ''
? <Map>[
{'label': 'website', 'value': website}
]
: <Map>[],
//"unifiedContactId": id.toString(),
//"singleContactId": id.toString(),
"otherKeys": {},
"label": '',
"date": DateTime.now().toIso8601String(),
"lastModified": DateTime.now().toIso8601String(),
"street": '',
"city": '',
"postcode": '',
"region": '',
"country": '',
'keys': ContactKeys.empty(ContactMode.single)
};
}
}

class _PhoneNumber {
int _id;
int _contactID;
String _phone;
Platform _platforms;

int get id => _id;
int get contactID => _contactID;
String get phone => _phone;
Platform get platforms => _platforms;

_PhoneNumber(dynamic phoneSet) {
Platform platform;
switch (phoneSet['platforms']) {
case 'WHATSAPP':
platform = Platform.WHATSAPP;
break;
default:
platform = Platform.TEXT_CALL;
break;
}
_id = phoneSet['id'];
_contactID = phoneSet['contact'];
_phone = phoneSet['phone'];
_platforms = platform;
}
}
7 changes: 7 additions & 0 deletions lib/models/from_postgres/contact/contact_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum ContactType { EMERGENCY, OFFICE, FACULTY_STAFF, OTHER }

extension ContactTypeShortString on ContactType {
String asString() {
return this.toString().split('.').last;
}
}
7 changes: 7 additions & 0 deletions lib/models/from_postgres/contact/department.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Department { CHEM, COMP, GEO, LIFE, MATH, PHYS, OTHER }

extension DepartmentShortString on Department {
String asString() {
return this.toString().split('.').last;
}
}
7 changes: 7 additions & 0 deletions lib/models/from_postgres/contact/platform.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum Platform { TEXT_CALL, WHATSAPP }

extension PlatformShortString on Platform {
String asString() {
return this.toString().split('.').last;
}
}
5 changes: 5 additions & 0 deletions lib/models/preferences/theme_preference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ class ThemePreference {
return ThemeMode.system;
}
}

@override
String toString() {
return 'Dark Mode On:\t${prefs.getBool(darkThemeStatus)}\nLight Mode On:\t${prefs.getBool(lightThemeStatus)}\nSystem Mode On:\t${prefs.getBool(systemThemeStatus)}';
}
}
Loading

0 comments on commit eaf9892

Please sign in to comment.